0

I am trying to account for if a single quote makes it into a field name, or a value of a picklist/multi-picklist, but can't seem to figure out a way to handle it.

I go through and get all the fields in a field set, then serialize them into JSON, and then I grab that JSON string in my Javascript on the client side, then parse the fields out with JSON.parse(). Obviously, a single quote will break this...

I have tried to use escapeSingleQuotes, but my JSON string on my client side has \ where the single quotes used to be instead of \'. I don't know how the single quote disappeared... If this would work as expected, I believe my issue would be resolved, but just having \ instead of \' breaks the JSON.parse as well.

The single quote would need to just be escaped, since if it is a value of a picklist/multi-picklist, it cannot be replaced with something else or it can't be selected and saved because that value won't exist in the picklist/multi-picklist.

1

I go through and get all the fields in a field set, then serialize them into JSON, and then I grab that JSON string in my Javascript on the client side, then parse the fields out with JSON.parse(). Obviously, a single quote will break this...

If you are passing the JSON properly to your JS, it should not matter what the field values are. Apex produces standards-compliant JSON (unless someone has an egregious counterexample?) and JavaScript reads it the same way.

|improve this answer|||||
  • Well, I build a list of custom objects that take the field label, its API name, any help text, and a list of picklist values if it has any (all with their own fields). Then I serialize that list of custom objects, then access it in my JS like `var fieldSet = '{!fieldSetValues}'. And the double quotes do get escaped, but single quotes don't, and then it becomes unparsable... I tried to just pass the object array directly to JS, but that comes out as a string as well when accessed like '{!fieldSetList}'. – Tyler Dahle Nov 10 '17 at 17:35
  • 2
    If you serialize an Apex array directly into JSON, then it is already in escaped notation compliant with JavaScript. You don't actually need JS to serialize it again. So you can just do: var fieldSetList = {!fieldSetListJsonString}; – Charles T Nov 10 '17 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.