- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 04:47 PM
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.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 05:06 PM
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");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 05:06 PM
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");

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 06:59 PM
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
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 11:14 AM
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');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 04:28 PM
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.