0

Premise: I have a list of string which I have to pass it from LWC to APEX and in APEX I have to put this List in SOQL where clause to get the data. Finally I have to return the SOQL result list back to LWC. I'm using imperative way of calling the APEX method and sending the parameters to APEX. At LWC side I have used methodName({strLst: JSON.stringify(strLstLWC)});

In each of the record of "strLstLWC", I have some special characters. For example, one value could be 'A || B' while another value could be 'someText #### someOtherText'.

Problem: At APEX side I'm getting this list of string but all of my special characters are getting convereted into their respective ASCII values. For example, A || B is getting converted into A || B and so on. And to use these values in my SOQL I dont need the ASCII values.

I can use some regEx to convert special character to normal characters or else can use replaceAll to get the desired result, but is there any standard way of achieving this?

Note: I tried unescape methods of String but there was no luck.

System.debug('unescapeHtml3 ++++++ ' + a.unescapeHtml3());
System.debug('unescapeHtml4 ++++++ ' + a.unescapeHtml4());
System.debug('unescapeXml ++++++ ' + a.unescapeXml());
//System.debug('unescapeJava ++++++ ' + a.unescapeJava());
//System.debug('unescapeUnicode ++++++ ' + a.unescapeUnicode());

1 Answer 1

1

Don't JSON-encode your strings; the platform does this for you automatically.

methodName({strLst: strLstLWC});

By double-encoding, you're making more work for yourself when you later have to double-decode it on the server side (one of these is done automatically for you).

Note that if you're passing in a list of strings, just say so in your apex:

@AuraEnabled public static void methodName(String[] strLst) {

The platform is smart enough to handle data conversions for you automatically. Do not try to outsmart the system, unless you happen to be running into some very specific bugs that you're trying to work around.

In general, the data types in LWC components are compatible with the server-side versions of those data types, so manual intervention, including serializing to JSON is rarely, if ever, needed.

2
  • Thanks @sfdcfox, as per your instructions I changed my code to above way and it worked like a charm. - "Issue is resolved". One small doubt related to this, when I print my list at apex side it still shows ASCII characters instead of special chracters, and to my amaze with that also system finds proper values from the database (records have special characters when seens in UI and not the ASCII values). So can you please explain this behaviour?
    – user43598
    Commented Nov 21, 2021 at 0:15
  • 1
    @user43598 During the serialization and deserialization process when you call Apex, everything is handled for you. As such, you don't ordinarily need to do your own encoding and decoding. The platform is already designed around the "principle of least astonishment", or, in other words, doing things in the most straight-forward way possible should always work the way you expect.
    – sfdcfox
    Commented Nov 21, 2021 at 1:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .