Skip to main content
Hi team,

I have written below code for whenever account checkbox field is checked, if we create new opportunity under this account service field is mandatory on opportunity.

trigger ProjectAlchemyFieldMandatory on Opportunity (after insert,after update) 

{

    Set<Id> Accids=new Set<Id>();

    if(trigger.isInsert || trigger.isUpdate)

    {

        for(Opportunity Opp:Trigger.new)

        {          

           Accids.add(opp.Accountid); 

           system.debug('@@@@@Accids'+Accids);          

        }

    }

    

    List<Account> lisAcc=[select id,Checkbox__C,(select id,Service__C from opportunities) from account where id in:Accids];

    for(Account acc: lisAcc)

    {

        system.debug('Checkbox__C'+acc.Checkbox__C);

        if(acc.Checkbox__C==true)

        {

            for(Opportunity op:acc.opportunities)

            {

                system.debug('Service__C for loop'+op.Service__C );

                if(op.Service__C==null || op.Service__C=='')

                {

                    system.debug('@@@@@Service__C'+op.Service__C);

                   

                    op.adderror('Please select');

               

                

                }

            }

        }

    }    

}

But i am getting below error when i create new record on opportunity object.

ProjectAlchemyFieldMandatory: execution of AfterInsert caused by: System.FinalException: SObject row does not allow errors: Trigger.ProjectAlchemyFieldMandatory: line 27, column 1

Please help me

Thanks

Satheesh k18

 
3 Antworten
  1. 15. Nov. 2017, 05:09
    Hi Aman,

    trigger ProjectAlchemyFieldMandatory on Opportunity (after insert,after update) 

    {

        Set<Id> Accids=new Set<Id>();

        if(trigger.isInsert || trigger.isUpdate)

        {

            for(Opportunity Opp:Trigger.new)

            {          

               Accids.add(opp.Accountid); 

               system.debug('@@@@@Accids'+Accids);          

            }

        }

        

        List<Account> lisAcc=[select id,Checkbox__C,(select id,Service__C from opportunities) from account where id in:Accids];

        for(Account acc: lisAcc)

        {

            system.debug('Checkbox__C'+acc.Checkbox__C);

            if(acc.Checkbox__C==true)

            {

                for(Opportunity op:acc.opportunities)

                {

                    system.debug('Service__C for loop'+op.Service__C );

                    if(op.Service__C==null || op.Service__C=='')

                    {

                        system.debug('@@@@@Service__C'+op.Service__C);

                        Opportunity actualRecord = Trigger.newMap.get(op.Id);

                        actualRecord.adderror('Please select');  

                       

               

                    }

                }

            }

        }

        

        

    }

    I am getting this error when i create a new record in opportunity.

    execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.ProjectAlchemyFieldMandatory: line 28, column 1

    Trigger.newMap.get(op.Id);--> this line gives null value

    Thanks

    Satheesh K
0/9000