Update Existing Cart Requested For

david_hreben
Giga Expert

Hello Community,

I am trying to create a Catalog Client script to update the an existing cart for the login user and change the requested for using the cart API. The requested for always gets updated with the current login user which is not the desired result. The sample below provides a   way to get the existing cart for the login user but when I use the SYS_ID to update the cart it does not work. Have anyone used "CartAjaxProcessor" to succesfully change the requested for? There is a way to do this by calling a GlideRecord script on a Client Script but want to move away from it. Thanks!

Sample Script:

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading || newValue == '')

return;

getCartId(g_user.userID)

function getCartId(userSysId) {
   
var ga =new GlideAjax('CartAjaxProcessor');// Class name
  ga
.addParam('sysparm_action','get_user_cart');// method name
  ga
.addParam('sysparm_value', userSysId);// method arg
  ga
.getXML(DisplayCart);// Callback to result
}

function DisplayCart(response){
    v
ar cartSysId = response.responseXML.documentElement.getElementsByTagName("sc_cart")[0].getAttribute("cart_sysid");
  alert
(cartSysId);
}

}

Source: Catalog client script examples

1 ACCEPTED SOLUTION

Ohhhhh, my bad, I totally didn't even see the requested_for field on the sc_cart table.


I modified the script to change that field, and then after I submit the Catalog Item, the requested_for field on the sc_request is showing as the new User.   Is that what you're after? If not, I'm still confused as to what you're hoping to accomplish by making this change.



Client Script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {  


  if (isLoading)  


          return;  


 


  g_form.hideFieldMsg('requested_for', true);  


   


  if (newValue == '')  


          return;  


 


  var ga = new GlideAjax('AjaxFunctions');  


  ga.addParam('sysparm_name', 'updateCartOwner');  


  ga.addParam('sysparm_user', g_user.userID);  


  ga.addParam('sysparm_requested_for', newValue);  


  ga.getXMLAnswer(function(answer) {  


          if (answer == 'true')  


                  g_form.showFieldMsg('requested_for', 'Updated \'Requested for\'', 'info');  


          else  


                  g_form.showFieldMsg('requested_for', 'No cart found', 'error');  


  });  


}



Script Include:


var AjaxFunctions = Class.create();


AjaxFunctions.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  updateCartOwner: function() {


          var user = this.getParameter('sysparm_user');


          var requested_for = this.getParameter('sysparm_requested_for');



          var cart = new GlideRecord('sc_cart');


          if (cart.get('user', user)) {


                  cart.requested_for = requested_for;


                  cart.update();


                  return true;


          }


          return false;


  },



  type: 'AjaxFunctions'


});



Result:


Screen Shot 2016-10-09 at 3.09.58 PM.png


View solution in original post

9 REPLIES 9

Geoffrey2
ServiceNow Employee
ServiceNow Employee

If all you want to do is specify the Requested for person, then this Cart API is not the correct way to do it.  


All you need to do is have a User Reference field on your form called requested_for (or something).   And then within your workflow, use a Run Script activity to update the field on the sc_request.



var req = new GlideRecord('sc_request');


if (req.get(current.request)) {


    req.requested_for = current.variables.requested_for;


    req.update();


}


Perhaps I did not explain it correctly but the example shown references the UI Script "CartAjaxProcessor" which gets the requested for of the currenmt login user from the "SC_CAR" table. Whenver you submit a request from the Shopping Cart, a record is created on sc_cart table and I want to change the requested for on the server from a client script.


I understood what you're trying to do - I don't understand why you're trying to do that. But not knowing what the true goal is - here is how you would change the User on a Cart from a Client Script:



You don't need CartAjaxProcessor.   We don't have access to it to see how it works internally, so you need to create your own functionality.


It is a basic GlideAjax: GlideAjax - ServiceNow Wiki



From what I understand, you want to find the Cart that belongs to the current logged in User and change it to a different User.   Which means you need to specify that User somewhere. So this code assumes you have a reference field called new_owner.



Create a Client Script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading)


          return;



  g_form.hideFieldMsg('new_owner', true);


  var newOwner = g_form.getValue('new_owner');



  if (newOwner == '')


          return;



  var ga = new GlideAjax('AjaxFunctions');


  ga.addParam('sysparm_name', 'updateCartOwner');


  ga.addParam('sysparm_current_owner', g_user.userID);


  ga.addParam('sysparm_new_owner', newOwner);


  ga.getXMLAnswer(function(answer) {


          if (answer == 'true')


                  g_form.showFieldMsg('new_owner', 'Updated Cart owner', 'info');


          else


                  g_form.showFieldMsg('new_owner', 'No cart found', 'error');


  });


}



Create a Script Include:


Name: AjaxFunctions


Client-callable: true


var AjaxFunctions = Class.create();


AjaxFunctions.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  updateCartOwner: function() {


          var currentOwner = this.getParameter('sysparm_current_owner');


          var newOwner = this.getParameter('sysparm_new_owner');



          var cart = new GlideRecord('sc_cart');


          if (cart.get('user', currentOwner)) {


                  cart.user = newOwner;


                  cart.update();


                  return true;


          }


          return false;


  },



  type: 'AjaxFunctions'


});


Hello Geoffrey,



First of all, thanks for trying. I want to change the "requested_for" field and not the user. I actually created a similar script like this one but always defaults the login user as the "requested_for" and same happens with your script. The user field I was able to change it with no problems but is actually the requested for field is the one giving me a head ache. Thanks!