The CreatorCon Call for Content is officially open! Get started here.

Help with transform map scripting!

MarleyB
Tera Contributor

Hi!

 

Would like to ask some help regarding transform map scripting, this is to copy a new data uploaded from source field and target field which is located in another table. 

 

Tried BR and unable to make it work, did some research and found about transform map scripting and I'm still trying to learn it.

 

Thanks!

1 ACCEPTED SOLUTION

@MarleyB 

try this

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

	/* 1. Get the value of source field */
	var sourceValue1 = target.name;

	/* 2. Glide record on Table in which you want to update the record */
	var gr = new GlideRecord('service_offering');
	gr.initialize();
	gr.name = sourceValue1 + new GlideDateTime().getLocalDate();
	gr.insert();

})(source, map, log, target);

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

View solution in original post

14 REPLIES 14

Thanks @Ankur Bawiskar, it's working now!!!

tanuj_agarwal
Tera Contributor

@MarleyB 

If i understand correctly, your requirement is to copy the record in service_offering when you have a new record in  cmdb_ci_service.

To achieve this there can be multiple methods but the easy way was the solution you were trying before with BR.

1. Your After BR should be on the source table (cmdb_ci_service). Check only insert checkbox as it has to run only for new records . All this data will be in the current object which you will use in step 2.

2. Use this current object to create a record in your target table ( service_offering ). Set all the required fields you want in the target table by dot walking the current object.

 

I did have a look at your BR in previous comments but seems it was not correct. Also you should avoid hardcoded sys_ids . You can get them from current object.

 

Try the solution and let me know for issues with snaps to explain.

 

Thanks,

Tanuj

I updated my BR script, after insert at cmdb_ci_service, however, it is still not working. Would you please check what I missed on this script?
 
(function executeRule(current, previous /*null when async*/ ) {

    var service = new GlideRecord("cmdb_ci_service");;
    service.addQuery('name', current.sys_id);
    service.query();
    while (service.next()) {
        service.sys_id.getDisplayValues();

        var x = new GlideRecord("service_offering");
        x.get('name', current.sys_id);
        x = service;
        x.update();
    }
})(current, previous);

 
I updated my BR, after insert at cmdb_ci_service table, however still not working.
Would you please check my script to see what I missed?
 
(function executeRule(current, previous /*null when async*/ ) {

    var service = new GlideRecord("cmdb_ci_service");;
    service.addQuery('name', current.sys_id);
    service.query();
    while (service.next()) {
        service.sys_id.getDisplayValues();

        var x = new GlideRecord("service_offering");
        x.get('name', current.sys_id);
        x = service;
        x.update();
    }
})(current, previous);

Rajdeep Ganguly
Mega Guru


Sure, I can help you with that. Transform Map scripting in ServiceNow is used to map source data to target data. Here's a simple example of how you can do it:

1. Navigate to System Import Sets > Create Transform Map.
2. Click on New to create a new Transform Map.
3. Fill in the required fields (Name, Source table, Target table).
4. In the 'Field Maps' related list, click on 'Auto Map Matching Fields' or manually create field maps.
5. In the 'onBefore' or 'onAfter' script fields, you can write a script to manipulate the data during the transformation process.

Here's a simple example of a script you might use:

javascript
(function transformEntry(source, target, map, log, isUpdate) {
// This is an example script that copies data from the source 'u_source_field' to the target 'u_target_field'
target.u_target_field = source.u_source_field;
})(source, target, map, log, action);


In this script:

- source refers to the Import Set Row (the data you're importing).
- target refers to the target table (where you want the data to go).
- map is the Transform Map.
- log is a GlideRecord for the Import Log.
- action is a string that will be either 'insert' or 'update', depending on whether the Transform Map is inserting a new record or updating an existing one.

Remember to replace 'u_source_field' and 'u_target_field' with the actual field names you're using.

This script should be placed in the 'onBefore' script field if you want it to run before the transformation happens, or in the 'onAfter' script field if you want it to run after the transformation.

Please note that this is a very basic example and you might need to adjust it to fit your specific needs.


nowKB.com