+ Start a Discussion
Saurav nirwal 21Saurav nirwal 21 

Related Opportunity

. Create a new PickList “Custom Status” in Opportunity object.(New,Open,Close,Reset) values.When this field changed and value is “Reset” now then delete all associated products(opp. Lines)
with related Opportunity
Jigar.LakhaniJigar.Lakhani
Hello,

Below is your trigger to achieve your requirement.

Apex Trigger
Trigger OpportunityTrigger on Opportunity(AFTER UPDATE){
	
	// After Update Trigger
	if (Trigger.IsAfter && Trigger.IsUpdate) {
		Set<Id> setOpporutnityId = new Set<Id>();
		for (Opportunity objOpportunity:Trigger.New) {
			if (objOpportunity.Custom_Status__c != null && objOpportunity.Custom_Status__c == 'Reset' && objOpportunity.Custom_Status__c != Trigger.OldMap.get(objOpportunity.Id).Custom_Status__c) {
				setOpporutnityId.Add(objOpportunity);
			}
		}
		
		if (setOpporutnityId != null && setOpporutnityId.size() > 0) {
			List<OpporutnityLineItem> listDeleteLineItems = new List<OpporutnityLineItem>();
			for (Opportunity objOpportunity:[SELECT Id,Name,(SELECT Id FROM OpporutnityLineItems) FROM Opportunity WHERE Id In:setOpporutnityId]) {
				if (objOpportunity.OpporutnityLineItems != null && objOpportunity.OpporutnityLineItems.size() > 0) {
					listDeleteLineItems.AddAll(objOpportunity.OpporutnityLineItems);
				}
			}
			if (listDeleteLineItems != null && listDeleteLineItems.size() > 0) {
				DELETE listDeleteLineItems;
			}
		}
	}
	
}

Thanks & Regards,
Jigar(pateljb90@gmail.com)

 
Jigar.LakhaniJigar.Lakhani
Please update line - 8 which is adding Id of opporutnity records in set.

Replace below line
setOpporutnityId.Add(objOpportunity);
With
setOpporutnityId.Add(objOpportunity.Id);

Thanks & Regards,
Jigar(pateljb90@gmail.com)


 
AmrenderAmrender

Hi Mr Salesforce

Just an update to your code. From line no 13 to 22

Trigger OpportunityTrigger on Opportunity(AFTER UPDATE){
    
    // After Update Trigger
    if (Trigger.IsAfter && Trigger.IsUpdate) {
        Set<Id> setOpporutnityId = new Set<Id>();
        for (Opportunity objOpportunity:Trigger.New) {
            if (objOpportunity.Custom_Status__c != null && objOpportunity.Custom_Status__c == 'Reset' && objOpportunity.Custom_Status__c != Trigger.OldMap.get(objOpportunity.Id).Custom_Status__c) {
                setOpporutnityId.Add(objOpportunity);
            }
        }
        
        if (setOpporutnityId != null && setOpporutnityId.size() > 0) {
            List<OpporutnityLineItem> listDeleteLineItems = new List<OpporutnityLineItem>();
            for (Opportunity objOpportunity:[SELECT Id,Name,(SELECT Id FROM OpporutnityLineItems) FROM Opportunity WHERE Id In:setOpporutnityId]) {
                if (objOpportunity.OpporutnityLineItems != null && objOpportunity.OpporutnityLineItems.size() > 0) {
                    listDeleteLineItems.AddAll(objOpportunity.OpporutnityLineItems);
                }
            }
            if (listDeleteLineItems != null && listDeleteLineItems.size() > 0) {
                DELETE listDeleteLineItems;
            }
        }

   delete [SELECT Id FROM OpporutnityLineItems where OpportunityId IN :setOpporutnityId];
    }
    
}