- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 06:17 AM
I have used following code for inbound scripted rest api create incident
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
gs.log("SOC REST API request body: " + request.body);
// implement resource here
var incomingBody = request.body.dataString; // request.body.data;
var parsedData = JSON.parse(incomingBody);
var shortDesc = base64Decode(parsedData.short_description);
var cat = parsedData.category;
gs.log("cat- CREATE" + cat);
var subcat = parsedData.subcategory;
var corid = parsedData.correlation_id;
var cordis = parsedData.correlation_display;
var pri = parsedData.priority;
var holdres = parsedData.hold_reason;
var ser = parsedData.business_service;
var seroff = parsedData.service_offering;
var prb_id = parsedData.problem_id;
var urg = parsedData.urgency;
var imp = parsedData.impact;
var worknotes = parsedData.work_notes;
var trans_type = parsedData.u_transaction_type;
var comp = parsedData.company;
var sta = parsedData.state;
var assgrp = "";
var group = new GlideRecord("sys_user_group");
group.addQuery("name", parsedData.assignment_group);
group.query();
if (group.next()) {
assgrp = group.sys_id;
}
var tkt_type = parsedData.u_ticket_type;
var con_type = parsedData.contact_type;
var call_id = parsedData.caller_id;
var comm = parsedData.comments;
var desc = base64Decode(parsedData.description);
var gr = new GlideRecord("incident");
gr.initialize();
gr.short_description = shortDesc;
gr.category = cat;
gs.log("gr.cat- CREATE" + gr.category);
gr.subcategory = subcat;
gs.log("gr.subcategory- CREATE" + gr.subcategory);
gr.correlation_id = corid;
gr.correlation_display = cordis;
gr.priority = pri;
gr.urgency = urg;
gr.impact = imp;
gr.work_notes = worknotes;
gr.state = sta;
gr.assignment_group = assgrp;
gr.company = comp;
gr.u_transaction_type = trans_type;
gr.u_ticket_type = tkt_type;
gr.comments = comm;
gr.caller_id = call_id;
gr.hold_reason = holdres;
gr.problem_id = prb_id;
gr.description = desc;
gr.business_service = ser;
gr.service_offering = seroff;
gr.insert();
var body = {};
body.number = gr.number;
// body.state = gr.state;
response.setBody(body);
})(request, response);
However Iam getting error when there is a payload with special characters in des and shortdes.
I have used base64 decode method to match with base64 encode .
please suggest better code.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 06:30 AM
Hi,
If you use request.body.data to get payload then it should work but you are using request.body.dataString may that is the reason throwing error
Thanks,
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 06:30 AM
Hi,
If you use request.body.data to get payload then it should work but you are using request.body.dataString may that is the reason throwing error
Thanks,
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 07:08 AM
Hi Pavan
You are right its working, Tq for your quick reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 11:46 PM
Hi Pavan,
all fields are populating well except service and service offering
is that due to Task table, do I need to change the code ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 06:50 AM
Hi,
Can you share how does your JSON request look like?
which content are you trying to decode?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader