I have one question I want to write a trigger when OpportunityLineItem status is closed for every lineitem under the opportunity and update the opportunity status is closed
Please try with the below trigger and let me know further :
trigger OpportunityLineItemTrigger on OpportunityLineItem (after update) {
// Collect the Opportunity Ids related to the updated Opportunity Line Items
Set<Id> opportunityIds = new Set<Id>();
for (OpportunityLineItem oli : Trigger.new) {
opportunityIds.add(oli.OpportunityId);
}
// Query the related Opportunities and check the status of their Opportunity Line Items
List<Opportunity> opportunitiesToUpdate = new List<Opportunity>([
SELECT Id, (SELECT Id, Status FROM OpportunityLineItems)
FROM Opportunity
WHERE Id IN :opportunityIds
]);
// Update the Opportunities if all Opportunity Line Items are closed
List<Opportunity> opportunitiesToUpdateStatus = new List<Opportunity>();
for (Opportunity opp : opportunitiesToUpdate) {
Boolean allLineItemsClosed = true;
for (OpportunityLineItem oli : opp.OpportunityLineItems) {
if (oli.Status != 'Closed') {
allLineItemsClosed = false;
break;
}
}
if (allLineItemsClosed) {
opp.StageName = 'Closed';
opportunitiesToUpdateStatus.add(opp);
}
}
// Perform the update on the Opportunities with closed Opportunity Line Items
if (!opportunitiesToUpdateStatus.isEmpty()) {
update opportunitiesToUpdateStatus;
}
}
If this helps , please mark this as best answer. Thank you.
Please try with the below trigger and let me know further :
trigger OpportunityLineItemTrigger on OpportunityLineItem (after update) {
// Collect the Opportunity Ids related to the updated Opportunity Line Items
Set<Id> opportunityIds = new Set<Id>();
for (OpportunityLineItem oli : Trigger.new) {
opportunityIds.add(oli.OpportunityId);
}
// Query the related Opportunities and check the status of their Opportunity Line Items
List<Opportunity> opportunitiesToUpdate = new List<Opportunity>([
SELECT Id, (SELECT Id, Status FROM OpportunityLineItems)
FROM Opportunity
WHERE Id IN :opportunityIds
]);
// Update the Opportunities if all Opportunity Line Items are closed
List<Opportunity> opportunitiesToUpdateStatus = new List<Opportunity>();
for (Opportunity opp : opportunitiesToUpdate) {
Boolean allLineItemsClosed = true;
for (OpportunityLineItem oli : opp.OpportunityLineItems) {
if (oli.Status != 'Closed') {
allLineItemsClosed = false;
break;
}
}
if (allLineItemsClosed) {
opp.StageName = 'Closed';
opportunitiesToUpdateStatus.add(opp);
}
}
// Perform the update on the Opportunities with closed Opportunity Line Items
if (!opportunitiesToUpdateStatus.isEmpty()) {
update opportunitiesToUpdateStatus;
}
}
If this helps , please mark this as best answer. Thank you.
Please try with the below trigger and let me know further : If this helps , please mark this as best answer.
Thank you.
All Answers
Please try with the below trigger and let me know further : If this helps , please mark this as best answer.
Thank you.
Thank you for answer.
thanks,
gopikrishna.