Script transform map issue

gdo
Tera Contributor

gdo_0-1741258441086.png

I am importing from an excel sheet assets on alm_hardware.
to create them, I need the company which is stored in sys_user.u_assignment_entities.
I did this field map script but it does not return anything on the import set.
What am I doing wrong ?
Here the userName return a sys_id (of the company to be paste on the company field of the asset)
Thanks

4 REPLIES 4

Tushar
Kilo Sage
Kilo Sage

Hi @gdo -

please try below script -

 

(function transformEntry(source) { 
    var userName = "";  
    var gr = new GlideRecord('sys_user');  
    gr.addQuery('name', source.assigned_to);  
    gr.query();  

    if (gr.next()) {  
        userName = gr.getValue('u_assignment_entities'); // Corrected way to get the value  
    }  

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

 

If assigned_to stores sys_id instead of the name, you should query sys_id instead -

 

gr.addQuery('sys_id', source.assigned_to);

 

If it is a reference field, it returns a sys_id. You might need gr.getDisplayValue('u_assignment_entities') and you need the display name instead.

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks

gdo
Tera Contributor

It still doesn't work, when I tried on background script it works 😞

I've added Reference value field name = core_company 
as it is a reference of a record in the core_company table, but not sure on this.

Also, is it important to have :
answer = (function transformEntry(source) ?
I see that you didn't use it

Thank you


Ankur Bawiskar
Tera Patron
Tera Patron

@gdo 

use this

I assume you are getting name in the u_assigned_to source field

I assume u_assignment_entities is list field referring to Company table

Also update the Referenced value field name in field map = sys_id

(function transformEntry(source) {

    var companySysIds = "";
    var gr = new GlideRecord('sys_user');
    gr.addQuery('name', source.u_assigned_to); // pass the source field name
    gr.query();
    if (gr.next()) {
        companySysIds = gr.getValue('u_assignment_entities');  
    }
    return companySysIds;

})(source);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

gdo
Tera Contributor

i've chaneg my approach as field map scripts could be overwhelming.
Now I am doing a Transform script on after :

gdo_0-1741271437257.png


When I print getvalue or getdisplayvalue I have the correct information to put in company field but still nothing.
I've checked any Business rule that may bothering my transform map but nothing.
When I create the record, the company field is empty whereas I do have the correct value in target.u_company

Thanks