The CreatorCon Call for Content is officially open! Get started here.

Automatically generate a request and request item when a new user record is created

Mike Insley
Kilo Expert

Hi,

I have a requirement whereby a user record is generated from an inbound email, and then from that user account creation a request is created with a set of sequential tasks. I've completed the first part (create a user record with an inbound action).   What would be the best way to then create the request - the nature of the request is to assign tasks to relevant support teams to create the new user's domain accounts (LDAP sync and update is already configured). To map out what i'm trying to achieve:

After new user record is created:

  1. Catalog item 'New user set up) automatically submitted to create an RITM for the new user, the 'Requested for' user being the new account (to simplify amending the requested for field value I've added a u_requested_for field to sc_req_item and intend to push the value from the catalog item Requested For field into the RITM via a Run Script in the workflow).
  2. Workflow runs to assign tasks.

I'm finding the stumbling block to be achieving the first point. I had considered running a scheduled job from an event on the sys_user table, using logic along the lines of; when a user account is created by a specific account, etc,   but cat get that to work. Any help would be greatly appreciated.

Thanks

Mike

1 ACCEPTED SOLUTION

Thanks again for your help.



I've got it working using the following (credit also to ServiceNowElite)



Table: sys_user


After Insert


Condition: Created By = <user account specified>



createRequest();



function createRequest() {


//Create cart


var cart = new Cart();


//add catalog item


var item = cart.addItem('5c75d6fedb330300b927711ebf96192d');



//set variables


cart.setVariable(item, 'u_employee_number' , current.employee_number);



var rc = cart.placeOrder();


cart.desciption = 'Mikes test cart';


gs.addInfoMessage('Request item created: ' + rc.number + ' for ' + current.employee_number + '.');


}


View solution in original post

4 REPLIES 4

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi Mike,



You can write a after insert BR to create this.



Below is a sample script to use Cart API.




createServiceRequest();




function createServiceRequest() {


var cart = new Cart();


var item = cart.addItem('af695d47314610003e55ce5fcb8deb06');



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


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


cart.setVariable(item, 'sr_cmdb_ci', current.cmdb_ci);


cart.setVariable(item, 'sr_resolver_group', current.assignment_group);



var cartGR = cart.getCart();


cartGR.requested_for = current.u_affected_person;


cartGR.update();


var newSerReq = cart.placeOrder();


newSerReq.opened_by = current.caller_id;


newSerReq.update();




var activityLogMsg = 'Your Incident ' + current.number + ' has been cancelled and a Service Request ' + newSerReq.number + ' is created. No action is needed on your part';


current.state = 5;


current.active = false;


current.work_notes = activityLogMsg;


current.parent = getRITM(newSerReq.sys_id);


current.update();



var disMessage = 'Incident ' + current.number + ' has been cancelled and your work will be captured via the newly created Service Request ' + newSerReq.number;


gs.addInfoMessage(disMessage);


action.setRedirectURL(newSerReq);


action.setReturnURL(current);


}




function getRITM(serReq) {


var ritm = '';


var grRITM = new GlideRecord('sc_req_item');


grRITM.addQuery('request',serReq);


grRITM.query();


if(grRITM.next()) {


ritm = grRITM.sys_id;


}


return ritm;


}



Thanks


Hi Ashutosh



Thanks for replying, this is really helpful.



I'll need to run this against the sys_user table initially (I think) as it's when to run condition would need to be something like : After insert, and if Created By = <a user>.



So, I've created an After Insert BR on the sys_user table with a condition of Created By = Admin as follows



createServiceRequest();



function createServiceRequest() {


var cart = newCart();


var item = cart.additem('5c75d6fedb330300b927711ebf96192d');



//copy user details to item


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


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


cart.setVariable(item, 'u_employee_number' , current.employee_number);


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


cart.setVariable(item, 'u_department' , current.department);


cart.setVariable(item, 'u_job_title' , current.title);



var cartGR = cart.getCart();


cartGR.requested_for = current.name;


cartGR.update();



var newSerReq = cart.placeOrder();


newSerReq.opened_by = 'system';


newSerReq.update();



}



function getRITM(serReq) {


var ritm = '';


var grRITM = new GlideRecord ('sc_req_item');


grRITM.addQuery('request',serReq);


grRITM.query();


if(grRITM.next()) {


ritm=grRITM.sys_id;


}


return ritm;


}



I'm missing something, although I'm not sure what, to make it work. Any suggestions would be appreciated (sorry, my javascript knowledge is still pretty rudimentary)


Thanks again for your help.



I've got it working using the following (credit also to ServiceNowElite)



Table: sys_user


After Insert


Condition: Created By = <user account specified>



createRequest();



function createRequest() {


//Create cart


var cart = new Cart();


//add catalog item


var item = cart.addItem('5c75d6fedb330300b927711ebf96192d');



//set variables


cart.setVariable(item, 'u_employee_number' , current.employee_number);



var rc = cart.placeOrder();


cart.desciption = 'Mikes test cart';


gs.addInfoMessage('Request item created: ' + rc.number + ' for ' + current.employee_number + '.');


}


Hi All,

I have one query here, 

 

How to get the newly created Request sys_id.

 

I have tried the below code,

 

var newSerReq = cart.placeOrder();

gs.log('newSerReq ' +newSerReq);

 

 

But I am getting '[object GlideRecord]' in log.