Help with a field map script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2022 03:22 AM
Hello All,
I am experiencing an issue with a transformation map I created. My issue is that one of the reference fields mapped in the transformation map is not picked up properly.
I logged a ticket with ServiceNow and they confirm that the transformation map is configured properly. They suggested using a script in the field mapping to make sure the proper field is picked up.
I told them that I don't have any code experience, but they can't provide any suggestions when it comes to scripting because it means "product customization" and, as a consequence, "not supported.
This is how the field map looks like.
How do I achieve the same result with a scrip? Is the following one correct?
answer = (function transformEntry(source) {
// Add your code here
return source.u_model;
})(source);
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2022 03:33 AM
you can try this
var source_model = source.u_model;
gs.log("Source:" +source_model);
target.model = source_model;
gs.log("Target:" +target.model );
Log is just to validate the values . You can comment it later.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2022 03:57 AM
Thank you.
It worked...namely, the transformation map ran successfully, but I did not get the result I am after.
A new record in the cmdb_model was created rather than using the existing one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2022 04:21 AM
var gr = new GlideRecord('cmdb_model');
gr.addQuery('display_name', source.u_model);// try with name as well if it does not work. Validate if the source values matches with the existing values .
gr.addEncodedQuery('cmdb_model_category=35bf2d4137101000deeabfc8bcbe5dbd');// model category as Software Licence
gr.query();
if(gr.next()){
var grmodel = gr.getDisplayValue();
gs.log('MODEL:' +grmodel);
var gr1 = new GlideRecord('alm_license');
gr1.addEncodedQuery('model_category=35bf2d4137101000deeabfc8bcbe5dbd'); // model category as Software licence
gr1.query();
if(gr1.next()){
gr1.model = grmodel;
//gr1.license_key = "TEST1234";
gr1.update();
gs.log('MODEL1:' +gr1.model );
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2022 04:39 AM
To avoid creation of new record, you will need to have coalesce field on transform map.
e.g. you can make your model field as coalesce field (depends on your requirement ), which will update existing record if model name matches. to avoid creation of new record, you need to select choice action as ignore ( refer below image to perform this action)
Please mark answer as Correct or Helpful based on impact.
Regards,
Abhijit
ServiceNow MVP