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.

Unable to set JSON object in script include

TT3
Kilo Guru

Please see below script.

 Even though I am setting values for the object, the final statement shows it is missing.

Is it correct way to set JSON object? If not then what is wrong?

var closeOutArray = [];

var closeOutObject = {
	"orderCloseOut": {
		"effectiveBillingDate": "",
		"workCompletionDate": "",
		"workOrderId": ""
	}
};

gs.info('...Adding CloseOut for location ' + current.u_wo_number  + ' \n');
// This shows correct value of u_wo_number

closeOutObject.orderCloseOut.effectiveBillingDate = current.u_effective_date;
closeOutObject.orderCloseOut.workCompletionDate = current.u_effective_date;
closeOutObject.orderCloseOut.workOrderId = current.u_wo_number;
closeOutArray.push(closeOutObject);

gs.info('...closeOutArray = ' + JSON.stringify(closeOutArray) + ' \n';)
// This logs ...closeOutArray = [{"orderCloseOut":{"effectiveBillingDate":{},"workCompletionDate":{},"workOrderId":{}}}]


 

Response will be marked helpful/correct if applicable.

1 ACCEPTED SOLUTION

Nick Parsons
Mega Sage

When you use .field_name, you get back an GlideElement, which when stringified using JSON.stringify gives "{}". You can use .getValue() (or getDisplayValue()) instead to get the string value at each field, or .toString() when setting your values:

 

closeOutObject.orderCloseOut.effectiveBillingDate = current.getValue("u_effective_date");

closeOutObject.orderCloseOut.workCompletionDate = current.getValue("u_effective_date");

closeOutObject.orderCloseOut.workOrderId = current.getValue("u_wo_number");

 

View solution in original post

5 REPLIES 5

Rajat Shirke1
Tera Contributor

Hello,

 

Could you please try by converting values into string and set into the object.

closeOutObject.orderCloseOut.effectiveBillingDate = current.u_effective_date.toString();
closeOutObject.orderCloseOut.workCompletionDate = current.u_effective_date.toString();
closeOutObject.orderCloseOut.workOrderId = current.u_wo_number.toString();
closeOutArray.push(closeOutObject);

 

Or you can use getDisplayValue() which returns value in string format.

closeOutObject.orderCloseOut.effectiveBillingDate = current.getDisplayValue(u_effective_date);
closeOutObject.orderCloseOut.workCompletionDate = current.getDisplayValue(u_effective_date);
closeOutObject.orderCloseOut.workOrderId = current.getDisplayValue(u_wo_number);
closeOutArray.push(closeOutObject);

 

Kind regards,

Rajat