0

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);
    }
}

2 Answers 2

2

You will need to create an future call that will trigger from the Leads, I go into more detail about future calls here, now you don't have to do it like this but I'm sure you can find more details online

//Call this from your trigger, pass it your qualifed lead IDS
public static void requestFutureCall(Set<ID> leadIDS)
{
    if(!System.isFuture() && !System.isBatch() && !System.isScheduled())
    {
        leadCallOut(leadIDS);
    }
}

@future(callout=true)
private static void leadCallOut(Set<ID> leadIDS)
{
    leadServiceCallOut(leadIDS);
}

private static void leadServiceCallOut(Set<ID> leadIDS)
{ 
    //Whatever pattern you want to do here, but query out your leads
    //Then you will need to build the expected object
    //Serilize that Object
    //Call out
    //Handle the response
}

There are specific patterns that you could research into but that would be another question for another day

1

You need about calluots in trigger. Shortly, you can do callouts from trigger if method is running asynchronously (future method, Queueable class). Create handler class for trigger, that has asynchronous method

public class LeadTriggerHandler {
    @future (callout=true)
    public static void doCalloutFromFuture(String domain, Id leadId) {
        MMarkParser mmarkparser = new MMarkParser();
        //use it with the relevant domain
        mmarkparser.parse(domain);
        Lead toUpdate = new Lead(
            Id = leadId,
            FieldToUpdate__c = mmarkparser.wrapper.fieldToUpdate
        );
        update toUpdate;
    }
}

trigger MMarkBasicFields on Lead (after insert) {
    for (Lead l : Trigger.new){
        //if lead has a website            
        if (String.isNotBlank(l.Website)){
            LeadTriggerHandler.doCalloutFromFuture(l.Website, l.Id);
        }
    }   
}

Actually this is a bad example, as this trigger works only with one record. If it is possible do triggers bulkified

Documentation

3
  • I down voted this because you built a trigger off a single record which may be one of the worst things you could do, and it was a extremely poor example of how to build a future call, but thanks for the down vote
    – EricSSH
    Jun 3, 2017 at 5:18
  • Exactly you are right, that is why I wrote "Actually this is a bad example, as this trigger works only with one record. If it is possible do triggers bulkified". If you knew about previous related question of author salesforce.stackexchange.com/questions/177221/… you would know, that there is no bulk api call, that could be used. Jun 3, 2017 at 6:49
  • You still should not handle that in the trigger, the triggers job is to simply process the job, It does not care that something can't handle bulk. IF that's the case there should be a design around the future to handle the unbulked service
    – EricSSH
    Jun 4, 2017 at 16:32

You must log in to answer this question.

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