+ Start a Discussion

test class adderror


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
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.

if (....)
You also need to create a String variable MyMessage with a value.

Here are links to test methods for triggers:
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');
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