how to create new record using script in field map in transform map

Priyanka145
Tera Contributor

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

 

3 REPLIES 3

Gunjan Kiratkar
Kilo Patron
Kilo Patron

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 :-

GunjanKiratkar_0-1670332175877.png

 

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

Hi @Gunjan Kiratkar 

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

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);


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy