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

Hi Pradeep,



I have everything working the way I want expect one last remaining thing which is setting the "opened_by" field on the RITM.I am trying to do this on lines 27-35 but it wont work.   Any ideas?   This is the last part I cant get to work.





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


     


      var cart = new Cart();


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


     


     


      // 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 setOpenedBy = cart.getCart();


              setOpenedBy.opened_by = openedBy.sys_id;


              setOpenedBy.update();


      }


     


      //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();


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


Hello Josh,



Quick question:


1.What is the target table defined on transformation map?


2.Is the req to populated opened by on RITM form?



Please confirm.


the target table is the sc_req_item (RITM) table.   Yes, I would like to populate the opened by field on the RITM form, but for some reason its not working.   I am able to successfully populate the requested for field on the RITM form.


Thanks for the update.


Change line   from


  1. var setOpenedBy = cart.getCart();  
  2.               setOpenedBy.opened_by = openedBy.sys_id;  
  3.               setOpenedBy.update();

to



target.opened_by = openedBy.sys_id;


this is what I have now but its still not working:



      var openedBy = new GlideRecord("sys_user");

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


      openedBy.query();


      if (openedBy.next()) {


    var setOpenedBy = cart.getCart();
    target.opened_by = openedBy.sys_id;
    setOpenedBy.update();

      }