Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am using Salesforce with a Java integration project.

Here I am integrating data from a SQL database to Salesforce using webservices and vice versa using JSON format webservices.

Here is the code:

public class updateAccount {

    public void UpdateExternalAccount() {

        AccountClass AccountClassObj = new AccountClass();
        AccountClassObj.CreateUserID = 'Krishnan';
        AccountClassObj.Industry = Entitlement_Values;
        AccountClassObj.UserCode = Rep;
        AccountClassObj.UserRole = UserRole;
        AccountClassObj.AccountID = AccountID;
        string serializedOutput = JSON.serializePretty(AccountClassObj);
        system.debug('system.debug '+ serializedOutput);
        String jsonoutput;  
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse(); 
        Http http = new Http(); 
        String url='http://webservice.url.com/sf/ws/Functionname';
        req.setMethod('POST');          
        req.setHeader('content-type', 'application/json');
        req.setHeader('Accept', 'application/json');
        req.setEndpoint(url); 
        system.debug('serializedOutput ' + serializedOutput);   
        req.setBody(serializedOutput);
        //req.setBody('{\"createUserID\":\"Kris\",\"AccountID\":\"123456789\",\"Industry\":\"Energy, Finance\",\"UserCode\":\"abc\",\"UserRole\":\"RW\"}');
        system.debug('req ' + req);
        res = http.send(req);
        System.debug(res.toString());
        System.debug(http.toString());                          
        //jsonoutput = res.getBody();
        if(res.getStatusCode() == 200) {
            jsonoutput = res.getBody();  
        } else {
            system.debug('Could not contact web service');
            jsonoutput = 'Exception : Could not contact web service';
        }
        system.debug('res ' + res);
        system.debug('jsonoutput '+ jsonoutput);
    }

    public class AccountClass {     
        public string Industry;   
        public string AccountID;
        public string UserCode;
        public string UserRole;
        public string CreateUserID;               
    }
}

If I call the above clas,s the DB is not updated, but if I change the code like this:

req.setEndpoint(url); 
//system.debug('serializedOutput ' + serializedOutput);   
//req.setBody(serializedOutput);
req.setBody('{\"createUserID\":\"Kris\",\"AccountID\":\"123456789\",\"Industry\":\"Energy, Finance\",\"UserCode\":\"abc\",\"UserRole\":\"RW\"}');

system.debug('req ' + req);

Now the values are updated in DB.

So it's not updated because of JSON functions:

String serializedOutput = JSON.serializePretty(AccountClassObj);

How do I resolve the above issue?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

I notice that you have the member name CreateUserID (upper case C) in your Apex inner class

public class AccountClass {     
    public string Industry;   
    public string AccountID;
    public string UserCode;
    public string UserRole;
    public string CreateUserID;               
}

but createUserID (lower case C) on this line:

req.setBody('{\"createUserID\":\"Kris\",\"AccountID\":\"123456789\",\"Industry\":\"Energy, Finance\",\"UserCode\":\"abc\",\"UserRole\":\"RW\"}');

Although the JSON spec is strangely silent on the issue, JavaScript itself is case-sensitive. Modify the member name in your inner class to createUserID and see if that fixes things.

share|improve this answer
1  
yes. its working thanks. –  Krishna Feb 13 '13 at 9:26

Though unlikely, it maybe that your JSON endpoint does not tolerate the additional spaces/tabs etc pretty print includes. Whats more likely is that there is subtle difference in the JSON your perhaps not spotting? Some difference caused by the Apex class AccountClass you have created perhaps? When I am working with JSON i find the JSONLint tool very useful. Here is what I recommend....

  1. Open JSONLint in two tabs in your browser
  2. In the first tab, take the debug output from your 'serializedOutput' variable and paste it into the tool, click the Validate button to format it.
  3. In the second tab, take the hard coded JSON string (likely one from the vendors samples?) your using in your question and perform the same.
  4. Alternate between the two tabs to see if there is any structural differences between the two.

This should help you identify and resolve the issue further.

Here is what step 3 looks like for me, before and after pressing Validate...

enter image description here

enter image description here

share|improve this answer
    
I thought spaces were guaranteed to be ignored in JSON, per the spec. If this isn't true, then good safety tip. Should probably minify as a best practice, and not just because it saves on the data transport size. –  Adam Feb 11 '13 at 18:13
    
Yep I expect so as well, though there is not telling it the end point is rolling its own parsing and/or using a library not as per spec. My money is on the two structures actually being different as the cause and not related to pretty print at all. –  Andrew Fawcett Feb 11 '13 at 18:45

One way i could see is to use JSON.serialize instead of pretty string.Use JSON.serialize() function .

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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