I have custom field Invoice Date
in OpportunityLineitem
when this is left blank it should show error using trigger when trying to update Opportunity with Opportunity stagename
as closed won
.
I tried the following way:
trigger OpportunityStageWon on Opportunity(before insert, before update) {
set<Id> Oid = new set<id>();
for (Opportunity o: Trigger.new) {
Oid.add(o.id);
}
List<OpportunityLineItem> OLI = [Select id, name, Invoice_Date1__c from OpportunityLineItem where Opportunityid =: Oid];
for (Opportunity o: Trigger.New) {
for(OpportunityLineItem ol : OLI){
if (o.StageName == 'Closed Won' && ol.Invoice_Date1__c == null) {
o.StageName.addError('Your custom error message');
}
}
}
}