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
vajralavajrala 

I faced probloms while writing the Dynamic Approval Process based on the Apex and Trigger

I got error:-  Error: Invalid Data. Review all error messages below to correct your data.
Attempt to de-reference a null object

while writing the following code.

trigger AutomateApprove on Opportunity(After insert, After update)
{

    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
     try
     {
        //Insure that previous value not equal to current, else if there is any field update on approval process action
        //there will be recurrence of the trigger.

        if(Trigger.new[i].Next_Step__c == 'Submit' && Trigger.old[i].Next_Step__c != 'Submit')
        {
           submitForApproval(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Approve' && Trigger.old[i].Next_Step__c != 'Approve')
        {
             approveRecord(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Reject' && Trigger.old[i].Next_Step__c != 'Reject')
        {
             rejectRecord(Trigger.new[i]);
        }
     }catch(Exception e)
     {
         Trigger.new[i].addError(e.getMessage());
     }
    }
please give me solution
Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Trigger.old is only populated for update and delete operations.  In your case the trigger is firing after an insert (as well as an update) so lines such as:

 

if(Trigger.new[i].Next_Step__c == 'Submit' && Trigger.old[i].Next_Step__c != 'Submit')

 

will throw an error.  I reckon you need something like:

 

trigger AutomateApprove on Opportunity(After insert, After update)
{

    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
     try
     {
        //Insure that previous value not equal to current, else if there is any field update on approval process action
        //there will be recurrence of the trigger.

        if (Trigger.new[i].Next_Step__c == 'Submit' &&  
            ( (trigger.isInsert) || (Trigger.old[i].Next_Step__c != 'Submit') )
        {
           submitForApproval(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Approve' && 
            ( (trigger.isInsert) || (Trigger.old[i].Next_Step__c != 'Approve') )
        {
             approveRecord(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Reject' && 
                ( (trigger.isInsert) || (Trigger.old[i].Next_Step__c != 'Reject') )
        {
             rejectRecord(Trigger.new[i]);
        }
     }catch(Exception e)
     {
         Trigger.new[i].addError(e.getMessage());
     }
    }

 

All Answers

bob_buzzardbob_buzzard

Trigger.old is only populated for update and delete operations.  In your case the trigger is firing after an insert (as well as an update) so lines such as:

 

if(Trigger.new[i].Next_Step__c == 'Submit' && Trigger.old[i].Next_Step__c != 'Submit')

 

will throw an error.  I reckon you need something like:

 

trigger AutomateApprove on Opportunity(After insert, After update)
{

    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
     try
     {
        //Insure that previous value not equal to current, else if there is any field update on approval process action
        //there will be recurrence of the trigger.

        if (Trigger.new[i].Next_Step__c == 'Submit' &&  
            ( (trigger.isInsert) || (Trigger.old[i].Next_Step__c != 'Submit') )
        {
           submitForApproval(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Approve' && 
            ( (trigger.isInsert) || (Trigger.old[i].Next_Step__c != 'Approve') )
        {
             approveRecord(Trigger.new[i]);
        }
        else if(Trigger.new[i].Next_Step__c == 'Reject' && 
                ( (trigger.isInsert) || (Trigger.old[i].Next_Step__c != 'Reject') )
        {
             rejectRecord(Trigger.new[i]);
        }
     }catch(Exception e)
     {
         Trigger.new[i].addError(e.getMessage());
     }
    }

 

This was selected as the best answer
vajralavajrala

Thank you very much.............

Now it working

vajralavajrala

ok, Its working successfully .But i faced problems while writing the test class for this trigger cover the code .

Please give me the code for the test class to cover the code .Please....