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

Thanks for reverting back Chris, I have got a custom Business rule that looks for insert of user in sys_user table and at which point opens a catalog Request. The script I pasted earlier is doing what I have just mentioned, since I am going through sc_cart I notice that on the checkout page there is requested date field which I would like to move to the u_requested_date on the REQ. I have created a new u_request_date field on the sc_cart table but even then the requested_date is not getting copied to the REQ.



Checkout page:


find_real_file.png


sc_cart page:


find_real_file.png


Request:


find_real_file.png


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Andrea



A Couple of questions.


Do you have problem with the actual code ?


Do you want to create a requested item for each active CI ?



Cheers


Robo


Hello,



Christopher's script worked.   We're actually going to create one request per user.



Thank you so much for the reply...