Create Service Request from Scheduled Job

Wade Clairmont
Tera Guru

I have a scheduled job that will run every morning 6 am to review current contractor profiles, and if they are expired, auto generate a service request for termination.

Everything works fine, just a question....   When I generate the service request using the following:

var cart = new Cart();

var item = cart.addItem("b151029edb110f00aa103c8f9d9619fd"); // System Termination

cart.setVariable(item, "sys_requested_for", usr.name);

cart.setVariable(item, "sys_delivery_date", gs.now());

cart.setVariable(item, "sys_comments", 'System generated termination request for expired contractor account.');

cart.placeOrder();

It generates the request fine, but only populates the RITM variables.   Is there a way to populate the Request fields, specifically the "requested for" field?

term.png

In order to follow the correct approval process, I need to be able to get this set to the same "requested for" as the RITM field.

Thanks in advance.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

You can get the 'sys_id' of the order created and use that to query the 'sc_request' table to update those details:



var cart = new Cart();


var item = cart.addItem("b151029edb110f00aa103c8f9d9619fd"); // System Termination


cart.setVariable(item, "sys_requested_for", usr.name);


cart.setVariable(item, "sys_delivery_date", gs.now());


cart.setVariable(item, "sys_comments", 'System generated termination request for expired contractor account.');


var placeOrder = cart.placeOrder();



var request = placeOrder.getValue('sys_id');



Use the 'request' value to do your query.



Hope that helps!



Cheers,



Tim


View solution in original post

12 REPLIES 12

Here is the code I used.  Now I used a range in days, so it didn't matter when the contractor account was created during the day.

//check all Contractor records for contract dates, and if they are active contractors //and expiring, send out notification to Manager EXCEPT if they are JV locations,     //then remove contract dates as they are handled in quarterly reports through //auditing

var usr = new GlideRecord('sys_user');
usr.addQuery('active', true);
usr.addQuery('u_test_account', false);
usr.addNullQuery('u_hr_status');
usr.addNotNullQuery('u_contract_end_date');
usr.query();
while(usr.next()) {
if(usr.location.name == '<location1>'){
usr.u_contract_start_date = '';
usr.u_contract_end_date = '';
usr.update();
}
else {
var diff = gs.dateDiff(gs.now(), usr.u_contract_end_date, true);
var days = diff/86400; 
if(days == 28) {
gs.eventQueue("contractor.expiry28.reminder", usr, usr.manager.email);
}
...

else if (days == 7) {
gs.eventQueue("contractor.expiry7.reminder", usr, usr.manager.email);
}
else if (days == 0) {
gs.log('**** CONTRACTOR EXPIRED (0) DAYS REMAINING **** ' + usr.name);
gs.eventQueue("contractor.expired.reminder", usr, usr.manager.email);
}

// if expired + 1 day, generate termination request for contractor and AUTO APPROVE (contained in workflow).
else if (days == -1) {
gs.eventQueue("contractor.expired.reminder", usr, "<email>");
var cart = new Cart();
var item = cart.addItem("b151029edb110f00aa103c8f9d9619fd"); // SysTermination
cart.setVariable(item, "emp_name", usr.sys_id);
cart.setVariable(item, "sys_delivery_date", gs.now());
cart.setVariable(item, "sys_comments", 'System generated termination request for expired contractor account.');
var placeOrder = cart.placeOrder();
}
}
}

Rohit_Burud
Tera Contributor

Thank you Wade.

 

utkarsh6
Giga Contributor

Hi Wade,

Could you please share the steps how did you get it done. I have written this code and also done the required changes in my schedule job but not able to raise a request automatically.

I have created a schedule job (Automatically run a script of your choosing) and write this code in the script but unable to generate a request.

Can you please suggest or help.

Thanks/Utkarsh