Help with a field map script

AEterni
Mega Guru

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.

AEterni_0-1669375308875.png

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.

7 REPLIES 7

MD AQUIB KHAN
Giga Guru

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.

AEterni
Mega Guru

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.

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

Abhijit4
Mega Sage

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)

AEterni_0-1669375308875.png

Please mark answer as Correct or Helpful based on impact.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP