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 getting some very odd behavior from the JSON serializer where if the Sobject was originally instantiated from a SOQL query it will exclude any fields that were not part of my query from the serialized JSON string even if I fill in those fields later in my code.

sample code:

Contact con = [select id, lastname, accountid from Contact limit 1];
con.firstname='foo';
string jsonstring = Json.serialize(con);
system.debug(jsonstring);
System.Assert(jsonstring.contains('foo') == false); //this should not be happening

Contact newcon = new Contact(id=con.ID,lastname=con.Lastname,accountid=con.AccountID);
newcon.firstname='foo';
jsonstring = Json.serialize(newcon);
system.debug(jsonstring);
System.Assert(jsonstring.contains('foo'));

UPDATE: This was fixed in Spring '13!

share|improve this question
    
Nothing new here in force.com. Many times we see without including the field in the query if we try some opertion we get errors like field is being refrenced without including in SOQL query –  Mohith Kumar Oct 20 '12 at 5:24
1  
Disagree with Mohith - you can set a field in an sobject even if the field was not in the select. In the example, update con will successfully update the firstname. It is odd that system.debug(con) does show 'foo' as the firstname, whilst system.debug(JSON.serialize(con)) does not. –  Stephen Willcock Oct 20 '12 at 13:19
    
@StephenWillcock I just did this Account acc=[Select Id from Account LIMIT 1]; System.debug('Validate'+acc.Name); I got error of Sobject Row was retrieved without including in query .But you are right when i did use this Account acc=[Select Id from Account LIMIT 1]; acc.Name='Hello'; System.debug('Validate'+acc.Name);This ran so ya i agree its weird little as to why in first case it gave error –  Mohith Kumar Oct 20 '12 at 13:28
2  
Mohith you cannot get a field that you havent selected, but you can set it –  Stephen Willcock Oct 20 '12 at 13:44
    
@StephenWillcock - Actually, I can get the field w/o querying it, but only after I first set it. No errors with this: Contact con = [select id, lastname, accountid from Contact limit 1]; con.firstname='foo'; System.debug('firstName=' + con.firstName);; however take out the con.firstname='foo'; and there will be an error. –  Peter Knolle Oct 20 '12 at 14:21
add comment

2 Answers

I do not see anything in the System JSON methods or JSON Parser methods that document the behavior that you observe. I don't see anything in the Getting Started with Apex wiki article. I don't see anything in the SOQL and SOSL guide. I also found nothing in the Salesforce Known Issues

The serialize method documentation is:

Serializes Apex objects into JSON content. The object argument is the Apex object to serialize.

The following example serializes a new Datetime value.

 Datetime dt =
 Datetime.newInstance(
                Date.newInstance(
                   2011, 3, 22),
                Time.newInstance(
                   1, 15, 18, 0)); 
     String str = JSON.serialize(dt); 
     System.assertEquals(
        '"2011-03-22T08:15:18.000Z"',
        str);

I executed your exact code from Execute Anonymous from my Force.com IDE and observed the exact same behavior.

My conclusion is that I either am not finding the correct documentation, this is the correct behavior and the documentation is lacking, or this is a SF issue.

I also modified your SOQL to:

Contact con = [select id, lastname, firstName, accountid from Contact limit 1];

Then set the first name the same:

con.firstName = 'foo';

This does result in 'foo' being in the jsonstring.

Obviously, the workaround is to always select all of the fields that you need to appear in the jsonstring or construct the new instance like you did.

share|improve this answer
    
+1 for this experimentation. –  Mohith Kumar Oct 20 '12 at 18:04
    
@PeterKnolle Thanks for all the legwork. I think it's a sf bug as well but I'm going to leave this open for a bit to see if we can get an "official" sf response. –  Greg Grinberg Oct 20 '12 at 20:17
    
@grigriforce - Good idea. Two of us confirm the behavior, so it's definitely not just something in your environment. –  Peter Knolle Oct 20 '12 at 20:21
    
This is much better than the answer I was going to give; this must be a Salesforce bug, you'll need to log a case –  Daniel Blackhall Oct 21 '12 at 6:00
add comment
up vote 3 down vote accepted

From speaking with salesforce support this is a known issue with no current time frame for when this will be fixed.

As Peter said the only workaround is to include all the fields in the query that you want to show up in the JSON.

UPDATE: This was fixed in Spring '13!

share|improve this answer
add comment

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.