How to map json string with salesforce object(consider Lead)??

closed as too broad by Chris Duncombe, martin, Phil Hawthorn, Ratan Paul, Sergej Utko Apr 21 '16 at 12:36

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


You have to create an auxiliar class with the fields that you have on the Json like for example

public with sharing Class LeadHelper {
    public String FXRating {get;set;}
    public String Status {get;set;}
    public String AnalyticsId {get;set;}
    public String AccountBussinessID {get;set;}
    public String source {Get;set;}

    LeadHelper(Lead L){
        AccountBussinessID = (L.isConverted)? String.valueOf(L.ConvertedAccountID):'';
        FXRating = (L.FXRatingLetter__c=='A+')? 'A%2B' :L.FXRatingLetter__c;
        Status = L.Lead_Status_GA__c;
        AnalyticsId = L.analytics_id__c;

Then you do a convert of the Json to this auxiliar class

  LeadHelper  Lead_converted = (LeadHelper) JSON.deserialize(LeadToSend);

then you just have to create a new lead and map the fields.

Lead L = new Lead (FirstName = Lead_converted.name or whatever field you want)

I just pointed general tips, but I think they should be enogh to help you.

If you still have any doubts comment below.

EDIT: okei So i would create this

public with sharing Class JsonLead {
    public String id {get;set;}
    public String Name {get;set;}

then lets say you declare your

JsonLead newJsonLead = (JsonLead) JSON.deserialize(name of the variable you have your Json stored in);
Lead L = new Lead (id=newJsonLead.id, Name = newJsonLead.Name);
update L;


    private static void sendInfoToGA (List<String> LeadsToInsert) {
List <Lead> leads = new List <lead> ();
JsonLead LeadToInsert;      
            while(!LeadsToInsert.isEmpty()) {
                    LeadToInsert = (JsonLead) JSON.deserialize(LeadsToInsert[0], JsonLead.class);
                   Lead L = new Lead (id=LeadToinsert.id, Name = LeadToinsert.Name);    

    insert leads;
  • Thanks for replay! Now i want to make update this json lead info into Lead object.. how can i workout for this function? – Guest Apr 21 '16 at 11:01
  • I suppose you're getting this leadJson info from some kind of Callout, you just have to create that JsonLead class (leadHelper in my case) on your class , and do as I said on my previous comment, then create the lead, copy the fields into it and insert/update normally like in any other apex code. Could you please paste your code so I can get more into context and give you a more precise answer. Thanks! – Alexander Aeons Torn Apr 21 '16 at 11:04
  • yes i'm getting json from external system! { "Id": "1234", "Name": "Test Lead"} I want to update json id with Lead_ID__c and name with Name – Guest Apr 21 '16 at 11:10
  • Look my edit. Hope this helps. – Alexander Aeons Torn Apr 21 '16 at 11:16
  • Almost done! For one record it's Ok here!! But if i have 100s of records so i want to iterate and map the fields.. – Guest Apr 21 '16 at 12:12

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