Setting RITM fields using Cart API

javis
Giga Expert

Hi all,

I was wondering if there is a way to populate fields (not variables) on RITM forms? Everything I've read talks about populating variables but I'd like to have the fields set as well based on what users send to SN via emails. Any help or suggestions is greatly appreciated. Thanks again,

22 REPLIES 22

Abhinay Erra
Giga Sage

Here you go




var cat_id='';


if(email.body.item != undefined && email.body.item!='') {


  var cat_item=email.body.item.trim();


  var gr= new GlideRecord('sc_cat_item');


  gr.get('name',cat_item);


  cat_id=gr.getValue('sys_id');



  var cart = new Cart();


  var item=cart.addItem(cat_id);


  var user = new GlideRecord('sys_user');


  if(user.get('email',email.from.toString())){


  // set requested for


  //Set Variables in your Cart Item


  cart.setVariable(item, 'requested_for', user.getValue('sys_id'));


  cart.setVariable(item, 'request_short_description', email.subject.toString());


  cart.setVariable(item, 'requested_for_department', user.department);


  cart.setVariable(item, 'requested_for_username', user.user_name);


  cart.setVariable(item, 'requested_for_phone', user.phone);


  cart.setVariable(item, 'requested_for_budgetcode', user.cost_center);


  cart.setVariable(item, 'requested_for_company', user.company);


  }


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


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


  var rc = cart.placeOrder();


  var rc = cart.placeOrder();


  var ritm= new GlideRecord('sc_req_item');


  ritm.addQuery('request',rc.sys_id);


  ritm.query();


  while(ritm.next()){


  ritm.short_description=email.subject;


  ritm.<field name>=<value>;


  //set your fields here


  ritm.update();


  }


}


}


Abhinay, if I had several fields to set in the RITM form would I have to hard code all the fields in the inbound action or is there a way I could use 'columns' and 'element' to populate reference fields faster in the RITM?


It depends what are the fields you want to populate?


This is how we are currently setting fields in an inbound action but the api doesn't work with this.



var columns = new GlideRecord('sys_dictionary');


  //Find all columns from the 'Task' and 'RITM' table


  columns.addQuery('nameINtask,sc_req_item');


  columns.query();


   


    //Get the item user in order to check if they have the ITIL role


    var curUser = gs.getUser();


   


    while(columns.next()){


  //Element variable to compare against


  var element = [columns.element].toString();



//Check to see if the User has ITIL role and the element isn't blacklisted or if the User doesn't have the ITIL role and the element is whitelisted


      if ((!checkB && curUser.hasRole('itil'))||(checkW && !curUser.hasRole('itil'))){



  // Set the SLA Type to 'Due date' if the SLA Due date is explicitly set in the email


  if ( (typeof email.body.due_date   != 'undefined') && curUser.hasRole('itil') ) {


  current.u_sla_type = 'due_date';


  }


  // Add day place holder to Panned Effort input in order to have standardized input for all time fields


  if ( typeof email.body.effort != 'undefined'){


  email.body[columns.element] = "0 " + email.body[columns.element] ;



  }


  // Set the Item to default to 'Other Request' if the field remains undefined. A


  if ( (email.body.current && email.body.cat_item) == 'undefined'){


  current.cat_item = 'Other Request' ;


  }


  // Set the Assigned to as the sender if undefined


  if ((typeof email.body.assigned_to == 'undefined') && (typeof email.body.assignment_group == 'undefined' ) && (curUser.hasRole('itil'))){


  current.assigned_to.setDisplayValue( email.from_sys_id ) ;


  }


  //Set the item record's element equal to the corresponding column element in the email.body object


  if ( typeof email.body[columns.element]   != 'undefined' ) {



  if ( columns.internal_type == 'reference' ){


  current[columns.element].setDisplayValue(email.body[columns.element]);


  }else{


  current[columns.element] = email.body[columns.element];


  }


  }


//Set the item record's element equal to the corresponding column label in the email.body object


  else   if ( typeof email.body[columns.column_label.toLowerCase().replace(/\s/g,"_")] != 'undefined' ) {


  if ( columns.internal_type == 'reference' ){


  current[columns.element].setDisplayValue(email.body[columns.column_label.toLowerCase().replace(/\s/g,"_")]);


  }else{


  current[columns.element] = email.body[columns.column_label.toLowerCase().replace(/\s/g,"_")];


  }


Would something like this work with the cart API or will I have to hard code all the fields into the inbound action?