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
imrohitimrohit 

hey guys i have a requirement on trigger

Create a trigger, when an OpportunityLineItem is created (or is updated and the PricebookEntry has changed) set OpportunityLineItem.Can_Be_Recurring__c to PricebookEntry.Product2.Can_Be_Recurring__c.

 how can i do this 
please help
thanks
Steven NsubugaSteven Nsubuga
Try this
trigger setRecurring on OpportunityLineItem (before insert, before update){

	Set<Id> pricebookEntryIds = new Set<Id>();
	if (Trigger.isInsert) {
		for (OpportunityLineItem lineItem : trigger.new) {
			pricebookEntryIds.add(lineIte.PriceBookEntryID);
		}
	}
	
	if (Trigger.isUpdate) {
		for (OpportunityLineItem lineItem : trigger.new) {
			if (lineItem.PriceBookEntryID != trigger.oldMap.get(lineItem.Id).PriceBookEntryID) {
				pricebookEntryIds.add(lineItem.PriceBookEntryID);
			}
		}
	}
	
	Map<Id, PricebookEntry> cs = new Map<Id, PricebookEntry>([SELECT Id, Product2.Can_Be_Recurring__c FROM PricebookEntry WHERE Id IN:pricebookEntryIds]);
	
	if (cs.size() > 0) {
		for (OpportunityLineItem lineItem : trigger.new) {
			lineItem.Can_Be_Recurring__c = cs.get(lineItem.PriceBookEntryID).Product2.Can_Be_Recurring__c;				
		}
	}
}