function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steve KucklincaSteve Kucklinca 

Why is my validation rule causeing my Apex class to fail?

I get this error related to a Validation rule when I try to run a test on my Apex class (below) The fields related to the rule are bold. What am I doing wrong? If approval fields (Account and Merchant Application) are 'Approved' when Opportunity stage is 'Signed' why would I get the error from my validation when it meets criteria to not be an error?

Error Message System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please contact Risk team for validation that the Merchant Application has been Approved: [StageName]
 
Stack Trace Class.MerchOppsRecordsTest.MO: line 42, column 1


@IsTest
public class MerchOppsRecordsTest { 
 
    public static testmethod void MO() {

  Account a=new account();
  a.name='testaccount';
  a.website='www.test.com';
  a.Base_Rate_Program__c='2.9%';
  a.socpl__Billing_Country__c='USA';
  a.Company_Description__c='testing';
  a.Incumbent_Processer__c='Startup';
  a.Current_Integration_Type__c='Hosted Pages';
  a.Primary_Investor_s_VC_s_Name_s__c='Self funded';
  a.Approval_Stage__c='Approved';
  
  //put all other mandatory fields on account
  insert a;
  
    Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Approved';
  ma.MA__c='null';
  ma.Principal_First_Name__c='Bruce';
  ma.Principal_Last_Name__c='Wayne';
  ma.Completed__c='Completed';
  
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
  
  Opportunity o=new opportunity ();
  o.name='opp1';
  o.stagename='Signed';
  o.closedate=date.parse('06/23/2014');
  o.Go_Live_Date__c=date.parse('07/16/2014');
  o.P_T_Date__c=date.parse('10/31/2014');
  o.Next_Step__c='test';
  o.type='New Business';
  o.leadsource='Get Started';
  
  insert o;
  
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
  
  insert mo;  
  
  ma = [select name from merchant_application__c where id = :ma.id];
update ma;

  o = [select name from Opportunity where id =:o.id];
  update o;
}
}
Here is the trigger I am trying to get code coverage for. When I turn off validation rules I get 75%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); 
    }
    
  
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
     
    for(Merchant_Application__c ma : Trigger.new){
        
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo); 
        }
    }
   
    insert mOps;  
    
}
David "w00t!" LiuDavid "w00t!" Liu
This all depends on how your validation rule is specifically written.

Most likely you somehow want an exception in your validation rule to allow for the scenario that your code is covering!  IE the validation rule catches when a user does actions, but the validation rule allows the input when the trigger does it.
Steve KucklincaSteve Kucklinca
I left you a post on your blog! Obviously I have chosen expert level for my first trigger how might I write this exception?