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
naidu@4naidu@4 

I need to display error message on Parent when child field is having null value.

I am having an custom filed x in child object if it is null and user is updating stage value on opportunity it needs to throe error. And I wrote below error and it is throwing error Sobject rows not allow errors

trigger UpdateOpp on Opportunity (before update) 
{
//Set<ID> oppid = new Set<ID>();
List<Id> oid = new List<ID>();
for(Opportunity opp:trigger.old)
{
oid.add(opp.id);
}

List<Opportunitylineitem> oppl = [select id, name, offerings__c from opportunitylineitem where Offerings__c = null AND opportunityId IN:oid];

for(opportunity opp1:trigger.old)
{
for(OpportunityLineItem opp:oppl)
{
if(opp.offerings__c==null)
{
opp.addError('Please update Opportunityproduct');
}
}
}
Best Answer chosen by naidu@4
Jainam ContractorJainam Contractor
Hi Naidu,

Please add the following Map definition before the SOQL query:

Map<Id, Opportunity> OppMap = Trigger.NewMap;

and in the error statement add the below field:

OppMap.get(opp1.id).addError('Please update Opportunityproduct');

Your trigger should look something like this:


trigger UpdateOpp on Opportunity (before update) 
{
//Set<ID> oppid = new Set<ID>();
List<Id> oid = new List<ID>();
for(Opportunity opp:trigger.old)
{
oid.add(opp.id);
}
Map<Id, Opportunity> OppMap = Trigger.NewMap;
List<Opportunitylineitem> oppl = [select id, name, offerings__c from opportunitylineitem where Offerings__c = null AND opportunityId IN:oid];

for(opportunity opp1:trigger.old)
{
for(OpportunityLineItem opp:oppl)
{
if(opp.offerings__c==null)
{
OppMap.get(opp1.id).addError('Please update Opportunityproduct');
//opp.addError('Please update Opportunityproduct');
}
}
}

Highlighted statements in BOLD are the changes that you should do in your original trigger.

Please check and let me know if it helps and marks as the solution if it solves your problem.

Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com

All Answers

Jainam ContractorJainam Contractor
Hi Naidu,

You have written trigger on Opportunity and you are trying to throw error on the OpportunityLineItem. Your requirement is to throw error on the Opportunity but you are trying to throw error on the Child Obj.

You got confused between the Alias for Opportuntiy and OppLineItem

Alias for Opportuntiy : opp1
Alias for OppLineItem : opp


Change this statement opp.addError('Please update Opportunityproduct'); to opp.addError('Please update Opportunityproduct');

This will solve your problem.

You might need to add one more condition in your IF condition i.e opp.OpportunityId == opp1.Id

To throw error only on those Opportunity where the Custom Field on OppLineItem is NULL.

Please let me know if you need more assistance and if this solves your problem mark it as the Solution and close the question.

Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
Shiva RajendranShiva Rajendran
Hi Naidu,
add.Error is to make the current record update or create not to commit in database.
So for Trigger in opportunity, you can add error to opportunity records you are changing not already existing records.
One small update to your code can be
Have a map of opportunity  and its related opportunitylineitem in list
Add the error to the opportunity record if any of the opprotunitylineitem fails.

Thanks and Regards,
Shiva RV
naidu@4naidu@4
Hi Jainam Contractor, 

I have updated theat line to Opp1, but still receving same error "Sobject rows not allow errors".
Shiva RajendranShiva Rajendran
Hey Naidu,
List<Opportunity> allOpp=[select id from opportunity where id in:Trigger.new];
have this line before
for(Opportunity opp:trigger.old)
{
oid.add(opp.id);
}

and work on that allOpp . add the error to the allOpp list data.

Thanks and Regards,
Shiva RV
Jainam ContractorJainam Contractor
Hi Naidu,

Please add the following Map definition before the SOQL query:

Map<Id, Opportunity> OppMap = Trigger.NewMap;

and in the error statement add the below field:

OppMap.get(opp1.id).addError('Please update Opportunityproduct');

Your trigger should look something like this:


trigger UpdateOpp on Opportunity (before update) 
{
//Set<ID> oppid = new Set<ID>();
List<Id> oid = new List<ID>();
for(Opportunity opp:trigger.old)
{
oid.add(opp.id);
}
Map<Id, Opportunity> OppMap = Trigger.NewMap;
List<Opportunitylineitem> oppl = [select id, name, offerings__c from opportunitylineitem where Offerings__c = null AND opportunityId IN:oid];

for(opportunity opp1:trigger.old)
{
for(OpportunityLineItem opp:oppl)
{
if(opp.offerings__c==null)
{
OppMap.get(opp1.id).addError('Please update Opportunityproduct');
//opp.addError('Please update Opportunityproduct');
}
}
}

Highlighted statements in BOLD are the changes that you should do in your original trigger.

Please check and let me know if it helps and marks as the solution if it solves your problem.

Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
This was selected as the best answer
naidu@4naidu@4
Thnak you Jainam Contractor.