how to create new record using script in field map in transform map
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2022 04:27 AM - edited 12-06-2022 04:34 AM
Hi All,
I am writing transform map on User table. Based on the feed coming from third party, I want to check if the company they are sending is available in servicenow companies table, then I need to map it, else I need to create it , but the user record should not be updated. Company is a reference field on user table.
I have a field map script checking few conditions and the target table is User table , target field is company
Please help me with the script. Only selecting choice action to Create would be sufficient ?
cmp = source.u_company;
//checking if company from source is available in core_company table
grdcmp = new GlideRecord('core_company');
grdcmp.addQuery('name', cmp);
grdcmp.query();
if (grdcmp.next()) {
target.company = grdcmp.name;
}
else
{
ignore= true;
}
Please guide
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2022 05:09 AM
Hi @Priyanka145 ,
If company is not present then you just want to create the company and make the company field on user table as empty. is that correct? if yes then use below script :-
answer = (function transformEntry(source) {
// Add your code here
cmp = source.u_company;
grdcmp = new GlideRecord('core_company');
grdcmp.addQuery('name', cmp);
grdcmp.query();
if (grdcmp.next()) {
return grdcmp.name;
} else
{
var com = new GlideRecord('core_company');
com.initialize();
com.name = cmp;
com.insert();
return "";
}
// return the value to be put into the target field
})(source);
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2022 06:45 AM
It should not be emtpy, it should be with the earlier value itself(as-is)
If the previous value is empty it should be emtpy, else if its with some other company name it should be the same
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2022 06:54 AM
Hi @Priyanka145
Use below code
answer = (function transformEntry(source) { // Add your code here
cmp = source.u_company;
grdcmp = new GlideRecord('core_company');
grdcmp.addQuery('name', cmp);
grdcmp.query();
if (grdcmp.next()) {
return grdcmp.name;
} else {
var com = new GlideRecord('core_company');
com.initialize();
com.name = cmp;
com.insert();
var user=new GlideRecord('sys_user');
user.addQuery('email',source.email); //check proper user field from Excel file
user.query();
if(user.next()){
return user.company.name;
}
} // return the value to be put into the target field })(source);