Script transform map issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2025 02:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2025 03:23 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2025 04:22 AM - edited ‎03-06-2025 04:24 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2025 04:44 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2025 06:32 AM
i've chaneg my approach as field map scripts could be overwhelming.
Now I am doing a Transform script on after :
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