Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Create JSON To be sent in Payload

Surbhi Srivasta
Tera Expert

Hi,

We need to send details of updated fields from an Incident form to 3rd party application.

For this, I have created a mapping table and have defined my source fields( which will be from ServiceNow) and Target fields which is of String Type.

Now, I need to trigger Rest call from a Business rule, and have written the script below:

This BR is written on the Incident table

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    	
    var getIncidentFields = new GlideRecord('Custom table Name');
    getIncidentFields.addQuery('active', true);
    getIncidentFields.query();
    while (getIncidentFields.next()) {
		
        if (current[getIncidentFields.source_attribute] != previous[getIncidentFields.source_attribute]) {
			var obj = {};
			gs.addInfoMessage('1 Value of JSON OBject is '+ current[getIncidentFields.source_attribute]);
			obj[getIncidentFields.target_attribute]= current[getIncidentFields.source_attribute];
            gs.addInfoMessage('Field ' + getIncidentFields['source_attribute'] + '.' + getIncidentFields.source_attribute + ' has changed!');
			gs.addInfoMessage('Value of JSON OBject is '+ JSON.stringify(obj));
        }
		
    }
	



})(current, previous);

 

 

 

Problem with my script is I am not able to Form the JSON object and is coming as blank. I need to form a JSON object here. Can someone please assist.

 

Regards,

Surbhi 

1 REPLY 1

Community Alums
Not applicable

@Surbhi Srivasta it seems issue would with your if condition

if (current[getIncidentFields.source_attribute] != previous[getIncidentFields.source_attribute])

 

I will suggest you to use current.key... instead of using current[] to fetch value, and make sure these condition are giving you true value so it can go inside the if statement.

Hope this will help you.