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
Gopikrishna DasariGopikrishna Dasari 

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

Best Answer chosen by Gopikrishna Dasari
SubratSubrat (Salesforce Developers) 
Hello Gopikrishna ,

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.
 

All Answers

SubratSubrat (Salesforce Developers) 
Hello Gopikrishna ,

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.
 
This was selected as the best answer
Gopikrishna DasariGopikrishna Dasari
Hi Subrat, 

Thank you for answer.

thanks,
gopikrishna.