Cart controls - want to delete cart items upon submission

kristenankeny
Tera Guru

I'm trying to figure out how to clear certain items from a cart. This is the background:

We have created a way for people to fill in catalog items as they would need to be populated for new hires. When they are doing this, we remove all of the fields that are particular to a "requested for" because they are filling them in to save and be used later. When they submit those items, the requested items are moved from the Request under which they are created under the Position bundle the person started from.

The issue is that I could start filling in forms for a position bundle and then decide to go elsewhere in ServiceNow without submitting my cart. If i go into the regular service catalog to fill in a different form, those items I filled in are still in my cart. Since they have no "requested for" information, I don't want them to create requested items. I was thinking that upon submission, I could evaluate the cart item (it will have a "hint" with a request parent reference, which I could look up in the position bundle table to see if I find a match) and delete it rather than creating a requested item, then use addInfoMessage() to notify the user as to why they don't see those other items.

How would I accomplish this? Or, is there a better way of clearing their cart if they don't submit the items intended for the position bundle?

1 ACCEPTED SOLUTION

kristenankeny
Tera Guru

I believe I've found a solution for our need. instead of doing any controls from the cart, in my workflow, I added the following run script at the beginning (this is the sc_request workflow):



var tot = 0;


var bun = 0;


var bunItems = [];


var bunNames = [];


var r = new GlideRecord('sc_req_item');


r.addQuery('request',current.sys_id);


r.query();


while(r.next()){


tot += 1;


if(r.variables.requested_for == ''){


bun += 1;


bunItems.push(r.sys_id + '');


bunNames.push(r.cat_item.name + '');


}


}


if (tot > bun && bun != 0) {


var mOrRm = '';


if(current.parent.state == 1){


mOrRm = 'move';


}


else{


mOrRm = 'remove';


}


var moveItems = new GlideRecord('sc_req_item');


moveItems.addQuery('sys_id','IN',bunItems);


moveItems.query();


while(moveItems.next()){


if(mOrRm == 'move'){


moveItems.request = current.parent;


moveItems.parent = current.parent;


moveItems.u_partitions = 'position_bundle';


moveItems.order_guide = '';


moveItems.update();


}


else if(mOrRm == 'remove'){


moveItems.deleteRecord();


}


}


var bundleName = current.parent.u_position_bundle_name;


current.parent = '';


if(mOrRm == 'move'){


gs.addInfoMessage('We moved ' + bun + ' item(s) (' + bunNames.toString() + ') to your position bundle ' + bundleName + '.');


}


else if(mOrRm == 'remove'){


gs.addInfoMessage('We removed ' + bun + ' item(s) (' + bunNames.toString() + ') from your order because they were intended for ' + bundleName + ' which is no longer in draft state.' );


}



}



This allows them to submit, they don't lose any work, and it keeps the requested items in their correct spot.


View solution in original post

3 REPLIES 3

Anurag Tripathi
Mega Patron
Mega Patron

Hi Kristen,



I think this will help you



Delete request items


-Anurag

Thanks for the suggestion, but I want to automatically delete items from the cart that shouldn't be there, either when they navigate to the regular catalog or when they submit requests from the regular catalog. I can identify the cart items that need to be deleted because they have a hint that includes the sys_id of a "position bundle". I'm just not sure where to add script to trigger the deletion of those items.


kristenankeny
Tera Guru

I believe I've found a solution for our need. instead of doing any controls from the cart, in my workflow, I added the following run script at the beginning (this is the sc_request workflow):



var tot = 0;


var bun = 0;


var bunItems = [];


var bunNames = [];


var r = new GlideRecord('sc_req_item');


r.addQuery('request',current.sys_id);


r.query();


while(r.next()){


tot += 1;


if(r.variables.requested_for == ''){


bun += 1;


bunItems.push(r.sys_id + '');


bunNames.push(r.cat_item.name + '');


}


}


if (tot > bun && bun != 0) {


var mOrRm = '';


if(current.parent.state == 1){


mOrRm = 'move';


}


else{


mOrRm = 'remove';


}


var moveItems = new GlideRecord('sc_req_item');


moveItems.addQuery('sys_id','IN',bunItems);


moveItems.query();


while(moveItems.next()){


if(mOrRm == 'move'){


moveItems.request = current.parent;


moveItems.parent = current.parent;


moveItems.u_partitions = 'position_bundle';


moveItems.order_guide = '';


moveItems.update();


}


else if(mOrRm == 'remove'){


moveItems.deleteRecord();


}


}


var bundleName = current.parent.u_position_bundle_name;


current.parent = '';


if(mOrRm == 'move'){


gs.addInfoMessage('We moved ' + bun + ' item(s) (' + bunNames.toString() + ') to your position bundle ' + bundleName + '.');


}


else if(mOrRm == 'remove'){


gs.addInfoMessage('We removed ' + bun + ' item(s) (' + bunNames.toString() + ') from your order because they were intended for ' + bundleName + ' which is no longer in draft state.' );


}



}



This allows them to submit, they don't lose any work, and it keeps the requested items in their correct spot.