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
brianspatsobrianspatso 

Test class failing on Deployment Validation

I am fairly new to Apex classes and triggers, but I am finding my feet with the code I have recently written.

 

Having written and tested the code I am now trying to deploy it in the main org, but I get an error when I try to validate it.

 

The most frustrating thing is that the validation error is being triggered by some code previously written by a developer - it seems that some validation rules, introduced since the developer wrote his code, are causing the problem.

 

What I don't understand is that I seem to have fixed the problem but am still getting the error message.

 

Here is the error:

	Failure Message: "System.DmlException: Update failed. First exception on row 0 with id 0062000000JZehyAAD; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter an email address on the contact page before closing deal.: []", Failure Stack Trace: "Class.TestTriggers.Test_AllTriggers: line 82, column 9 External entry point"

 And here is the test class that seems to be causing the issue:

@IsTest private class TestTriggers {

     static testMethod void Test_AllTriggers(){
    
    //Create an SRT for user 2020 mgt in Aug
        SRT__c srt = New SRT__c(Name='Test SRT',User__c='00520000001BboG',Date__c=System.Today(),New_Business_Target__c=10000,Online_Target__c=1000);
        insert srt;
        
        //Integer lastMonth = Date.month(srt.Date__c)-1;
        Date d = srt.Date__c;//Date.newInstance(Date.year(srt.Date__c), , pInteger)
        d  = d.AddMonths(-1);
        SRT__c srtLastMonth = New SRT__c(Name='Test SRT',User__c='00520000001BboG',Date__c=d,New_Business_Target__c=10000,Online_Target__c=1000);
        insert srtLastMonth;
        
        srt.Last_Month_s_SRT__c=srtLastMonth.id;
        update srt;
        SRT__c srtTest = [Select OwnerId,User__c from SRT__c where Id = :srt.id];
        
                
        //Add an account
        User u = [select id from user where lastname = 'burns' and isactive=true limit 1];
        Account acc = New Account(Name='Adam test',OwnerId=u.id,Billingstreet='asa',BillingPostalCode='test',Billingcity='test',billingcountry='United Kingdom');
        
        Insert acc;
        //Get delegate record type Id
        List<Schema.RecordTypeInfo> contactRecordTypeInfos = Contact.SobjectType.getDescribe().getRecordTypeInfos();
        RecordType rt;
        
        for(Schema.RecordTypeInfo item : contactRecordTypeInfos){
            
            if(item.getName()=='Delegate'){
                rt = New RecordType(id=item.getRecordTypeId()) ;    
            }
        }
        
        //Add a contact
        Contact con = New Contact(AccountID= acc.Id,RecordTYpeId=rt.id,Lastname='Tester',Email='test@test.com');
        insert con;
        String conOwnerId = [select ownerid from contact where id = :con.id].ownerid;
        
        System.debug('Account Owner ID' + acc.OwnerId);
        System.debug('Contact Owner ID' + conOwnerId);
        System.assert(acc.Ownerid <> conOwnerId);
        
        //Add an opportunity
        List<Opportunity> oppArray = New List<Opportunity>();
        oppArray.add(New Opportunity(AccountId=acc.Id,Name='Opp Test',CloseDate=System.today(),Stagename='No ANswer'));
        oppArray.add(New Opportunity(AccountId=acc.Id,Name='Opp Test',CloseDate=System.today(),Stagename='No ANswer'));
        //Opportunity opp = New Opportunity(AccountId=acc.Id,Name='Opp Test',CloseDate=System.today(),Stagename='No ANswer');
        Opportunity opp = New Opportunity(AccountId=acc.Id,Name='Opp Test',CloseDate=System.today(),Stagename='No ANswer');
        insert opp;
        //insert oppArray;
        list<id> oppId = New list<id>();
        
        for(integer i = 0;i<oppArray.size();i++){
            oppId.add(oppArray[i].id);
        }
       System.debug('|||||' + oppId);
        Opportunity oppTest = [select name,deal_ref__c from opportunity where id = :opp.Id];
        //oppTest[0].CDSOpportunity__c =true;
        //oppTest[1].CDSOpportunity__c =true;
        //update oppTest;
        System.debug('@@@@@@' + oppTest);
        System.assert(oppTest.Name == oppTest.Deal_Ref__c);
        
        // Add an item
        
        //create an event/Issue
        Issue_Year__c ei = New Issue_Year__c(Name='Test',Description__c = 'Test Desc',Product__c='CFO',Date__c=System.today(),Mag_or_Event__c='Mag',Online__c='No', Copy_Deadline__c=System.today());
        insert ei;
        List<Schema.RecordTypeInfo> itemRecordTypeInfos = Item__c.SobjectType.getDescribe().getRecordTypeInfos();
        
        for(Schema.RecordTypeInfo item : itemRecordTypeInfos){
            
            if(item.getName()=='Delegate'){
                rt = New RecordType(id=item.getRecordTypeId()) ;    
            }
        }
        opp.StageName = 'Closed - Awaiting Approval';
        opp.Invoice_Address__c = 'Company';
        opp.Address_Invoice_to_contact_above__c = 'yes';
        update opp;
        Item__c item = New Item__c(Payment_terms__c='7 days',Probability__c=100,RecordTYpeId = rt.Id,Attendee__c=con.id,Name='Item Test',Opportunity__c=opp.id,Item_Sold__c='delegate',Event_Issue__c=ei.id);
        insert item;
        
        Item__c itemTest = [Select Line_Number__c from Item__c where id = :item.id];
        System.assert(itemTest.Line_Number__c == 1);
        Integer contactAttendanceCounter = [select count() from ContactAttendance__c where Contact__c = :con.id];
        System.Assert(contactAttendanceCounter==1);
        
        //Change the opportunity to be closed won
        opp.StageName = 'Closed test';
        //opp.Probability = 100;
        Update opp;
        
        opp.StageName = 'Closed Won';      
        Update opp;
        
       item.Overide_Deal_Date__c = d.addYears(2);
       
       update item;
        
        //opp.CDSOpportunity__c = true;
        //update opp;
        opp.StageName = 'Closed test';
        update opp;
        delete item;
        delete opp;
        
        
    }
}

 As you can see, I have set the email address on the contact page here:

        //Add a contact
        Contact con = New Contact(AccountID= acc.Id,RecordTYpeId=rt.id,Lastname='Tester',Email='test@test.com');
        insert con;

So what am I doing wrong?

 

Thanks for your help.

Best Answer chosen by Admin (Salesforce Developers) 
iBr0theriBr0ther

Quick fix: deactivate the related validation rules. and deploy again. After that, enable those validation.

 

Regards,

All Answers

iBr0theriBr0ther

Quick fix: deactivate the related validation rules. and deploy again. After that, enable those validation.

 

Regards,

This was selected as the best answer
brianspatsobrianspatso

Thanks for that - I should have thought of it!

 

I have now deployed successfully!

 

:smileyvery-happy: