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

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


Right! But I have a similar script (like the one you posted),   but on my instance it does not update the requested_for field on the sc_cart table. At least with your test I know that there might another script affecting the change. I even tried with the same exact script you posted and the requested for is still not updating. Alwas defaults back to the current login user as the requested for.


Did you check if the value also changed on the "sc_cart" table instead of the forms? I am still not able to accomplish this.


Yes, the field definitely changed on the sc_cart table.


I have made it work but only after I have set the value "sc_cart_view_requested_for" on the "custom_cart" (Cart) field in the executing catalog item. Is this the same value you have there? It seems I have to change this value on all the catalog items but I lost the order now buttons.