Create request from Business Rule

alhicks
Tera Guru

We're trying to create a request when a user is Terminated and the user is the Assigned To on any Active computers.   I'm having trouble query active computers and assigning the requested for as the actual terminated user.   I only want the business rule to open a request if the user was terminated and have active ci's assigned to them.  

Business Rule on sys_user table.

After Update

Conditions: current.u_status.changesTo('T')

Script:

var user = current.sys_id;

var compRec = new GlideRecord("cmdb_ci_computer");

compRec.addQuery('assigned_to',user);

compRec.addQuery('install_status', '!=', 7);

compRec.query();

if(compRec.next()) {

}

CartFunction();

function CartFunction() {

    gs.include('Cart');

    var cart = new Cart();

    var item = cart.addItem('440f9f7c0a0a3c4900a93fcfd4d82084');

        cart.setVariable(item,'contact_type',"Automation");

    cart.update();

    var cartGR = cart.getCart();

    cartGR.requested_for = current.user.setDisplayValue();

    cartGR.update();

    var rc = cart.placeOrder();

  }

1 ACCEPTED SOLUTION

ccajohnson
Kilo Sage

Looking at your script, there are a few adjustments that you can try.


1.   The function is not being called after the if condition.


2.   The cartGR.requested_for value should be the sys_id of the user from the record, not the Display value.



Here is the your script with the adjustments made:



var user = current.sys_id;


var compRec = new GlideRecord("cmdb_ci_computer");


compRec.addQuery('assigned_to',user);


compRec.addQuery('install_status', '!=', 7);


compRec.query();


if(compRec.next()) {


      CartFunction();


}



function CartFunction() {


      gs.include('Cart');


      var cart = new Cart();


      var item = cart.addItem('440f9f7c0a0a3c4900a93fcfd4d82084');


      cart.setVariable(item,'contact_type',"Automation");


      cart.update();


      var cartGR = cart.getCart();


      cartGR.requested_for = user;


      cartGR.update();


      var rc = cart.placeOrder();


}



Let me know if you are successful or not.


View solution in original post

7 REPLIES 7

ccajohnson
Kilo Sage

Looking at your script, there are a few adjustments that you can try.


1.   The function is not being called after the if condition.


2.   The cartGR.requested_for value should be the sys_id of the user from the record, not the Display value.



Here is the your script with the adjustments made:



var user = current.sys_id;


var compRec = new GlideRecord("cmdb_ci_computer");


compRec.addQuery('assigned_to',user);


compRec.addQuery('install_status', '!=', 7);


compRec.query();


if(compRec.next()) {


      CartFunction();


}



function CartFunction() {


      gs.include('Cart');


      var cart = new Cart();


      var item = cart.addItem('440f9f7c0a0a3c4900a93fcfd4d82084');


      cart.setVariable(item,'contact_type',"Automation");


      cart.update();


      var cartGR = cart.getCart();


      cartGR.requested_for = user;


      cartGR.update();


      var rc = cart.placeOrder();


}



Let me know if you are successful or not.


Ah, that was it...Thank you so much..:)


Hi Chris,



I know this is old thread and closed but would like to ask a question regarding setting cartGR values while the cartGR.requested_for works fine I am trying to set the cartGR.requetsed.date to gs.now() but that doesn't seem to be working, would you know why that is. infact of the 3 values I am trying to set I managed to only get requested_for working but none other



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


createRequest();


function createRequest() {


  //Create Request


  var cart = new Cart();


  var item = cart.addItem('1e989225db1947003f57ffa9bf9619a8'); //sysID of catalog Item



  //Set Variables in Cart Item


  cart.setVariable(item, 'first_name',current.first_name);


  cart.setVariable(item, 'last_name', current.last_name);


  cart.setVariable(item, 'start_date', current.u_sap_start_date);


  cart.setVariable(item, 'end_date', current.u_sap_end_date);


  cart.setVariable(item, 'sap_id',current.u_sap_id);


  cart.setVariable(item, 'user_type', ' permanent');


  if(current.u_returning_user == 'true')


  {


  cart.setVariable(item, 'returning_employee', 'Yes');


  }


  else


  {


  cart.setVariable(item, 'returning_employee', 'No');


  }


  cart.setVariable(item, 'location', current.location);


  cart.update();



  var cartGR = cart.getCart();


  cartGR.requested_date = gs.now();


  cartGR.requested_for = 'd52240e80f8e6600308cf88ce1050e6f'; //SysID of User in servicenow


  cartGR.alternative_contact = 'acada240acb7300060e946e6a5d4c289';


  cartGR.update();



  var rc = cart.placeOrder();


  gs.addInfoMessage('Request Created: ' + rc.number);


}


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


It is unclear what you are trying to accomplish. The cartGR.requested_for assignment will set the Requested for [requested_for] field on the cart record. The other fields are not on the Shopping Cart [sc_cart] table, therefore those fields will not be set.