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
Scott LandesScott Landes 

How to pull in fields from record in Apex Trigger

I'm trying to figure out how to create an Apex trigger that fires different classes depending on the Opportunity Type. I added what I thought would work below, but it doesn't seem to be pulling the fields like I thought it would. Can someone show me how to pull in values from the updated record? Any help is greatly appreciated as I'm trying to get this fixed as soon as possible.

trigger OrderRollupSummaryTrigger on Order_Location_Package__c (after insert, after update, after delete, after undelete) {
    
	if (Order_Location_Package__c.Order_Sheet__c.Opportunity__c.Type = 'NEW' || Order_Location_Package__c.Order_Sheet__c.Opportunity__c.Type = 'Renewal') {
	    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
	        OrderLocationRollupSummary.rollupOrderPackages(trigger.new);
	    }
	    else if (trigger.isAfter && trigger.isDelete) {
	        OrderLocationRollupSummary.rollupOrderPackages(trigger.old);
		}
	}

	else if (Order_Location_Package__c.Order_Sheet__c.Opportunity__c.Type != 'NEW' || Order_Location_Package__c.Order_Sheet__c.Opportunity__c.Type != 'Renewal') {
	    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
	        OrderLocationRollupSummaryOther.rollupOrderPackages(trigger.new);
	    }
	    else if (trigger.isAfter && trigger.isDelete) {
	        OrderLocationRollupSummaryOther.rollupOrderPackages(trigger.old);
		}
	}
}
 

Thanks!

Scott LandesScott Landes

After quite a lot of reading, I found the best way to do this. Specifically with a formula field (to avoid SOQL queries in the trigger)

 

trigger OrderLocationRollupSummaryTrigger on Order_Location_Package__c (after insert, after update, after delete, after undelete) {
	
	List<Order_Location_Package__c> orderLocationPackageRenewalList = new list<Order_Location_Package__c>();
	List<Order_Location_Package__c> orderLocationPackageOtherList = new list<Order_Location_Package__c>();

	//Check and add the records to list based on condition
    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
    	for(Order_Location_Package__c olp: trigger.new)
    		if(olp.Opportunity_Type__c == 'NEW' || olp.Opportunity_Type__c == 'Renewal')
    			orderLocationPackageRenewalList.add(olp);
    		else
    			orderLocationPackageOtherList.add(olp);
    }
    else if (trigger.isAfter && trigger.isDelete) {
    	for(Order_Location_Package__c olp: trigger.old)
    		if(olp.Opportunity_Type__c == 'NEW' || olp.Opportunity_Type__c == 'Renewal')
    			orderLocationPackageRenewalList.add(olp);
    		else
    			orderLocationPackageOtherList.add(olp);		
    }


    //Send the records for processing to RollupSummary class
    if(orderLocationPackageRenewalList!=null && !orderLocationPackageRenewalList.isEmpty())
        OrderLocationRollupSummary.rollupOrderPackages(orderLocationPackageRenewalList);

    //Send the records not meeting the criteria to other RollupSummary class
    if(orderLocationPackageOtherList!=null && !orderLocationPackageOtherList.isEmpty())
        OrderLocationRollupSummaryOther.rollupOrderPackages(orderLocationPackageOtherList);

}