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
Heather_HansonHeather_Hanson 

Apex Trigger for validation of object during initial approval request

I've completed the Developer Basic trail and the Apex lessons, but I only have minimal experience using Apex Triggers and Classes and have been struggling all afternoon to figure out the best and most user friendly way to do this.

I need to validate fields on the Opportunity object when someone clicks the "Submit for Approval" button related to an approval process.  Since validation rules do not work with field updates in the approval process, I though I could do an Apex Trigger instead using add.error method.

I have been working on my trigger and listing all the possible things to validate, but I ran into the issue where it only throws one error message at a time.  So I investigated further and now I'm not sure if I need to create a visualforce page or just adjust my trigger to fix it.  I would like to do visualforce as well since I don't like the screen that pops up with the error message.  I'm comfortable with Visual force, but I'm not sure how to tie in my trigger and do I need to write a separate controller for the Visualforce page.

Any guidance would be greatly appreciated!!

Here is my trigger...again I'm new so please excuse me if it is totally the wrong way to go about this...
 
trigger OppResiApprovalVali on Opportunity (before update) {

  for(Opportunity o : trigger.new)
  {  if(o.RecordTypeId=='0121H0000019Lna' && o.StageName=='PRE-PROVISIONING' && o.Internet_Requested__c==TRUE)
   {     
        if(o.Existing_Internet_Service__c==null)      
    {
      o.Existing_Internet_Service__c.addError('Please make a selection for Existing Internet Service.');
    }
       
       if(o.Existing_Internet_Provider__c==null && o.Existing_Internet_Service__c=='Yes')
       {
           o.addError ('Please make a selectcion for Existing Internet Provider.');           
       }     
          if(o.Existing_Internet_Provider__c=='Bell' && o.If_Bell_is_internet_on_line_or_Dry_Loop__c==null)
       {
           o.addError ('Please make a selectcion for If Bell, is internet on line or Dry Loop');           
       }
             if(o.Router_Option__c==null)
       {
           o.addError ('Please make a selectcion for Router Option');           
       }
   }
   
   {if(o.RecordTypeId=='0121H0000019Lna' && o.StageName=='PRE-PROVISIONING' && o.Home_Digital_Line_Requested__c==TRUE)
   {
       if(o.Telephone_to_be_transferred__c==null)      
    {
      o.addError('Please provide a response to Telephone Number to be Transferred.');
    }
       if(o.Current_Provider__c==null)      
    {
      o.addError('Please make a selection for Current Provider');
    }
       if(o.Call_Forward_Not_Reachable__c==null)      
    {
      o.addError('Please provide a response to Call Forward Not Reachable Number');
    }
      
       
   }
       
   }
     
   
   }

}