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

Paul35
ServiceNow Employee
ServiceNow Employee

Good pick up! thanks Michael.

This resolves the number issue and shouldn't have an impact on any out of box scripts. However it is always best to test in a non-production environment as a precaution.

 

No prob! And yes absolutely, always good to test in non-prod. 

Just wanted to reply and confirm that this is working swimmingly for us.  ServiceNow Support also recommended the same solution.  Thank you so much!

Awesome! Glad to hear that it's working for you. You're welcome. 

Sorry to piggyback on this super old thread... Any of guys ever tried to set a multiple choice variable in the inbound email action? I'm trying to change the variable according to the name of the subject. Any help is appreciated.

 

(Please excuse any craziness or weird logs - i've been trying this for a little while.)

 

I've also tried removing the `IF` and `IF ELSE` statements and just straight up setting the variable, and it has just straight up stopped generating a record at all.

 

createRequest();
		gs.log('Whee: This is after the function is called.');

function createRequest() {
	var cartId = GlideGuid.generate(null);
	var cart = new Cart();
	var item = cart.addItem('e5f5fe0bdbb87300796e76740f96190c');
	var subject = sys_email.subject;
	var shortd = sys_email.subject;
	var setdesc = sys_email.body_text;
	var usern = sys_email.user_id;
			gs.log('Whee: We are inside the function. Subject: ' + subject + ' || Short Description' + shortd + ' || User ID: ' + usern);

	var sid = gs.createUser(email.from);
		
		cart.setVariable(item, 'user_name', usern);
		cart.setVariable(item, 'u_DSSLDAP_short_description', shortd);
		cart.setVariable(item, 'u_DSSLDAP_description', setdesc);
		gs.log('Whee: Variables were set. ||');

	if(subject.indexOf("Employee Added") > -1){
       cart.setVariable(item, 'u_DSSLDAP_type', 'Added');
		
			gs.log('Whee what is DSS LDAP type? ' + u_dssldap_type);
		}

	else if(subject.indexOf("Employee Updated") > -1){
       cart.setVariable(item, 'u_DSSLDAP_type', 'Updated');
		
			gs.log('Whee what is DSS LDAP type? ' + u_dssldap_type);
		}

	else if(subject.indexOf("Employee Terminated") > -1){
       cart.setVariable(item, 'u_DSSLDAP_type', 'Terminated');
		
			gs.log('Whee what is DSS LDAP type? ' + u_dssldap_type);
		}
			gs.log('Whee what is DSS LDAP type? ' + u_dssldap_type);

	
		var rc = cart.placeOrder();
			gs.log('placeOrder log wheeeee');
	
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();             

}
		var ritm = new GlideRecord('sc_req_item');
		ritm.get('request', rc.sys_id.toString());
	
			gs.log('WHEE FIRST: Is it a valid record? ' + ritm.isValidRecord());
	
		var em = new GlideRecord('sys_email');
		em.get('uid', email.uid);
	
			gs.log('WHEE SECOND: Is it a valid record? ' + em.isValidRecord());
	
		GlideSysAttachment.copy('sys_email', em.sys_id.toString(), ritm.sys_class_name, ritm.sys_id.toString());
	
		gs.log('WHEE Email Sys Id : ' + em.sys_id.toString(), 'email');
		gs.log('WHEE Current Sys Id : ' + current.sys_id.toString(), 'email');
		gs.log('WHEE RC Sys Id : ' + rc.sys_id.toString(), 'email');
		gs.log('WHEE RC Class Name : ' + rc.sys_class_name, 'email');
		
	gs.addInfoMessage(rc.number);	
	}
	
	//event.state = "stop_processing";