Search code examples
salesforcevisualforceapexforce.com

Compare two different SOQL queries


enter image description hereI am new to salesforce and I am stuck with a situation here.

I have a class which is scheduled every hour. I hit an account with the below code and an email is sent out to MAROPOST (Marketing automation tool). When this happen I want to track the Account and create a case or a log which says Welcome Email is sent so that I don't hit the same Account again.

Please help. Below is the working class. Please help

public class PD_WelcomeMaroPost {
public static string sendEmailThroughMaro(string myInpEmail) {
    string successContacts = '';
    string failureContacts = '';


    // SQL to fetch FBO who Joined Today
    list<Account> conts = new list<Account> ([SELECT name, Email_FLP_com__c,
    (SELECT Id
    FROM Stripe_Subscriptons__r
    WHERE Start_Date__c= TODAY
        AND Status__c='active'
        AND Welcome_Email__C = false
    LIMIT 1)
from account
where ID IN (
    select Distributor__c
    from Stripe_Subscripton__c
    where Start_Date__c= TODAY
        AND Status__c='active'
        AND Welcome_Email__C = false)
AND  Email_FLP_com__c != NULL
LIMIT 100]);



    system.debug('>>>>>>>>>>' + conts);
    overallEmail myEmail = new overallEmail();
    List<Stripe_Subscripton__c> subsToUpdate = new List<Stripe_Subscripton__c>();
    for(Account c : conts){

        myEmail.email.campaign_id = 172;
        myEmail.email.contact.Email = c.Email_FLP_com__c;
        myEmail.email.contact.first_name = c.name;
        /**MAp<String, String> tags = new Map<String, String>();
        tags.put('firstName', c.name);
        myEmail.email.tags = tags;**/
        system.debug('#### Input JSON: ' + JSON.serialize(myEmail));


        try{
            String endpoint = 'http://api.maropost.com/accounts/1173/emails/deliver.json?auth_token=j-V4sx8ueUT7eKM8us_Cz5JqXBzoRrNS3p1lEZyPUPGcwWNoVNZpKQ';
            HttpRequest req = new HttpRequest();
            req.setEndpoint(endpoint);
            req.setMethod('POST');
            req.setHeader('Content-type', 'application/json');
            req.setbody(JSON.serialize(myEmail));
            Http http = new Http();
            system.debug('Sending email');
            HTTPResponse response = http.send(req); 
            system.debug('sent email');
            string resultBodyGet = '';
            resultBodyGet = response.getBody();
            system.debug('Output response:' + resultBodyGet);
            maroResponse myMaroResponse = new maroResponse();
            myMaroResponse = (maroResponse) JSON.deserialize(resultBodyGet, maroResponse.class);
            system.debug('#### myMaroResponse: ' + myMaroResponse);
            if(myMaroResponse.message == 'Email was sent successfully')
               successContacts = successContacts + ';' + c.Email_FLP_com__c;
            else
                failureContacts = failureContacts + ';' + c.Email_FLP_com__c;
        }
        catch (exception e) {
            failureContacts = failureContacts + ';' + c.Email_FLP_com__c;
            system.debug('#### Exception caught: ' + e.getMessage());                
        }

        c.Stripe_Subscriptons__r[0].Welcome_Email__c = true;
        subsToUpdate.add(c.Stripe_Subscriptons__r[0]);

    }
    Update subsToUpdate;
   return 'successContacts=' + successContacts + '---' + 'failureContacts=' + failureContacts;   

}

public class maroResponse {
    public string message {get;set;}
}

public class overallEmail {
    public emailJson email = new emailJson();
}

public class emailJson {
    public Integer campaign_id;
    public contactJson contact = new contactJson();
   // Public Map<String, String> tags;
}

public class contactJson {
    public string email;
    public string first_name;
}

}


Solution

  • You're making a callout in a loop, there's governor limit of max 100 callouts. See Limits class to obtain current & max numbers programatically rather than hardcoding it.

    Other than that it should be pretty simple change. First add your filter to the query and add a "subquery" (something like a JOIN) that pulls the related list of subscriptions

    list<Account> conts = new list<Account> ([SELECT name, Email_FLP_com__c,
            (SELECT Id
            FROM Stripe_Subscriptions__r
            WHERE Start_Date__c= TODAY
                AND Status__c='active'
                AND Welcome_Email__C = false
            LIMIT 1)
        from account
        where ID IN (
            select Distributor__c
            from Stripe_Subscripton__c
            where Start_Date__c= TODAY
                AND Status__c='active'
                AND Welcome_Email__C = false)
        AND  Email_FLP_com__c != NULL
        LIMIT 100]);
    

    Then it's just few lines more

    List<Stripe_Subscription__c> subsToUpdate = new List<Stripe_Subscription__c>();
    
    for(Account a : conts){
        // do your maropost code here
    
        a.Stripe_Subscriptions__r[0].Welcome_Email__c = true;
        subsToUpdate.add(a.Stripe_Subscriptions__r[0]);
    }
    update subsToUpdate;
    

    Of course you might want to set that checkbox to true only if callout went OK ;)