0

The following code works fine when I have a small json data set see the following:

{  
   "salesforceSeedData":{  
        "LastName":"English",
        "CurrencyIsoCode":"EUR",
        "Email":"[email protected]"    
    }
}

-

global static String createMerchandise(Item[] salesforceSeedData) {

        Lead [] leads  = new Lead [] {};

        for (Item i : salesforceSeedData) {
            Lead l  = new Lead (LastName = i.LastName, CurrencyIsoCode = i.CurrencyIsoCode, Email = i.Email);
            insert l;
        }

}

However this is limited to if I only ever want to insert Leads if I have the following:

{  
   "salesforceSeedData": {
       "fredTheLead":{  
         "object":"Lead",
         "data":{  
            "Email":"[email protected]",
            "Phone":"00447866548732",
            "CountryCode":"gb",
            "FirstName":"Fred",
            "LastName":"Bloggs"
         }
      },
      "fredTheRegistrant":{  
         "object":"Registrant__c",
         "data":{  
            "Lead__c":"{{fredTheLead}}",
            "Campaign":"123"
         }
      }
   }
}

I need to first insert the Lead object then get the ID and pass it to the next thing "fredTheRegistrant" so it knows which user to map it to. however I am a little lost.

Any help would be great, Even just help with how I would iterate through "fredTheLead" and "fredTheRegistrant" and more if I added them.

2 Answers 2

1

use salesforce json parser here is the link

2
  • just checking it out :)
    – OliverBS
    Feb 24, 2015 at 16:41
  • I would suggest considering Json.deserializeUntyped before going with JsonParser, because JsonParser is too low level and could complicate your coding. Feb 24, 2015 at 18:17
0

Salesforce supports three ways in which you can parse the json content, from easy/least flexible to difficult/most flexible. So choose your pick depending on your needs.

  1. Json.deserialize/Json.deserializeStrict Converts the json string into Objects of specified type. So once parsed, you can just read the values as you would from any other Object. In this method, your json schema/structure needs to be predefined at compile time.

  2. Json.deserializeUntyped Parses the json into combination of List and Map objects. You can iterate through them and extract the values you would need. If method 1 doesn't meet your needs, this certainly will as you can read the data you need.

  3. JSONParser class This gives you low level access to reading the json data. If your json is really huge, then you might consider this.

You must log in to answer this question.

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