Cart API / Service Catalog Script API

abelangulo
Kilo Contributor

Hello,

I'm pretty new to the SN world, and I have an issue I want to see if you guys can help me with.

I created the Inbound Email Action (shown below) for our On-Boarding process, but

1) Sometimes it works, sometimes it doesn't (giving only the following info: "Skipping 'Create OnBoarding Request', did not create or update sc_req_item"). I haven't been able to pin point how, when, or why...

2) When it does work, it doesn't populate any of the variables (values hard-coded for now), or at least they don't show in the item created.

Any help would be really appreciated.

Thank you.

=======================================================

Target Table: Requested Item [sc_req_item]

 

=======================================================

gs.include('Cart');

createRequest();

function createRequest() {

      var cart = new Cart();

      // add in cart, substitute your cat item sys_id

      var item = cart.addItem('ec2f659a6fa29100bbc6dd1cbb3ee4f6'); // On-boarding

      //var item = cart.addItem('ea77984a6f629100bbc6dd1cbb3ee452'); // Configuration Auths for testing purposes  

      // set requested for, substitute your requested for

      //Set Variables in your Cart Item

      // REQUESTOR INFORMATION

      cart.setVariable(item, 'type', 'New Employee');

      cart.setVariable(item, 'title', 'Any Title');

      cart.setVariable(item, 'email', 'Abel.Angulo@avmed.org');

      cart.setVariable(item, 'department', 'IS');

      cart.setVariable(item, 'phone', '305-671-0226');

      //gs.addInfoMessage("Requestor finish");

      //EMPLOYEE INFORMATION

      cart.setVariable(item, 'full_name', 'Pepito Perez'); //email.body.name);

      cart.setVariable(item, 'emp_title', 'Empl Title'); //email.body.title);

      cart.setVariable(item, 'emp_department', 'IS'); //email.body.department);

      cart.setVariable(item, 'emp_location', 'Miami'); //email.body.location);

      cart.setVariable(item, 'eff_date', '2014-05-01'); //email.body.start_date);

      //gs.addInfoMessage("Employee finish");

      //HARDWARE REQUIREMENTS

      cart.setVariable(item, 'service_desk_system', 'Desktop');

      var cartmsg = "Received from: " + email.origemail + "\n\n" + email.body_text;

      cart.setVariable(item, 'description', cartmsg);

      var rc = cart.placeOrder();

      //gs.addInfoMessage(rc.number);

}

26 REPLIES 26

cloke
Kilo Contributor

Hi Abel,



I know I'm probably late to the game on this, but I just had the EXACT same problem as you, and I was able to fix this by setting the Target Table to Requested Item (like you have it) and making sure to add a current.update(); RIGHT AFTER the var rc = cart.placeOrder(); line at the end. I found doing this created the request record. Apparently the cart API was constructing all the data, but it just needed pointed in the right direction? Either way, it worked for me. Let me know if this helps you!



Regards,


Bryce


Hi Bryce,



I have used sc_req_item table and current.update() as suggested. But   I can see that 2 requested items are getting created.   Any idea?


Hello,



You may have to do only one of the following but don't do both. Both of these can create a record in "Requested Item" table, so you will end up with 2 new records instead of one.



1. Cart.placeOrder();


2. current.update();



Target table name is set in "Inbound Email Actions" just to make current object refer to this table. You will have to submit/insert your current object (use current.update() function since the current object is already initialized by ServiceNow) to make sure your record is submitted.



However if you are submitting to "Requested Item" table, then it is better to go for Cart object and use its function placeOrder() as both "Request" and "Requested Item" records are generated. In earlier approach, "Request" record will not be created.



In both the above approaches, you will have to make sure you are assigning data to your fields and variables (if any) before you create the records. I Hope this helps. Let me know if you have any questions.



Kind Regards,


Mohammad Nayeem


Paul35
ServiceNow Employee
ServiceNow Employee

Hi All,



You will find that when using only "Cart.placeOrder();" in an inbound action it will return with a "skipped" status. When the cart API is called it starts the workflow process for requesting an item. This workflow process is outside of the inbound action where it creates the request, request item and tasks etc.



Inbound actions are recognized as "processed" if there has been a change to the "current" object in the target table. As the workflow is outside of the inbound action (not changing the current object), the inbound action doesn't recognize the request and thinks that there has been no update to the target table. This then returns the log entry: "Skipping '<inbound action name>', did not create or update <target table>".



If you add "current.update()" to the inbound action, it will change the status of the inbound action to "processed" but it will create an additional record in the target table. This is because current.update() creates a new record if it is unable to find a record to update.



To solve this issue, you can set the "current" object to the created cart request by using current.get() and then performing an update. This will make an update to the "current" object/record (which you set to your created request) and will change the status of the inbound action to "processed"



Example:


Target table: sc_request


// To be placed after var rc = cart.placeOrder



if(current.get('sys_id', rc.sys_id)) // If the request exists in sc_request


{        


      //   insert any fields that need updating here.                  


      current.work_notes = "This is a test";                              


      current.update();            


}



Ref: http://wiki.servicenow.com/index.php?title=GlideRecord#Get_Methods



Hope that helps!



Paul


Howdy.  I realize this thread is a bit old, but I'm seeing some behavior when I'm doing this and I wanted to inquire about it. 

If I set the inbound email action table to sc_request and use the Cart API to placeOrder(), I've noticed that two sc_request table numbers get used (eg REQ0012345 and REQ0012346), although only one sc_request record gets created.  Have you seen the same?  Seems to happen if using the Cart API, regardless of including the current.get/current.update().  If I set the table to sc_req_item and use the Cart API(), then it looks like two sc_req_item numbers get used.

Thank you!

-Chuck