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

For what its worth, I logged the sys_id of the user that should be showing in the opened_by field and its correct in the logs.



However, the RITM still defaults to my user name on the import.


it looks like the opened_by field is on the task table.   How can I dot walk to that field to set it?


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.


Thanks Pradeep, would you be able to post a full snippet of how the code should look? I am a little bit confused at this point.   Thanks!


Hello Josh,



Please find the update code. Please review and let me know if you have any questions.


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



  // set the opened by on the RITM


  var openedBy = new GlideRecord("sys_user");


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


  openedBy.query();


  if (openedBy.next()) {



  var opened_by = openedBy.sys_id;


  }



  var cart = new Cart(null,opened_by);


  //Catalog Item sys_id of "Other Requests"


  var item = cart.addItem('8f93da846f93de80c7fe90264b3ee41d');


  //set the additional comment variable from the data source


  var dataSource = source.u_additional_comments;


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


  ignore = true; //used so duplicate RITM's are not created




  // set the requested for on the RITM


  var gr = new GlideRecord("sys_user");


  gr.addQuery("email", source.u_requested_for_work_email);


  gr.query();


  if (gr.next()) {


  var cartGR = cart.getCart();


  cartGR.requested_for = gr.sys_id;


  cartGR.update();


  }



  gs.log(gr.sys_id);





  //gs.log('this is a test' + openedBy.sys_id, test);




  //set the requested for and manager of the requested for on the actual catalog item (separate from the RITM)


  cart.setVariable(item, 'requested_for', gr.sys_id);


  cart.setVariable(item, 'manager', gr.manager);



  // query the user table for the requested by user record


  var setRequestedBy = new GlideRecord("sys_user");


  setRequestedBy.addQuery("email", source.u_requested_by_work_email);


  setRequestedBy.query();


  if (setRequestedBy.next()) {


  var userCart = cart.getCart(); //I don't think this and next line is required here. Please remove and test once.


  userCart.update();


  }



  //set the requested by on the catalog item


  cart.setVariable(item, 'requested_by', setRequestedBy.sys_id);






  var rc = cart.placeOrder();



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