Scripting to Map a Field in Transform Map

Vamsi43
Tera Contributor

I have 2 fields on table build a transform map to load the data into table.

 

Vamsi43_0-1688310018939.png

I want to map the "Service" Field using script.
Can you please help me on this:

If the Service is not empty, map each value of the Excel with the right Service to Target.

I wrote the below script which is not working

 

answer = (function transformEntry(source) {
    if (target.u_delivery_time.nil()) {
        target.u_delivery_time = source.u_delivery_time;
    }
})(source);

Vamsi43_1-1688310393427.png

Thanks

 

11 REPLIES 11

MahaAgineo
Tera Guru

Hello @Vamsi43 ,

I think you need to return the value in you script, because you don't have access to the target object. Here what you script need to look like:

 

answer = (function transformEntry(source) {
    if (target.u_delivery_time.nil()) {
        return source.u_delivery_time;
    }
})(source);

 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up. 

best regards 

 

Hello @MahaAgineo 

 

I want to check both the conditions on the script

1. If the Service is not empty, map each value of the Excel with the right Service  to Target.

2. If Service is empty, then set the Delivery time with the value of the Catalog Item.

 

I wrote the below script which is not working.

 

answer = (function transformEntry(source) {
    if (target.u_delivery_time.nil()) {
        var delivery;
        var catalogItemGR = new GlidRecord("sc_cat_item");
        catalogItemGR.addNullQuery('delivery_time');
        catalogItemGR.query();
        while (catalogItemGR.next()) {
            delivery = source.delivery_time;
        }
        return delivery; // return the value to be put into the target field
    } else {
        return source.u_delivery_time;
    }
})(source);

  

Hello @Vamsi43,

You don't have access to the target object, so you need to use a transform map script (set run Script to true see Screenshot), and use this code:

 

(function transformRow(source, target, map, log, isUpdate) {
if (target.u_delivery_time.nil()) {
        var delivery;
        var catalogItemGR = new GlidRecord("sc_cat_item");
        catalogItemGR.addNullQuery('delivery_time');
        catalogItemGR.query();
        while (catalogItemGR.next()) {
            delivery = source.delivery_time;
        }
        target.u_delivery_time = delivery; // return the value to be put into the target field
    } else {
       target.u_delivery_time  = source.u_delivery_time;
    }
})(source, target, map, log, action === "update");

 

 

MahaAgineo_0-1688543932761.png

And I don't think you need the else because the field will be by default mapped to the source, with the normal field field mapping (without script)

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up. 

best regards 

Then what about the Field Maps script i need erase it all and map direct like this?

 

Vamsi43_0-1688549146180.png