- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 05:11 AM
Hi All,
I have a requirement where I need to send an update back to third party when worknotes are changed. I created the business rule with condition "current.work_notes.changes()"
BR is getting triggered but when I am capturing the fields in body nothing is getting captured and it is not going inside try. Directly going in catch so not sending any data. Below is the BR. I captured work notes in var notes and getting log: "Notes 26-08-2022 17:26:15 - Shivani Malhan (Work notes)
test7"
Then in var body i am giving the fields which i want to send. and in logs I am getting: "Body [object Object]"
Business Rule:
(function executeRule(current, previous /*null when async*/) {
var sysid = current.sys_id;
//gs.log('sysid '+sysid);
var notes = current.work_notes.getJournalEntry(1);
gs.log('Notes '+notes);
var body = {
"number": current.number.toString(),
"state":current.getDisplayValue('state'),
"work_notes":notes,
};
gs.log('Body '+body);
try {
var r = new sn_ws.RESTMessageV2('xyz', 'Default PUT');
r.setRequestBody(JSON.stringify(body));
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('response'+ responseBody);
gs.log("status1234"+ httpStatus);
gs.log('Body'+ r.getRequestBody());
}
catch(ex) {
var message = ex.message;
gs.log('fail');
}
})(current, previous);
Need help!!
Thanks,
Shivani
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 05:50 AM
Are you sure above is the complete code snippet that you are using?
because in the exception it says "response" is not defined. but actually we defined var response
if you are using other line of coding make sure you define the variable before using it.
Thanks & Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 05:28 AM
Please use the below code snippet and check the logs, if there is any exception then in the log you will see a message that starts with "Failed due to exception "
(function executeRule(current, previous /*null when async*/ ) {
var sysid = current.sys_id;
//gs.log('sysid '+sysid);
var notes = current.work_notes.getJournalEntry(1);
gs.log('Notes ' + notes);
var body = {
"number": current.number.toString(),
"state": current.getDisplayValue('state'),
"work_notes": notes.toString(),
};
gs.log('Request Body JSON String ' + JSON.stringify(body));
try {
var r = new sn_ws.RESTMessageV2('xyz', 'Default PUT');
r.setRequestBody(JSON.stringify(body));
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('Response body' + responseBody);
gs.log("HTTP Response Code " + httpStatus);
gs.log('Request Body Posted ' + r.getRequestBody());
} catch (ex) {
var message = ex.message;
gs.log('Failed due to exception ' + message);
}
})(current, previous);
Thanks & Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 05:39 AM
Hi,
Thanks for your response. I tried the snippet you suggested and below is the outcome:
gs.log('Request Body JSON String ' + JSON.stringify(body));
outcome of this:
Request Body JSON String {"number":"INC0780589","state":"In Progress","work_notes":"26-08-2022 18:04:39 - Shivani Malhan (Work notes)\ntesting\n\n"}
which is correct.
gs.log('Failed due to exception ' + message);
outcome of this:
Failed due to exception "response" is not defined.
Kindly let me know what should be done.
Thanks,
Shivani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 05:50 AM
Are you sure above is the complete code snippet that you are using?
because in the exception it says "response" is not defined. but actually we defined var response
if you are using other line of coding make sure you define the variable before using it.
Thanks & Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2022 06:12 AM
Yes, this is the entire code which I am using and response is defined as well.