Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Source Script in Tansform mapping

Priya14
Tera Contributor

I want to import beneficiary data to the employee document table so I have created a data source and transform map 

I have written the Source script for the beneficiay feild (reference field)

 

answer = (function transformEntry(source) {
    

    // Add your code here

    var name = new GlideRecord("sn_hr_core_beneficiary");
    name.addQuery("beneficiary_name",source.u_beneficiary_name);
    name.addQuery("employee",source.u_employee_name);
    name.query();
    if(name.next())
        {
            var id= current.sys_id;
            
        }    


return id; 

    
    
        
// return the value to be put into the target field
})(source);

 

When I import the beneficiary data it is not getting imported in the employee document table and getting this Message log : "Reference field value for sn_hr_ef_employee_document.u_beneficiary rejected: undefined"

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the target field which you are setting is reference to "sn_hr_core_beneficiary" right?

then you need to return that record sysId

Also employee field on "sn_hr_core_beneficiary" is reference to sys_user so you need to dot walk to name since you are getting name in your excel

answer = (function transformEntry(source) {

    // Add your code here
    var id;
    var name = new GlideRecord("sn_hr_core_beneficiary");
    name.addQuery("beneficiary_name",source.u_beneficiary_name);
    name.addQuery("employee.name",source.u_employee_name);
    name.query();
    if(name.next())
    {
        id = name.getUniqueValue();
    }    
    return id; 

    // return the value to be put into the target field
})(source);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Swathi P
Tera Guru

Hi ,

Please add name instead of current.

answer = (function transformEntry(source) {
    

    // Add your code here

 var id =""

    var name = new GlideRecord("sn_hr_core_beneficiary");
    name.addQuery("beneficiary_name",source.u_beneficiary_name);
    name.addQuery("employee",source.u_employee_name);
    name.query();
    if(name.next())
        {
          id= name.sys_id;
            
        }    


return id; 

Swathi P
Tera Guru

Hi,

 

 Employee Field in sn_hr_core_beneficiary table is reference field to user table. We need to query with the sys_id of user at Line 3. 

Add something like below . It should work . Please check.

 var id = "";
 var id1 = "";

var user = new GlideRecord('sys_user');
    user.addQuery('name' , source.u_employee);
    user.query();
    if(user.next()){
        id1 = user.sys_id;
    }

 var name = new GlideRecord("sn_hr_core_beneficiary");
    name.addQuery("beneficiary_name",source.u_beneficiary_name);
    name.addQuery("employee",source.u_employee_name);      Line 3
    name.query();
    if(name.next())
        {
            var id= current.sys_id;
            
        }    

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the target field which you are setting is reference to "sn_hr_core_beneficiary" right?

then you need to return that record sysId

Also employee field on "sn_hr_core_beneficiary" is reference to sys_user so you need to dot walk to name since you are getting name in your excel

answer = (function transformEntry(source) {

    // Add your code here
    var id;
    var name = new GlideRecord("sn_hr_core_beneficiary");
    name.addQuery("beneficiary_name",source.u_beneficiary_name);
    name.addQuery("employee.name",source.u_employee_name);
    name.query();
    if(name.next())
    {
        id = name.getUniqueValue();
    }    
    return id; 

    // return the value to be put into the target field
})(source);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader