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
satheesh k 18satheesh k 18 

SObject row does not allow errors: Trigger.ProjectAlchemyFieldMandatory

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
 
Aman MalikAman Malik
Hi satheesh,
You can use the addError method for only those records that are avaliable in Trigger Context. Therefore you need to update the code block like below:
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');           
}
Give it a try and let me know in case any query.

Please like the answer and mark it as best if this helps.

Thanks,
Aman
 
satheesh k 18satheesh k 18
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
Aman MalikAman Malik
It seems the opportunity record is not in trigger context, therefore you can't use addError here.
You can use the addError method for only those records that are avaliable in Trigger Context.
Thanks,
Aman