How to fix this error in my scheduled script? ```...
# ask-stanley-ai
a
How to fix this error in my scheduled script?
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "UNSUPPORTED_DATA_FORMAT",
  "message": "You have supplied an invalid value for data format: JSON: The content of elements must consist of well-formed character data or markup.",
  "id": "",
  "stack": [
    "Error\n at Object.report (/SuiteScripts/Scheduled Script/ss_fbsg_notify_pochanges_on_solines.js:60:13)"
  ],
  "cause": {
    "type": "internal error",
    "code": "UNSUPPORTED_DATA_FORMAT",
    "details": "You have supplied an invalid value for data format: JSON: The content of elements must consist of well-formed character data or markup.",
    "userEvent": null,
    "stackTrace": [
      "Error\n at Object.report (/SuiteScripts/Scheduled Script/ss_fbsg_mysscript.js:60:13)"
    ],
    "notifyOff": false
  },
  "notifyOff": false,
  "userFacing": true
}
The erroring line in the code is this:
Copy code
tRenderer.addCustomDataSource({alias: "poChangesBySo", format: render.DataSource.OBJECT, data: JSON.parse(JSON.stringify(poChangesBySo))});
I have tried making it a JSON string, and I have tried leaving it as a Javascript object, and I have tried stringifying the object and re-parsing it into a JSON string, and I think I did all these with render.DataSource.OBJECT and render.DataSource.JSON poChangesBySo is initialized as a Javascript object:
Copy code
let poChangesBySo = {};
It is populated inside an iterator over search results:
Copy code
poChangesBySo[soResult.id] = {
				salesOrder: JSON.parse(JSON.stringify(soResult)),
				lines: poResults.filter(po => {
					return soResult.getValue({name: "applyingtransaction"}) ==	po.id;
				}).map(po => JSON.parse(JSON.stringify(po)))
			};
So it is an object, not an array, and not a string when I use render.DataSource.OBJECT.
c
AI generated answer (using internal Slack threads):
The error you're encountering is due to the fact that the
addCustomDataSource
method expects an object, not an array or a string. When you use
JSON.stringify()
, it converts the object into a string, which is not a valid input for this method. Similarly, passing an array directly is also not valid. The solution is to wrap your array inside an object. Here's how you can modify your code:
Copy code
javascript
tRenderer.addCustomDataSource({
    alias: "poChangesBySo", 
    format: render.DataSource.OBJECT, 
    data: { 'poChangesBySo': JSON.parse(JSON.stringify(poChangesBySo)) }
});
In this code,
poChangesBySo
is an array that is wrapped inside an object. This way, you're passing an object to the
addCustomDataSource
method, which is the expected input. Remember, when you're referencing this data in your template, you'll need to use the
alias.property
format. In this case, it would be
poChangesBySo.poChangesBySo
. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.904756069
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.87716794
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.863276899
Please provide your feedback on this answer.