3

I am working on a website that is using Salesforce for membership management purposes. Salesforce is the single repository for membership information and needs to be able to send membership information to a website when new members join the organization.

I've figured out most of this, but I've run into a problem when attempting to send my data in JSON format. Below is the code I currently have attached to a button in the Contact View:

{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}

sforce.connection.remoteFunction(
{
    url : "XXXXXXX",
    method : "POST",
    dataType: "json",
    contentType: "application/x-www-form-urlencoded; charset=UTF-8"
    requestData : "{id:" + {!Contact.Id} + ",fn=" + {!Contact.FirstName} + "}",
    onSuccess : function(response) {
        console.log(response);
    }
} );

I've tried a few ways to format the requestData variable, but I can't seem to figure out how to format this correctly when utilizing Salesforce merge fields. Can someone provide me with an example of how to send JSON data in this way?

  • This is one of the purposes of the Outbound Message workflow action, which you might also take a look at. – greenstork Sep 25 '14 at 16:20
  • You can also use REST API calls in a trigger. Outbound Messaging is usually more work (development) on the remote end, and REST is more work on the local end (Apex Code). – sfdcfox Sep 25 '14 at 16:27
1

Your JSON is incorrect. I personally recommend forcing the browser to encode things correctly with JSON.stringify.

...
requestData: JSON.stringify( { "id": "{!Contact.Id}", "fn": "{!JSENCODE(Contact.FirstName)}" } ),
...
| improve this answer | |
  • That appeared to work, but the data is placed in Request Payload and not in Post. Is that normal? – Eric K Sep 25 '14 at 16:31

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.