You need to sign in to do that
Don't have an account?
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
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
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: 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
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
You can use the addError method for only those records that are avaliable in Trigger Context.
Thanks,
Aman