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

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");

 

I agree with Nick.

You can try below:

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';)

 

Always convert your values in string format while setting JSON attributes.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

That worked, but I am just wondering then how come it is logging without any conversion?

gs.info('...Adding CloseOut for location ' + current.u_wo_number + ' \n');

It showed correctly in your first log because when we concatenate an object (in this case the object is the current.u_wo_number GlideElement) with a string using +, the object's .toString() method is implicitly/automatically called. The .toString() method on a GlideElement returns the actual string value. So your current log is actually the same as doing:

 

gs.info('...Adding CloseOut for location ' + current.u_wo_number.toString() + ' \n');

Unlike this, when you use JSON.stringify(), the toString() method doesn't get called. To correctly see what current.u_wo_number is when stringified you can log that instead:

gs.info(JSON.stringify(current.u_wo_number));
// > "{}"

Because the GlideElement gr.number doesn't have any "enumerable" properties by default, it shows an empty object when stringified.