【问题标题】:How do I test a trigger with an approval process?如何使用审批流程测试触发器?
【发布时间】:2012-03-24 13:57:04
【问题描述】:

我有一个触发器,可在满足某些条件时启动审批流程:

trigger AddendumAfterIHMS on Addendum__c (after update) {

  for (integer i = 0; i<Trigger.new.size(); i++){

    if(Trigger.new[i].RecordTypeId != '012V0000000CkQA'){

        if(Trigger.new[i].From_IHMS__c != null && Trigger.old[i].From_IHMS__c == null){

            ID addendumId = Trigger.new[i].Id;

            // Start next approval process
            Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
            request.setObjectId(addendumId);
            Approval.ProcessResult requestResult = Approval.process(request);

        }
    }
  }
}

它工作得很好,但现在我需要为它创建一个测试类。我创建了一个类,它使代码的覆盖率达到 75%,这是最低限度,但我很挑剔,喜欢对我的代码有 100% 的覆盖率。我现在拥有的测试课程卡在request.setObjectId(addendumId); 线上,并且没有越过它。我收到的错误是:

System.DmlException: Update failed. First exception on row 0 with id a0CV0000000B8cgMAC; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AddendumAfterIHMS: execution of AfterUpdate

这是我到目前为止编写的测试类,大部分类实际上测试了其他一些触发器,但引发错误的重要行是最后一行update addendumTierFeature;

@isTest
private class AddendumTest {

static testMethod void myUnitTest() {

    // Query Testing Account, will need ID changed before testing to place into production
    Account existingAccount = [SELECT Id FROM Account LIMIT 1];
    Model__c existingModel = [SELECT Id FROM Model__c WHERE Active__c = TRUE LIMIT 1];
    Pricebook2 existingPricebook = [SELECT Id,Name FROM Pricebook2 WHERE IsActive = TRUE LIMIT 1];
    List<Contact> existingContacts = [SELECT Id,Name FROM Contact LIMIT 2];

    Contact existingContactPrimary = existingContacts[0];
    Contact existingContactSecondary = existingContacts[1];

    Opportunity newOpportunity = new Opportunity(

        Name = 'New Opportunity',
        Account = existingAccount,
        CloseDate = Date.today(),
        Order_Proposed__c = Date.today(),
        StageName = 'Branch Visit - Not Responding',
        Opportunity_Follow_Up__c = 'Every 120 Days',
        LeadSource = 'Farm Lists',
        Source_Detail__c = 'FSBO',
        Model_Name__c = existingModel.Id,
        Processing_Fee__c = 100.50,
        Site_State__c = 'OR',
        base_Build_Zone__c = 'OR',
        Pricebook_from_Lead__c = existingPricebook.Name

    );

    insert newOpportunity;
    //system.assert(newOpportunity.Id != null);

    ID newOppId = newOpportunity.Id;

    OpportunityContactRole contactPrimary = new OpportunityContactRole(
        Role = 'Primary',
        IsPrimary = true,
        OpportunityId = newOppId,
        ContactId = existingContactPrimary.Id
    );

    OpportunityContactRole contactSecondary = new OpportunityContactRole(
        Role = 'Primary',
        IsPrimary = false,
        OpportunityId = newOppId,
        ContactId = existingContactPrimary.Id
    );

    insert contactPrimary;
    insert contactSecondary;

    newOpportunity.Name = 'Different - Updating';
    newOpportunity.Order_Accepted__c = Datetime.now();

    update newOpportunity;

    Addendum__c addendumCustomOption = new Addendum__c(
        RecordTypeId = '012V0000000CkQA', //Pre Priced Custom Option
        Opportunity__c = newOppId,
        Item_Pre_Priced_Description__c = 'a1eV00000004DNu',
        Reason__c = 'This is a reason',
        Item__c = 'This is an Item',
        Quantity__c = 1
    );

    Addendum__c addendumTierFeature = new Addendum__c(
        RecordTypeId = '012V0000000Cjks', //Tier Feature
        Opportunity__c = newOppId,
        Category__c = 'Countertops',
        Reason__c = 'This is a reason',
        Item__c = 'This is an Item',
        Quantity__c = 1
    );

    insert addendumCustomOption;
    insert addendumTierFeature;

    addendumCustomOption.Quantity__c = 2;
    addendumTierFeature.Quantity__c = 2;

    update addendumCustomOption;
    update addendumTierFeature;
    update newOpportunity;

    addendumTierFeature.To_IHMS__c = system.now();

    update addendumTierFeature;

    addendumTierFeature.From_IHMS__c = system.now();


    update addendumTierFeature;

  }
}

对于此事的任何帮助将不胜感激。我相信问题出在我测试审批流程开始的方式上。是否有一个特殊的测试功能?

【问题讨论】:

  • 该错误消息看起来可能被截断了。你确定这就是全部吗?
  • 不,就是这样。这是IDE中出现的错误。在日志前面有一点点文字,就像时间戳一样。我刚刚又跑了一次测试课,得到了同样的措辞。应该还有更多吗?
  • 是的,它通常会说“由”,然后是异常,例如“由空指针异常引起”。你能在错误之后发布接下来的 10 行左右的日志吗?
  • 或者只是粘贴很多
  • 好的,我创建了一个 pastebin:pastebin.com/avEU2AwZ。您可以在第 1505 行附近看到错误。现在我仔细查看它,它看起来可能是由于无法加载另一个测试类“AddOptionsTest”引起的。但是那个测试类是完全不相关的......

标签: triggers salesforce apex-code


【解决方案1】:

摆弄了一会儿后,我发现这个错误实际上与我的审批流程有关。我一直在挖掘错误日志,直到发现错误:caused by: System.DmlException: Process failed. First exception on row 0; first error: MANAGER_NOT_DEFINED, Manager undefined.: []。这句话表示在我的审批流程中没有为下一步定义任何人。

当我创建机会时,我没有设置所有者,并且不知何故,这创造了一个机会,它有所有者而没有经理。该附录也是在没有所有者/经理的情况下创建的。因此,当我尝试启动下一个审批流程时,没有经理可以将审批发送给并引发错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多