clip_image002Often times, things are never as simple as they seem when beginning a portion of a project. Recently, I was asked to create a button on a form that would take data from the existing form and create a new Salesforce Lead record from it. Sounds simple enough, right? Sure, there is the all the preliminary work of gather requirements, understanding the existing business rules and application rules, but still pretty straight forward.

I began mapping out the fields to be translated to the new Lead record. A good number of them were picklist fields, a few were numeric fields and a couple fields were free text fields between 2000 and 3200 characters in length. So far so good. The client supplied test data and test records so I began my routine testing methodology. All went well, so I passed it on to the client for UAT (user acceptance testing). The client was happy, so, it was time to deploy to production.

Deployment went smoothly and users were beginning to use the new functionality. However, intermittently an error was being produced with a somewhat unclear message. Something was being seen as an unclosed string. Why was it intermittent and where was the unclosed string? It took a little poking around to discover that, on certain records, users were adding special characters to the free text fields. Somehow, this scenario was never tested for, or considered by the client. I tried the usual Find/Replace functions available, but none of them would work. After a bit more research I discovered the encoding function JSENCODE.

This function is a sort of all-purpose function for encoding text in order to make it play nice in JavaScript. Wrapping your text around JSENCODE allows you to capture quotes, apostrophes, line feeds, carriage returns, etc. and translate them within the text in your JavaScript code. If you’re working in the Salesforce scripting window of a form or code behind a button of a form, the JSENCODE function does not appear in the list of available functions. Hopefully this blog will help someone else find a solution quickly when facing the same intermittent error from your JavaScript in Salesforce.

For more information about this blog or C5 Insight, contact us here!