Using Cart.PlaceOrder Return Object

sjohanse
Kilo Contributor

Hi,

we have a need to create a service catalog request from an inbound email and set the assignment group on the resulting requested item based on content in the email (country value pair). Everything works fine, but I cannot seem to get the requested item updated with the assignment group.

We are using the cart object and I first tried to use MyCart.SetVariable(reqItem,'assignment_group', assn_group_id) method after having confirmed that the assn_group_id contains the correct value. This method works for all other variables, but does not seem to work for assignment group as that is a column in the table and not a variable.

Thus we moved off to instead do an update of the requested item(s) after the request and item is created. Thus after we have called the PlaceOrder method of the cart-object. But we cannot seem to get that to work either. Our code now looks like this, but we have tried a myriad of variations:

var catReq = MyCart.placeOrder(); //submit the shopping cart

if(!assn_group_sid.isNil() || assn_group_sid == '') {

  // Query for the RITMs and set the assignment group

  var grReqItem = new GlideRecord('sc_req_item');

  grReqItem.AddActiveQuery();

  grReqItem.addQuery('parent',catReq.sys_id);

  grReqItem.query();

  if(grReqItem.Next()) {

    grReqItem.assignment_group = assn_group_sid;

    grReqItem.update();

  }

}

We have tried to find some info on what is returned by the placeOrder method, but as usual the documentation is very thin.

Any ideas to what we do wrong here?

Regards, Stig

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

MyCart.placeOrder(); returns a GlideRecord from the 'sc_request' table, which is the record that was created when the cart was submitted.





var catReq = MyCart.placeOrder(); //submit the shopping cart



if(!assn_group_sid.isNil() || assn_group_sid == '') {


  // Query for the RITMs and set the assignment group


  var grReqItem = new GlideRecord('sc_req_item');


  grReqItem.addActiveQuery(); //watch case here


  grReqItem.addQuery('request',catReq.getValue('sys_id')); // I use .getValue() when querying for sys_id's. Are you sure parent is the correct field?


  grReqItem.query();


  if(grReqItem.next()) { //Watch your case here!


    grReqItem.assignment_group = assn_group_sid;


    grReqItem.update();


  }


}



Also, are you sure 'parent' is the correct field?


Watch out for case sensitive functions too



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

4 REPLIES 4

The SN Nerd
Giga Sage
Giga Sage

MyCart.placeOrder(); returns a GlideRecord from the 'sc_request' table, which is the record that was created when the cart was submitted.





var catReq = MyCart.placeOrder(); //submit the shopping cart



if(!assn_group_sid.isNil() || assn_group_sid == '') {


  // Query for the RITMs and set the assignment group


  var grReqItem = new GlideRecord('sc_req_item');


  grReqItem.addActiveQuery(); //watch case here


  grReqItem.addQuery('request',catReq.getValue('sys_id')); // I use .getValue() when querying for sys_id's. Are you sure parent is the correct field?


  grReqItem.query();


  if(grReqItem.next()) { //Watch your case here!


    grReqItem.assignment_group = assn_group_sid;


    grReqItem.update();


  }


}



Also, are you sure 'parent' is the correct field?


Watch out for case sensitive functions too



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

I did try first to use the 'request', but as that did not work I tried the 'parent' field. In principle they should both have the request sys_id. Thanks for spotting the case error (auto-correct not always nice).



I will try your suggestion and update the post with the results.



Thanks for the answer.


You may be right, I just looked at my code for a similar requirement.
I just corrected line 4; correct method is "addActiveQuery()"


JavaScript is case sensitive!



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

It works!



Final script:


if(!assn_group_sid.isNil() || assn_group_sid == '') {


// Query for the RITMs and set the assignment group


var grReqItem = new GlideRecord('sc_req_item');


grReqItem.addActiveQuery();


grReqItem.addQuery('request',catReq.getValue('sys_id'));


grReqItem.query();


if(grReqItem.next()) {


grReqItem.assignment_group = assn_group_sid;


grReqItem.update();


}


}



Also allows me to set the CI on the requested item. Perfect for our purposes!



Thanks for your help!