Bulkify 铅行业重新分类

Bulkify Lead industry recategorization

我正在尝试设置一份工作,我可以每天 运行 几次,以使我们的潜在客户分类以满足我们的销售和营销批准列表。我的测试脚本运行良好。但是,当我在我的沙箱中访问 运行 它时,我是 运行 一堆虚拟数据 我正在 运行 陷入 DML 限制问题。

我正在寻找如何高效处理尽可能多的潜在客户的想法。

编辑:

18:00:20.106 (10106660336)|EXCEPTION_THROWN|[377]|System.LimitException: Too many DML statements: 151 18:00:20.106 (10106789908)|FATAL_ERROR|System.LimitException: Too many DML statements: 151

Number of DML statements: 151 out of 150 ******* CLOSE TO LIMIT Number of DML rows: 150 out of 10000

global class Industry_Mappings Implements Schedulable {
    Public List < Lead > DisplayIndLeads;
    global void execute(SchedulableContext sc) {

        DisplayIndLeads = new List < Lead > ();
        DisplayIndLeads = [select Industry, Sub_Industry__c from Lead where Sub_Industry__c = null and Industry < > Null and IsConverted < > True];

        //This will create a little efficenty with the for loops
        Integer skip = 0;
        Integer i = 0;

        //Advertising and Marketing
        List < string > AdvertisingAndMarketing = new List < string > {
            'Design', 'Graphic Design', 'Market Research'
        };


        //List for looping
        List < lead > leadstoupdate = new List < Lead > {};

        //This starts the Loop for the leads
        for (Lead ld : DisplayIndLeads) {
            //lead l = (Lead)ld;
            //leadstoupdate.size();

            //Advertising and Marketing
            if (skip == 0) {
                for (string AnM : AdvertisingAndMarketing) {
                    if (ld.Industry == AnM) {
                        ld.Sub_Industry__c = ld.Industry;
                        ld.Industry = 'Advertising and Marketing';
                        skip = 99;
                        leadstoupdate.add(ld);
                    }
                }
            }

            System.debug('***** What is in leadstoupdate: ' + leadstoupdate);
            update leadstoupdate;
        }
    }
}

您在 for 循环中使用 DML 操作导致错误。 您只需要在 for 循环

之外模式化 DML 语句
    for (Lead ld : DisplayIndLeads) {
        if (skip == 0) {
            for (string AnM : AdvertisingAndMarketing) {
                if (ld.Industry == AnM) {
                    ld.Sub_Industry__c = ld.Industry;
                    ld.Industry = 'Advertising and Marketing';
                    skip = 99;
                    leadstoupdate.add(ld);
                }
            }
        }
    }
    update leadstoupdate;

- - - - 更新 - - - -

您的代码只更新一个潜在客户的原因是,在将第一个潜在客户添加到 leadsToUpdate 列表后,您的条件 if (skip == 0) 为假(因为您将其设置为 99)。

更改此条件甚至删除它,因为我看不出有任何理由这样做。

    for (Lead ld : DisplayIndLeads) {
        for (string AnM : AdvertisingAndMarketing) {
            if (ld.Industry == AnM) {
                ld.Sub_Industry__c = ld.Industry;
                ld.Industry = 'Advertising and Marketing';
                leadstoupdate.add(ld);
            }
        }
    }
    update leadstoupdate;