Frustration with Cart/SPCart API placing the order and retaining the cart's Requested_For

daveslater
Tera Contributor

Hi All,

I have trawled through many forums, dev community posts, snguru blog posts, etc. etc. etc. However, I am still left with an 'unworkroundable' issue with placing orders via the cart API.

The Aim:
To automate the Leavers Process to raise a request after assembling a cart with request items, creating tasks for different teams to complete.

The Method:

  1. A Business Rule looks at the sys_user table, listening out for the "source" field to change and include a certain string. (This works perfectly)
  2. The BR then takes the current record's sys_id as the "user" and the current.manager as values to populate the cart's requested_for and user fields respectively. (This too works perfectly)
  3. The BR then adds the necessary catalog items into the raised cart and sets any necessary variables. (Perfect)
  4. The BR then places the order creating a Request. - at this point the Requested_For and User fields of the cart should populate the Requested_For and Opened_By of the request, but both are completely ignored and overwritten with my own details, me being the one who updated the sys_user table's source field manually.

The Failure:

I have tried many different approaches, ones laid out in various blog posts and issue reports to force these values to be correct, here are two of the best contenders:

  1. var leaverProcess = cart.placeOrder();
    leaverProcess.opened_by = userLM;
    leaverProcess.requested_for = userLeaver;

    This does nothing at all.

  2. var reqDeetSetOK = setReqDeetsNowLike();

    function setReqDeetsNowLike() {
    var rg = new GlideRecord('sc_request');
    if(!rg.get(reqsys_id)){return false;}   rg.addQuery('number', reqnumber);
    rg.query();
    rg.requested_for = userLeaver;
    rg.opened_by = userLM;
    rg.short_description = 'JML: Leaver - ' + userFullname;
    rg.update();
    return true;

              }
                  // log whether or not this worked.

                  switch (reqDeetSetOK) {

                  case false:

                  gs.log('JML Leaver process for ' + userFullname + ' - Could not find request ' + reqnumber + ' to update' );

                  break;

                  case true:

                  gs.log('JML Leaver process for ' + userFullname + ' - Updated request ' + reqnumber + ' with correct details');

                  }

                  This logs as if it has worked. I check the request and get the attached development-lollocaust:

                                      lolololol.jpg

The requests workflow is triggered by the Short Description being set correctly, and it does actually trigger even though it's actually still blank !!!! Only, when it gets to the end it uses all the wrong details (the the email sent on creation too).

I've seen many people raise similar issues, and all fixed by using methods I have already tried.

What is going on?!

1 ACCEPTED SOLUTION

I honestly dunno what I did exactly, but after very nearly losing my mind several times over these last few months, this seems to work as needed:



This is an AFTER Business Rule on the sys_user table, running on update, looking for a "JML leaver" option to change to TRUE.



// Collect leaver details as variables:


var userLeaver = current.sys_id;


var userFullname = current.name;


var userLM = current.manager;




// generate a cart to add the request items into


var cartId = 'JML: Leaver - ' + userFullname;


var cart = new SPCart(cartId, userLM);


var jmlCart = cart.getCart(cartId, current).sys_id;




cart.setRequestedFor(userLeaver); // NEVER FORGET THIS USAGE!




// now add the request items


// var leavers_req = cart.addItem('..... this goes on for a bit so ill leave it out as its not necessary




// log cart ID


current.u_jml_leaver_cart = jmlCart;


current.update();



gs.log('JML Leaver Cart created for ' + userFullname + ' as ' + jmlCart);



// place order and raise Request


var leaverProcess = cart.placeOrder();


var reqnumber = leaverProcess.getValue("number");


var reqsys_id = leaverProcess.getUniqueValue();



gs.addInfoMessage('JML: Leavers Request raised: ' + reqnumber);



// open Request and set details correctly


var reqDeetSetOK = setReqDeetsNowLike();



function setReqDeetsNowLike() {


var rg = new GlideRecord('sc_request');



if(!rg.get(reqsys_id)){return false;}


rg.get(reqsys_id);


rg.short_description = cartId;


rg.update();


return true;


}



// then there's a message for value true and message for value false. so ill leave it out.




That Cart API is a nightmare. I've had to rewrite the front end for our new joiners this from scratch to avoid using the cart API at all until ready to place order.



Hope this helps!


View solution in original post

7 REPLIES 7

daveslater
Tera Contributor

For clarity:



I am using Helsinki with latest available patches installed.
Had the same problem with the New Joiner Process but could work around it by asking their line manager to manually set the requested_for field on the cart - this works fine but can't ask their lm to get involved at this stage for a leaver.


Dave - I have the same issue. Where you ever successful in dynamically setting the requested_for value in the cart and requested item with a value other than yourself/logged in user?


Thank you,


Kathy


I honestly dunno what I did exactly, but after very nearly losing my mind several times over these last few months, this seems to work as needed:



This is an AFTER Business Rule on the sys_user table, running on update, looking for a "JML leaver" option to change to TRUE.



// Collect leaver details as variables:


var userLeaver = current.sys_id;


var userFullname = current.name;


var userLM = current.manager;




// generate a cart to add the request items into


var cartId = 'JML: Leaver - ' + userFullname;


var cart = new SPCart(cartId, userLM);


var jmlCart = cart.getCart(cartId, current).sys_id;




cart.setRequestedFor(userLeaver); // NEVER FORGET THIS USAGE!




// now add the request items


// var leavers_req = cart.addItem('..... this goes on for a bit so ill leave it out as its not necessary




// log cart ID


current.u_jml_leaver_cart = jmlCart;


current.update();



gs.log('JML Leaver Cart created for ' + userFullname + ' as ' + jmlCart);



// place order and raise Request


var leaverProcess = cart.placeOrder();


var reqnumber = leaverProcess.getValue("number");


var reqsys_id = leaverProcess.getUniqueValue();



gs.addInfoMessage('JML: Leavers Request raised: ' + reqnumber);



// open Request and set details correctly


var reqDeetSetOK = setReqDeetsNowLike();



function setReqDeetsNowLike() {


var rg = new GlideRecord('sc_request');



if(!rg.get(reqsys_id)){return false;}


rg.get(reqsys_id);


rg.short_description = cartId;


rg.update();


return true;


}



// then there's a message for value true and message for value false. so ill leave it out.




That Cart API is a nightmare. I've had to rewrite the front end for our new joiners this from scratch to avoid using the cart API at all until ready to place order.



Hope this helps!


Thanks Dave for quick response, and for sharing. Much appreciated. So much for out of the box solutions, heh? Guess I'll keep hacking away at it.



Kathy Chisefsky