Create Catalog task

Michael51
Tera Guru

Hello All, 

I have an requirement to create an catalog task and associate to Existing RITM , So can I please have some sample script to develop this

So in the payload what details do they need to send etc 

@jaheerhattiwale 

2 ACCEPTED SOLUTIONS

@Michael51 

update as this

Ensure you link that sc_task with that RITM and REQ

Also I assume 3rd party will send data like this

{

"request_item":"RITM001"

}

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

	var ritmNumber;
	var payload = request.body.dataString;
	if (JSUtil.isEmpty(payload))
		return new sn_ws_err.BadRequestError("No data provided");
	
	var parsedData = JSON.parse(payload);
	
	ritmNumber = parsedData.request_item;

	var ritm = new GlideRecord('sc_req_item');
	ritm.addQuery('number','ritmNumber');
	ritm.query();
	if(ritm.next()){
		var gr = new GlideRecord('sc_task');
		gr.request_item = ritm.getUniqueValue();
		gr.request = ritm.request;
		gr.initialize();
		var taskId = gr.insert();

		// Return the created SC Task details in the response
		var responseBody = {
			number: gr.number.getDisplayValue(),
		};
		response.setStatus(201); // 201 - Created
		response.setBody(responseBody);
	}
	
})(request, response);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@Michael51 Use updated code below to add the assignment group as well

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
 
 
    var payload = request.body.data;
    if (!payload) {
        return new sn_ws_err.BadRequestError("Request body is empty");
    }
 
    if (!payload.ritmNumber) {
        return new sn_ws_err.BadRequestError("Request body does not contain the RITM number");
    }
    var ritmNumber = payload.ritmNumber;
    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('number', ritmNumber);
    ritm.query();
    if (ritm.next()) {
 
        var gr = new GlideRecord('sc_task');
        gr.initialize();
        gr.parent = ritm.sys_id.toString();
gr.request_item = ritm.sys_id.toString();
gr.assignment_group = payload.assignment_group;
        gr.short_description = "New catalog task";
        gr.insert();
 
        // Return the created SC Task details in the response
        var responseBody = {
 
            number: gr.number
 
        };
    }
    response.setStatus(201); // 201 - Created
    response.setBody(responseBody);
})(request, response);
 
Payload should be:
{"ritmNumber": "RITM0010049", "assignment_group":"<GROUP SYS ID HERE>"}
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

20 REPLIES 20

Hello @jaheerhattiwale ,

Am getting following error while Testing the API

 

 

{
    "error": {
        "message""Requested URI does not represent any resource",
        "detail"null
    },
    "status""failure"
}
 
can you please take a look

@Michael51 You are trying it from postman? The scripted rest resource url (end point) you use used is not correct i think. please check.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

@jaheerhattiwale , same works in Rest API but not here in postman

@Michael51 Send screenshot of scripted rest resource record and postman?

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

@jaheerhattiwale , here it is 

Michael51_0-1685710147502.png

Michael51_1-1685710401457.png