简体   繁体   English

比较两个不同的SOQL查询

[英]Compare two different SOQL queries

在此处输入图片说明 I am new to salesforce and I am stuck with a situation here. 我是Salesforce的新手,在这里我陷入困境。

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). 我用下面的代码打了一个帐户,然后电子邮件发送给了MAROPOST(营销自动化工具)。 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;
}

} }

You're making a callout in a loop, there's governor limit of max 100 callouts . 您要在一个循环中进行标注,因此调速器限制为最多100个标注 See Limits class to obtain current & max numbers programatically rather than hardcoding it. 请参阅Limits类以编程方式获取当前和最大数量,而不是对其进行硬编码。

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 首先,将过滤器添加到查询中,然后添加一个“子查询”(类似于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]);

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 ;) 当然,您可能只想在标注正常的情况下才将复选框设置为true;)

After reading your code, I don't see where you tried to accomplish this. 阅读您的代码后,我看不到您尝试在何处完成此操作。 If you post your attempt I'd be glad to help fix it. 如果您发表尝试,我们很乐意为您解决。

Instead I'll give you different logic for what you are trying to do. 相反,我将为您尝试执行的操作提供不同的逻辑。

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

to answer your comment here is some sample code, you will need to fix it yourself, i am just making temp names 要回答您的评论,这里是一些示例代码,您需要自己进行修复,我只是在临时命名

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

it'd be better to make it bulkified though 最好将其散开

list<yourSObject> tobeupdated = new list<yourSObject>([Your query]);

for(yourSObject gg: tobeupdated){
send email;
gg.checkbox = true;
}
update tobeupdated;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM