代码之家  ›  专栏  ›  技术社区  ›  vinieth anirudh

比较两个不同的SOQL查询

  •  0
  • vinieth anirudh  · 技术社区  · 7 年前

    enter image description here 我是salesforce的新手,我被这里的情况困住了。

    我每小时都有一节课。我用下面的代码访问了一个帐户,并向MAROPOST(营销自动化工具)发送了一封电子邮件。当这种情况发生时,我想跟踪该帐户,并创建一个案例或日志,说明已发送欢迎电子邮件,这样我就不会再次访问同一帐户。

    请帮忙。下面是工人阶级。请帮忙

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

    }

    2 回复  |  直到 7 年前
        1
  •  1
  •   eyescream    7 年前

    你在循环中调用 governor limit of max 100 callouts . 看见 Limits class 获取当前(&A);以编程方式而非硬编码方式最大化数字。

    除此之外,这应该是非常简单的改变。首先将您的过滤器添加到查询中,并添加一个“子查询”(类似于联接),以提取相关的订阅列表

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

    那就再多几行

    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;
    

    当然,只有在callout正常时,才可能希望将该复选框设置为true;)

        2
  •  0
  •   Tim Willis    7 年前

    在阅读了您的代码之后,我不知道您在哪里尝试实现这一点。如果您发布您的尝试,我很乐意帮助您修复它。

    相反,我会给你不同的逻辑,你想做什么。

    1.) create new checkbox field
    2.) in batch query where box is not checked
    3.) send email
    4.) check checkbox
    

    为了回答您的评论,这里有一些示例代码,您需要自己修复它,我只是在为临时工命名

    for(sobjectname gg:[your query]){
    Send email;
    gg.checkbox = checked;
    update gg;
    }
    

    不过最好是把它膨胀起来

    list<yourSObject> tobeupdated = new list<yourSObject>([Your query]);
    
    for(yourSObject gg: tobeupdated){
    send email;
    gg.checkbox = true;
    }
    update tobeupdated;