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

Transform Script Field Mapping

brostoff17
Tera Contributor

I am trying to map a field from a data source through a transform script, but I keep getting an error after I run the transform.   Any idea what could be wrong here?

I want to map the additional_field (datasource) to the "additional" variable.

When:onBefore

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

var cart = new Cart();

//Catalog Item sys_id

var item = cart.addItem('10a2f5dfc6112276018db58138c7a1e0');

//set variables

var dataSource = source.additional_test;

cart.setVariable(item, 'additional', dataSource);

var rc = cart.placeOrder();

     

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

Screenshot of test data source

Capture.PNG

1 ACCEPTED SOLUTION

Finally I figured it out after spending sometime on this.


Basically to create a cart for a specific user I.e set opened_by field you need to place this line above var cart = new Cart(); in your code.



var openedBy = new GlideRecord("sys_user");


openedBy.addQuery("email", source.u_opened_by_work_email);


openedBy.query();


if (openedBy.next()) {


var openedBy = openedBy.sys_id;


}



Now modify the line var cart = new Cart(); as var cart = new Cart(null, openedBy); //This will set the openedBy field on item table.



I think you can also GlideRecord the item table and set the field. I haven't tried that option as of now. Will keep you posted.



P.S : Community is currently down. Will modify your code and update on community once it is up and running.


View solution in original post

27 REPLIES 27

Pradeep - if I wanted to ignore all user records found on the target table that were inactive, would this work?  



if(target.u_active == 'false'){


ignore=true;


}


Hi Josh,



i think the column name here should be "name" instead of "user_name" while querying user table and could you also print log message inside if loop to see if   value of "source_u_requested_for" is found in sys_user table.



gr.addQuery("user_name", source.u_requested_for);  




correction:


                    gr.addQuery("name", source.u_requested_for);  



Not sure if this is the issue here,yet you can try this if required.






Opened by is still showing as me.