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
SF Beginner 2019SF Beginner 2019 

How to Create a trigger/Process builder wherein if a record from opportunityline item was deleted it will create a record?

I am planning to create a history or audit trail on Opportunity Product or Line Item, I am currently stuck in deleting and creating a history when deleted, can it be done using the process builder wherein when a record is deleted it will create a record on opp prod history object. I have created update and create, how can I achieve these on delete and if it is in apex how can I achieve these?
Andrew GAndrew G
Hi 
Process Builder for delete doesnt work at this stage
https://success.salesforce.com/ideaView?id=08730000000DlPBAA0

Going to Trigger, here is an example of trigger and handler that works on updates and deletes
This particular example updates a field on the parent with a count, so you should be able to extrapolate/modify to solve your challenge.
Trigger
trigger testTrigger on SkillRequirement (after insert, after update, after delete) {

	System.debug('In Trigger - testTrigger');

	if ( trigger.isAfter ) {
		if ( trigger.isInsert ) {
			System.debug('In Trigger - about to call on after insert');			
			SkillRequirementTriggerHandler.OnAfterInsert( trigger.new );
		} else if ( trigger.isUpdate ) {
				System.debug('In Trigger - about to call on after update');			
				SkillRequirementTriggerHandler.OnAfterUpdate( trigger.old, trigger.new, trigger.oldMap, trigger.newMap );
			} else if (trigger.isDelete ) {
				System.debug('In Trigger - about to call on after delete');			
				SkillRequirementTriggerHandler.OnAfterDelete( trigger.old, trigger.oldMap );
			}
	}
}
Handler
public with sharing class SkillRequirementTriggerHandler {

	//update the Count Value when new records are inserted from trigger
	public static void OnAfterInsert( List<SkillRequirement> newRecords ) {
		 updateSiteInductionCount(newRecords);
	}

	//update the Count Value when records are update from the trigger
	public static void onAfterUpdate( List<SkillRequirement> oldRecords,List<SkillRequirement> updatedRecords, Map<ID,SkillRequirement> oldMap, Map<ID,SkillRequirement> newMap ) {
		updateSiteInductionCount(updatedRecords);
	}
 
	//update the Count Value when records are update from the trigger
	public static void onAfterDelete( List<SkillRequirement> oldRecords, Map<ID,SkillRequirement> oldMap ) {
		updateSiteInductionCount(oldRecords);
	}

 	//
	private static void updateSiteInductionCount (List<SkillRequirement> newRecords) {

		List<Id> woIDs = new List<Id>();
		List<WorkOrder> toUpdate = new List<WorkOrder>();
		
		for (SkillRequirement sr : newRecords) {
			if (String.valueof(sr.RelatedRecordId).startsWith('0WO')) {
				woIDs.add(sr.RelatedRecordId);	
			} 
		}
	
		AggregateResult[] counts = [SELECT RelatedRecordId, sum(SiteInductionCheck__c)sic FROM SkillRequirement WHERE RelatedRecordId IN :woIds GROUP BY RelatedRecordId];
	    
		if (counts.isEmpty()) {   //to handle where all related skills have been deleted
			for (ID woid : woIDs ) {
				WorkOrder tempWO = new WorkOrder(Id = woid, SiteInductionCount__c = 0);
		    	toUpdate.add(tempWO);
			}	
		} else {
		    for (AggregateResult ar : counts ) {
		    	WorkOrder tempWO = new WorkOrder(Id=string.valueof(ar.get('RelatedRecordId')), SiteInductionCount__c=integer.valueof(ar.get('sic')));

		    	toUpdate.add(tempWO);
			}
	    }
	    update toUpdate;
	}

}

Regards
Andrew