可调度类不创建新记录 - 没有做任何事情

时间:2014-08-10 01:08:22

标签: apex-code apex

我在Salesforce中有一系列会计对象。 accounts对象与自定义Transaction对象具有主从关系。此交易对象应包含对该帐户的所有付款和费用。费用每周发生一次,并且基于存储在不同对象中的字段。

我正在尝试编写一个顶级课程,该课程每周会通过并创建反映每周费用的新交易记录。但是,我写的课程正在运行但是(据我所知)没有做任何事情。

我仍然是面向对象编程的新手,对Apex来说当然是新手。任何有关如何了解我做错的更多帮助和参考资料将不胜感激!

global class scheduledWeekly implements Schedulable {

public static String CRON_EXP = '0 50 21 * * ? *';


global static String scheduleIt() {
scheduledWeekly sw = new scheduledWeekly();
return System.schedule('Weekly Billing', CRON_EXP, sw);
}


global void execute(SchedulableContext sc) {
    List <Transaction__c> trans_insert = new List <Transaction__c>();

    Child__c ac = [
        Select Family_Account__c
        From Child__c
        Where Enrolled__c = true];

    Account at = [
        Select Name
            , Weekly_Tuition__c
        From Account
        Where Name = :ac.Family_Account__c
            and Weekly_Tuition__c > 0
    ];

    Account ae = [
        Select Name
            , Extra_Curricular_Tuition_Fee__c
        From Account
        Where Name = :ac.Family_Account__c
            and Extra_Curricular_Tuition_Fee__c > 0
    ];

        for(Account account: [
            Select Name
            From Account
            Where Name = :ac.Family_Account__c
                and Weekly_Tuition__c > 0
        ]) {Transaction__c nt = new Transaction__c(); 

           nt.Family_Account__c = at.Name;
           nt.Notes__c = 'Automated Charge';
           nt.Transaction_Date__c = System.Today();
           nt.Transaction_Type__c = 'Tuition Charge';
           nt.Amount__c = at.Weekly_Tuition__c * -1;

           trans_insert.add(nt);}
           //database.insert(nt);

        for(Account account: [
            Select Name
            From Account
            Where Name = :ac.Family_Account__c
                and Extra_Curricular_Tuition_Fee__c > 0
        ]) {Transaction__c ne = new Transaction__c();

           ne.Family_Account__c = ae.Name;
           ne.Notes__c = 'Automated Charge';
           ne.Transaction_Date__c = System.Today();
           ne.Transaction_Type__c = 'Tuition Charge';
           ne.Amount__c = ae.Extra_Curricular_Tuition_Fee__c * -1;

           trans_insert.add(ne);}

    insert trans_insert;
    }
}

这是我工作的代码,最后!!! 为简单起见,我将我尝试编写的两个事务划分为两个单独的类(更容易调试)。这是我到目前为止所从事的课程。

global class scheduledWeekly implements Schedulable {
    //Variables of CRON_EXP in the order they appear
        //Seconds: 0-59 - No special characters allowed
        //Minutes: 0-59 - No special characters allowed
        //Hours: 0-23 - Special Characters: , - * /
        //Day of Month: 1-31 - Special Characters: , - * ? / L W
        //Month: 1-12 - Special Characters: , - * ? / L W
        //Day of Week: 1-7 - Special Characters: , - * ? / L #
        //Year (optional): null or 1970-2099
   //Special Character Descriptions
        //, delimits values
        //- specifies a range
        //* specifies all possible values
        //? no specific value
        //use a slash(/) to indicate a starting point followed by an increment
        //L is the Last
        //W is the closest Weekday
        //# specifies the nth day in the format of day_of_week by day of month
public static String CRON_EXP = '0 39 15 * * ? *';

global static String scheduleIt() {
scheduledWeekly sw = new scheduledWeekly();
return System.schedule('Weekly Billing', CRON_EXP, sw);
}

global void execute(SchedulableContext sc) {
    List <Transaction__c> trans_insert = new List <Transaction__c>();

    Account[] at = [
        Select Name
            , Weekly_Tuition__c
        From Account
        Where Id in (Select Family_Account__c 
                     From Child__c 
                     Where Enrolled__c = true)
            and Weekly_Tuition__c > 0
    ];

    system.debug('Manual Debug >>>>>>>>>>>>>>>>>Active Accounts with Tuition:' + at);

    System.debug('Manual Debug >>>>>>>>>>>>>>>>> ' + at.size() + ' records found with tuition charges needed.');
    if(at.size() > 0)
    {
        System.debug('Manual Debug >>>>>>>>>>>>>>>>> at.size found to be greater than 0.');
        for(Account account: [
        Select Name
            , Weekly_Tuition__c
        From Account
        Where Id in (Select Family_Account__c 
                     From Child__c 
                     Where Enrolled__c = true)
            and Weekly_Tuition__c > 0
    ]) {Transaction__c newTuitionCharge = new Transaction__c(); 
           newTuitionCharge.Family_Account__c = account.Id;
           newTuitionCharge.Notes__c = 'Automated Charge';
           newTuitionCharge.Transaction_Date__c = System.Today();
           newTuitionCharge.Transaction_Type__c = 'Tuition Charge';
           newTuitionCharge.Amount__c = account.Weekly_Tuition__c * -1;

           system.debug('Manual Debug >>>>>>>>>>>>>>>>>Family_Account: ' + newTuitionCharge.Family_Account__c);
           system.debug('Manual Debug >>>>>>>>>>>>>>>>>Tuition Amount: ' + newTuitionCharge.Amount__c);

           trans_insert.add(newTuitionCharge);
           system.debug('Manual Debug >>>>>>>>>>>>>>>>>newTuitionCharge added to trans_insert list.');}
    }

    system.debug('Manual Debug >>>>>>>>>>>>>>>>>' + trans_insert.size() + ' rows ready to be saved.');

    Database.SaveResult[] srList = Database.insert(trans_insert,false);
    for(Database.SaveResult sr : srList){
        if(sr.isSuccess()){
            system.debug('Successfully inserted Transaction. Transaction ID: ' + sr.getID());
        }
        else{
            for(Database.Error err : sr.getErrors()){
                System.debug('The following error has occurred.');
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Transaction fields that affected this error: ' + err.getFields());

            }
        }
    }
    }
}

1 个答案:

答案 0 :(得分:1)

您确定您的代码正在执行吗?根据文档,Salesforce CRON表达式只需要6个参数,你有7个。这里记录了6部分CRON表达式:

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_crontrigger.htm

你在/ a0e(&#34; Scheduled Apex&#34;在设置侧菜单中)看到了一行吗? &#34;下一次预定运行&#34;专栏并符合您的期望?您可以从管理员屏幕中删除该行,然后重新运行scheduledWeekly.scheduleIt()以在必要时重新安排。

如果您的代码中存在问题,则需要收集调试日志并通过它解析以获取线索。您可以添加各种System.debug()调用,以便更好地了解解析日志时代码的作用。例如trans_insert的大小是多少? Zero将是一个很大的线索,然后我会问acatae的大小是什么?

另外,将最后一行更改为

Database.SaveResult[] srList = Database.insert(trans_insert);

然后您可以从插入中梳理日志错误。根据我的经验,验证规则经常在插入/更新操作期间冒出来,您可以从Database.SaveResult[]获取必要的详细信息,如下所示:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database_saveresult.htm#apex_methods_system_database_saveresult

我觉得发现我的调试线埋在其他日志噪声中有点困难,所以我习惯使用一些文字装饰,以便在日志中轻松找到我感兴趣的内容:

System.debug('\n\n\n####### ' + something_interesting + ' ########\n\n\n');

附注:我非常肯定您可以通过监控&#34;调试日志&#34;的用户记录来捕获实际作业执行的结果。 /setup/ui/listApexTraces.apexp但这可能很烦人且耗时。设置名为execute2()的第二个方法并在其中移动execute(SchedulableContext sc)代码会更容易。 execute()显然会调用execute2()。美妙之处在于您可以根据需要运行scheduledWeekly.execute2();以轻松捕获调试日志。