Work notes details not reflected in incident work notes Activities using scripted rest API.

UDV Prasad
Tera Contributor

The sample payload that came to us contained the user details in the work notes. I Mentioned Payload Work Notes Details In Incident Work Notes In Scripted Rest API. But for me the incident form is reflected as work notes [object Object].

Please help me @Ankur Bawiskar .

sample payload:

{

  
"correlation_display": "",

"number": "INCXXXXXX",

"priority":"P2",

"state": "New",

"contact_type": "Self-service",

"short_description":"Test",

"description":"Test",

"work_notes": {
    "Name": " Demo",
    "Email": "demo@demo.com",
    "Manager": "demo",
    "Phone": "9874564"
  },

"comments": "Test",

"close_notes": "",

"close_code": "",

"assignment_group": "Test"
 
}
 
Scripted rest API:
 
var WorkNotes = '';
WorkNotes = reqData.work_notes;
\\ create incident
var inc = new GlideRecord('incident');
inc.initialize();
inc.work_notes = WorkNotes;
 
var incSubmission = inc.insert();
incident_sys_id = incSubmission;


var incNumber = new GlideRecord('incident');
incNumber.addQuery('sys_id', incSubmission);
incNumber.query();
if (incNumber.next()) {
incident_number = incNumber.number;
}
 
 
Incident form:
find_real_file.png
 
 
 
 
 

 
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi UDV,

Is the question about inserting an incident and a worknote?

If so, following Scripted REST API will insert a record. Incident "Number" field is automatically generated and field "caller id" is mandatory so I've change the script accordingly.

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        var requestData = request.body.dataString;
        var json = JSON.parse(requestData);
		
		var inCallerId = json.caller_id;
		var inShortDescription = json.short_description;
		
		var workNotes = json.work_notes;
		var wnName = workNotes.Name;
		var wnEmail = workNotes.Email;
		var wnManager = workNotes.Manager;
		var wnPhone = workNotes.Phone;
		
        var grIncident = new GlideRecord('incident');
		grIncident.initialize();
		//grIncident.number = inNumber;
		grIncident.caller_id = inCallerId;
		grIncident.short_description = inShortDescription;
		var incidentSysId = grIncident.insert();
		
		var grJournal = new GlideRecord('sys_journal_field');
		grJournal.initialize();
		grJournal.name = 'incident';
		grJournal.element = 'comments';
		grJournal.element_id = incidentSysId;
		grJournal.value = 'Name:' + wnName + '\nEmail:' + wnEmail + '\nManager:' + wnManager + '\nPhone:' + wnPhone;
		grJournal.insert();
		
        return {
            'message': incidentSysId
        };

    } catch (e) {
        return {
            'error': e.message
        };
    }

})(request, response);

Tested with following curl command.

curl -u <user name>:<password> -X POST -d '{"caller_id":"<sys_user sys_id>","correlation_display":"","number":"INCXXXXXX","priority":"P2","state":"New","contact_type":"Self-service","short_description":"Test API Incident insert","description":"Test","work_notes":{"Name":" Demo","Email":"demo@demo.com","Manager":"demo","Phone":"9874564"},"comments":"Test","close_notes":"","close_code":"","assignment_group":"Test"}' https://<instance name>.service-now.com/api/<workspace name>/worknotes -H "Content-Type:application/json"

Result

find_real_file.png

 

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

data for work notes is an object so you need to parse it to go inside and fetch the data

So what do you wish to fetch from this?

"work_notes": {
    "Name": " Demo",
    "Email": "demo@demo.com",
    "Manager": "demo",
    "Phone": "9874564"
  },
 
like this
 
var WorkNotes = '';
WorkNotes = reqData.work_notes;

var name = reqData.work_notes.Name;
var email = reqData.work_notes.Email;

\\ create incident
var inc = new GlideRecord('incident');
inc.initialize();
inc.work_notes = WorkNotes;

var incSubmission = inc.insert();
incident_sys_id = incSubmission;


var incNumber = new GlideRecord('incident');
incNumber.addQuery('sys_id', incSubmission);
incNumber.query();
if (incNumber.next()) {
    incident_number = incNumber.number;
}

Regards
Ankur

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

@UDV Prasad 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi UDV,

Is the question about inserting an incident and a worknote?

If so, following Scripted REST API will insert a record. Incident "Number" field is automatically generated and field "caller id" is mandatory so I've change the script accordingly.

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        var requestData = request.body.dataString;
        var json = JSON.parse(requestData);
		
		var inCallerId = json.caller_id;
		var inShortDescription = json.short_description;
		
		var workNotes = json.work_notes;
		var wnName = workNotes.Name;
		var wnEmail = workNotes.Email;
		var wnManager = workNotes.Manager;
		var wnPhone = workNotes.Phone;
		
        var grIncident = new GlideRecord('incident');
		grIncident.initialize();
		//grIncident.number = inNumber;
		grIncident.caller_id = inCallerId;
		grIncident.short_description = inShortDescription;
		var incidentSysId = grIncident.insert();
		
		var grJournal = new GlideRecord('sys_journal_field');
		grJournal.initialize();
		grJournal.name = 'incident';
		grJournal.element = 'comments';
		grJournal.element_id = incidentSysId;
		grJournal.value = 'Name:' + wnName + '\nEmail:' + wnEmail + '\nManager:' + wnManager + '\nPhone:' + wnPhone;
		grJournal.insert();
		
        return {
            'message': incidentSysId
        };

    } catch (e) {
        return {
            'error': e.message
        };
    }

})(request, response);

Tested with following curl command.

curl -u <user name>:<password> -X POST -d '{"caller_id":"<sys_user sys_id>","correlation_display":"","number":"INCXXXXXX","priority":"P2","state":"New","contact_type":"Self-service","short_description":"Test API Incident insert","description":"Test","work_notes":{"Name":" Demo","Email":"demo@demo.com","Manager":"demo","Phone":"9874564"},"comments":"Test","close_notes":"","close_code":"","assignment_group":"Test"}' https://<instance name>.service-now.com/api/<workspace name>/worknotes -H "Content-Type:application/json"

Result

find_real_file.png