Scripted API to Query data before record creation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2022 11:08 AM
Hi All! I have a good one today, I have a requirement to create a scripted API to create standard changes from another in house product. All was going well until I hit a snag. The data coming from the in house product will be giving me string values only, meaning I have to query those strings to get the sys_ids of each of those fields before I can create the standard change. The script below is where I have left off, the records get created but the reference fields like requested_by and company are left empty. Any advice is greatly appreciated as always many thanks!
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
this.status = '200';
var respObj = {}; //declare the response object
var payload = request.body.data; //retrieving the JSON body
var requested_by = payload.requested_by;
var company = payload.company;
var business_service = payload.business_service;
var assignment_group = payload.assignment_group;
var change_tester = payload.u_change_tester;
(function(){
var grinc = new GlideRecord("change_request");
grinc.initialize();
grinc.company = company;
grinc.requested_by = requested_by;
grinc.business_service = business_service;
grinc.assignment_group = assignment_group;
grinc.u_change_tester = change_tester;
grinc.type = 'standard';
grinc.std_change_producer_version = '3c1015d9874f911097842137cebb358f'; // Sys ID of the Standard Change Templates version
grinc.applyTemplate('PUPS Production Monthly Patching');
grinc.insert();
var RB = new GlideRecord("sys_user");
RB.addQuery('user_name', 'requested_by');
RB.query();
while(RB.next())
{
grinc.requested_by = RB;
grinc.u_change_tester = RB;
grinc.update();
}
this.status = '200';
respObj.body = {
"message": "Creation Success!",
"detail": "Change " + grinc.number + " created successfully"
};
if (this.status == '200') {
response.setBody(respObj);
response.setStatus(this.status);
return response;
} else {
var setError= new sn_ws_err.ServiceError(); //this API used to set the custom error messages
setError.setStatus(this.status);
setError.setMessage(respObj.body.message);
setError.setDetail(respObj.body.detail);
return setError;
}
}());
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2022 10:13 AM - edited ‎11-21-2022 11:02 AM
I finally got this syntax cleaned up and working like it should, thank you all for your ideas and help if I keep going with this format its working exactly how I want it to. Updating the code below with the mostly finished product.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
this.status = '200';
var respObj = {}; //declare the response object
var payload = request.body.data; //retrieving the JSON body
var requested_by = payload.requested_by;
var company = payload.company;
var business_service = payload.business_service;
var assignment_group = payload.assignment_group;
var change_tester = payload.u_change_tester;
//query change tester WIN ID and place into tester object
var tester="";
var CT = new GlideRecord("sys_user");
CT.addQuery('u_win_id', change_tester);
CT.query();
if(CT.next()){
tester=CT.sys_id;
}
//query business service with name and place into the BusinessServ Object
var BusinessServ="";
var BS = new GlideRecord('cmdb_ci_service');
BS.addQuery('name', business_service);
BS.query();
if(BS.next()){
BusinessServ=BS.sys_id;
}
//query the company record by name and place into the affcust object
var affcust="";
var cmp = new GlideRecord('core_company');
cmp.addQuery('name', company);
cmp.query();
if(cmp.next()){
affcust=cmp.sys_id;
}
// query the requested by user by WIN ID and place into the requested object
var requested="";
var RB = new GlideRecord('sys_user');
RB.addQuery('u_win_id', requested_by);
RB.query();
if(RB.next()){
requested=RB.sys_id;
}
// query the assignment group by name and place into the assignment object
var assignment="";
var AG = new GlideRecord('sys_user_group');
AG.addQuery('name', assignment_group);
AG.query();
if(AG.next()){
assignment=AG.sys_id;
}
// create standard change with each queried object above and utilize the pups standard change template
var grinc = new GlideRecord("change_request");
grinc.initialize();
grinc.company = affcust;
grinc.requested_by = requested;
grinc.business_service = BusinessServ;
grinc.assignment_group = assignment;
grinc.u_change_tester = tester;
grinc.type = 'standard';
grinc.std_change_producer_version = '3c1015d9874f911097842137cebb358f'; // Sys ID of the Standard Change Templates version
grinc.applyTemplate('PUPS Production Monthly Patching');
grinc.insert();
this.status = '200';
respObj.body = {
"message": "Creation Success!",
"detail": "Change " + grinc.number + " created successfully"
};
if (this.status == '200') {
response.setBody(respObj);
response.setStatus(this.status);
return response;
} else {
var setError= new sn_ws_err.ServiceError(); //this API used to set the custom error messages
setError.setStatus(this.status);
setError.setMessage(respObj.body.message);
setError.setDetail(respObj.body.detail);
return setError;
}
})(request, response);