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

No problem.



One hint about the Cart API: "cart.loadCart" copies all the loaded cart items into the cart opened at the "cart = new SPCart()" bit.


(SPCart() is a little more usable than Cart())



That means if the page is refreshed, the cart ends up with duplicates.



And if you try to "clearcart()" before loading the cart on the cart widget, it seems to cycle through a few times and you end up with an empty current cart.



Super annoying.



Happy hacking!


And I think the key to the issue is the line:



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



I can tell retrospectively by the way my commenting is in all-caps. hehe.


kostyakozachuk
Tera Expert

Thanks! You saved my mind 🙂