比较两个不同的 SOQL 查询

Compare two different SOQL queries

我是 salesforce 的新手,我遇到了一个问题。

我有一个 class 每小时安排一次。我用下面的代码打了一个帐户,一封电子邮件被发送到 MAROPOST(营销自动化工具)。发生这种情况时,我想跟踪帐户并创建一个案例或日志,其中显示欢迎电子邮件已发送,这样我就不会再次访问同一个帐户。

请帮忙。下面是工作 class。请帮助

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

}

阅读您的代码后,我看不出您试图在何处完成此操作。如果您 post 您的尝试,我很乐意帮助修复它。

相反,我会针对您尝试做的事情给出不同的逻辑。

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;

您正在循环中进行标注,governor limit of max 100 callouts. See Limits class 以编程方式获取当前和最大数字,而不是对其进行硬编码。

除此之外应该是非常简单的改变。首先将您的过滤器添加到查询中,然后添加一个 "subquery"(类似于 JOIN)来提取相关的订阅列表

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;

当然,您可能希望仅在标注成功时将该复选框设置为 true ;)