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

Abhijit4
Mega Sage

Hi Mahak,

The line incidentRec.work_notes.changes() will not work as you are gliding into table again and verifying if value is getting changed.

Your script is not efficiently written, please use below approach and script.

Script Include :

var PayloadBuilderUpdate = Class.create();
PayloadBuilderUpdate.prototype = {
initialize: function() {
},
createIncidentPayloadUpdate:function(current,changedFieldName){

var incidentValues = {};
if(changedFieldName=="work_notes"){
incidentValues.work_notes =current.work_notes.getJournalEntry(1);
}
else if(changedFieldName=="comments"){
incidentValues.additional_notes=current.additional_notes.getJournalEntry(1);
}
else{
incidentValues.additional_notes=current.work_notes.getJournalEntry(1);
incidentValues.work_notes =current.additional_notes.getJournalEntry(1);
}

}

Business Rule :

find_real_file.png

Business Rule script:

find_real_file.png

Also make sure that you change field names depending on your field names.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Hello Abhijit, 

Thanks for your reply.

Actually I am using flow designer to trigger the process of copying work notes and comments.

And in flow designer I have created some actions using out of the ebonding actions. I have called script include in that action.

There is one action update remote incident which is copying different fields , i have created copy of that action and i am only copying work notes at present .

But as per my requirement I want to copy both work notes and additional comments as per the changes in individual fields.

So how can i proceed with that approach.

 

 

I know I can create another copy of that action and call different script include there which contains only additional comments but it would require creation of separate flow , action and script include . I was thinking if there is a way to collaborate everything in one .

and what is the trigger point for the flow?

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

The trigger point is if work notes changes or additional comments changes and updated by is not integration user.