How to create a json payload in script include ?

Mahak2
Kilo Guru

Hello experts,

My requirement is to transfer the worknotes and additional comments to target instance whenever worknotes changes or additional comments changes in source instance.

I have used Ebonding.

But while creating payload i am not able to get the correct data in my payload:

Below is the script I am using:

var PayloadBuilderUpdate = Class.create();
PayloadBuilderUpdate.prototype = {
initialize: function() {
},
createIncidentPayloadUpdate:function(incident_sys_id){
var incidentSysId = incident_sys_id;
var incidentRec = new GlideRecord('incident');
//incidentRec.get(incidentSysId);
incidentRec.addQuery('sys_id',incidentSysId);
incidentRec.query();
while(incidentRec.next())
{
if(incidentRec.work_notes.changes())
{
var incidentValues = {};

incidentValues.work_notes =incidentRec.work_notes.getJournalEntry(1);
//comments: incidentRec.comments.getJournalEntry(1)


}
}


return incidentValues;
},

type: 'PayloadBuilderUpdate'
};

 

Can you please help here.

@Ankur Bawiskar @Ashutosh Munot 

Thanks in advance!!

10 REPLIES 10

VaranAwesomenow
Mega Sage

can you declare outside the while loop

var incidentValues = {};

so you can retain the values stored.

Hello Anil,

 

I have tried this but it's giving empty value in response.

Ravi9
ServiceNow Employee
ServiceNow Employee

Can you elaborate on where  are you failing ?

Few things i noticed not that they are all wrong but need consideration

1. if you are passing direct sysid of incident in the function you dont need a while , it will always be one record

2. you are returning the object directly , convert to a string and then try , something like JSON.stringify(variable_name)

Hello Ravi ,

 

My issue is whenever I am changing work notes in one record in source instance same work notes are not getting copied through ebonding.

So earlier it was working fine with this script:

PayloadBuilderUpdate.prototype = {
initialize: function() {
},
createIncidentPayloadUpdate:function(incident_sys_id){
var incidentSysId = incident_sys_id;
var incidentRec = new GlideRecord('incident');
incidentRec.get(incidentSysId);

var incidentValues = {

work_notes: incidentRec.work_notes.getJournalEntry(1)
//comments: incidentRec.comments.getJournalEntry(1)

};




return incidentValues;
},

type: 'PayloadBuilderUpdate'
};

But since my requirement is to copy only worknotes when work notes are added and copy only additional comments when additional comments are added ,through above script when either of worknotes or additional comments are added it was copying both work notes and additional comments.

That's why i though to add if condition and added the condition of work_notes.changes() but it is now not copying the worknotes as well.

 

Also the points which you have highlighted are taken care of , still it's not working.