+ Start a Discussion
Kamil MieczakowskiKamil Mieczakowski 

deserialise json into custom lead fields

This is probably really easy but I am completely new to Salesforce and struggling to find any tutorials on this.
I have managed to deserialise a JSON object into the following wrapper:
 
public class MMark {

public class Funding {
    public String amount {get; set;}
    public String deal_currency {get; set;}
}

public class Data {
    public String name {get; set;}
    public String description {get; set;}
}

My plan now is to write a trigger class so that whenever a new lead is created and it contains a website, to grab the website variable and query an API containing additional information with it to then return a JSON file and deserialise it using the above class.
I want to use the deserialised data to populate custom fields in the Lead Object.
That's what I've got so far:
 
trigger MMarkBasicFields on Lead (after insert) {
                for (Lead l : Trigger.new){
        //if lead has a website            
        if (l.Website != ''){
        //create a variable containing the website
                        string domain = l.website;
        //invoke parser class
                        MMarkParser mmarkparser = new MMarkParser();
        //use it with the relevant domain
                        mmarkparser.parse(domain);
        //subsequently I want to access the individual values that have been 
//deserialised and update the custom 
 //   fields in the lead object with them but 
 //   I am not sure how to reference them...

                    }
                }   
            }

If anyone wonders here's my deserialise class:
 
public class MMarkParser {
    public MMarkMetadata.Company wrapper {
        get;
        set;
    }

    public void parse(string domain) {

        string companyID = companyIDFetcher.companyIDFetcher(domain);
        system.debug('company ID is'+companyID);

        HTTPRequest request = new HTTPRequest();
        request.setEndpoint('https://api.companies.com'+companyID);  
        request.setMethod('GET');

        HTTP h = new HTTP();
        HTTPResponse response = h.send(request);
        string strresponse = response.getBody();
        system.debug(strresponse);

        this.wrapper = (MMarkMetadata.Company) JSON.deserialize(response.getBody(), MMarkMetadata.Company.class);
        System.debug('wrapper data says----->'+wrapper);
    }
}