+ Start a Discussion
Bob.390672021994282E12Bob.390672021994282E12 

test class adderror

Hello,

I am having an issue with my test class adderror on a trigger.

trigger Validation on ObjectA (before update) {
 
        for(ObjectA lstVAdd :  trigger.new){   

            if(lstVAdd.Supplier__c==null && lstVAdd.Status__c=='Closed' && lstVAdd.Comments__c ==null){
        Trigger.new[0].Supplier.addError(My Message);
           
                       }
        }
    }

Thank you
B2000B2000
Your code above is not a test class.  It is a trigger.  

In your trigger, replace Trigger.new[0] with 1stVAdd as you are iterating over the ObjectA with the for loop using the 1stVAdd variable.

Try
if (....)
1stVAdd.Supplier.addError(MyMessage);
You also need to create a String variable MyMessage with a value.

Here are links to test methods for triggers:
http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm
Vamshi Reddy BandaruVamshi Reddy Bandaru
Hi Brian,
Your trigger will only throw error on first record. because  you are using Trigger.new[0].supplier. and also your supplier should be appened with '__C' since its a custom field. if you want to throw an error try adding 1stVAdd.Supplier__C.addError('MyMessage');
trigger Validation on ObjectA__C (before update) {

        for(ObjectA__C lstVAdd :  trigger.new){

                    if(lstVAdd.Supplier__c==null && lstVAdd.Status__c=='Closed' && lstVAdd.Comments__c ==null){
                           1stVAdd.Supplier__C.addError('My Message');
        
                     }
        }
}
Bob.390672021994282E12Bob.390672021994282E12
Thank you for the reference Vamshi. Typically I have written triggers that inserted or VF that did something. I havnt wrote a test class that just displayed an error message. I will see if the above can provide a solution to the simple test class for adderror