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
TeashaTeasha 

Apex Trigger Help

Can someone help me with the code for a trigger that rolls up opportunity values into a custom object?  For example:

 

Custom Object  = Forecast_C

 

Custom Object Field  to update = Q1_Bookings_c

 

The information will come from

 

Opportunity Object Fields

 

Closed_To_Date_C + (Opportunities at the stage Closed Won) - With a close date in current FQ

 

Any help?

 

Best Answer chosen by Admin (Salesforce Developers) 
jbroquistjbroquist

If I'm understanding you correct then this trigger should only need to fire from an Opportunity when it goes Closed Won. It will then increment the number in the Q*_Bookings__c field based on which quarter it falls in within the fiscal calendar.

 

You will definitely need to write some additional business logic where I highlighted in red below, but this should get you started. I've commented up the code to help you understand how the logic flows.

 

trigger TestTrigger on Opportunity (after insert, after update) 
{
	//create set of forecast records ids to query
	Set<Id> forecastIds = new Set<Id>();
	//create list of won opportunities
	Opportunity[] wonOpportunities = new Opportunity[]{};

	for(Opportunity o : Trigger.new)
	{
		//check if the opportunity is related to a forecast record
		if(o.Forecast__c != null)
		{
			//if the opportunity is being inserted and is marked as Won add to
			//the won opportunity map 
			if(Trigger.isInsert && o.IsWon = true)
			{
				forecastIds.add(o.Forecast__c);
				wonOpportunities.add(o);
			}
			//if the opportunity is being changed to Won
			else if(Trigger.isUpdate && o.IsWon && o.IsWon != Trigger.oldMap.get(o.Id).IsWon)
			{
				forecastIds.add(o.Forecast__c);
				wonOpportunities.add(o);
			}
		}
	}

	//query forecast records to update
	Map<Id, Forecast__c> forecastMap = new Map<Id, Forecast__c>([SELECT Id, Q1_Bookings__c, Q2_Bookings__c, Q3_Bookings__c, Q4_Bookings__c FROM Forecast__c WHERE Id IN :forecastIds]);

	//iterate through the won opportunities and increment the quarterly bookings count
	for(Opportunity o : wonOpportunities)
	{
		//TODO: Write additional logic to determine which quarter it is based on your
		//companies fiscal calendar
		{
			//increment the quarterly bookings count
			Forecast__c f = forecastMap.get(o.Forecast__c);
			f.Q1_Bookings__c = f.Q1_Bookings__c + 1;
		}
	}

	if(forecastMap.size() > 0) update forecastMap.values();
}

 

Its also worth noting that you may want to consider expanding the trigger to update the related forecast records on delete and undelete as well.

 

Let us know if you have any questions!

All Answers

imutsavimutsav
what is the relationship between Opportunity and your custom Object. is there a lookup field etc ?
TeashaTeasha

Yes, there is a look up on opportunity to the custom object

jbroquistjbroquist

If I'm understanding you correct then this trigger should only need to fire from an Opportunity when it goes Closed Won. It will then increment the number in the Q*_Bookings__c field based on which quarter it falls in within the fiscal calendar.

 

You will definitely need to write some additional business logic where I highlighted in red below, but this should get you started. I've commented up the code to help you understand how the logic flows.

 

trigger TestTrigger on Opportunity (after insert, after update) 
{
	//create set of forecast records ids to query
	Set<Id> forecastIds = new Set<Id>();
	//create list of won opportunities
	Opportunity[] wonOpportunities = new Opportunity[]{};

	for(Opportunity o : Trigger.new)
	{
		//check if the opportunity is related to a forecast record
		if(o.Forecast__c != null)
		{
			//if the opportunity is being inserted and is marked as Won add to
			//the won opportunity map 
			if(Trigger.isInsert && o.IsWon = true)
			{
				forecastIds.add(o.Forecast__c);
				wonOpportunities.add(o);
			}
			//if the opportunity is being changed to Won
			else if(Trigger.isUpdate && o.IsWon && o.IsWon != Trigger.oldMap.get(o.Id).IsWon)
			{
				forecastIds.add(o.Forecast__c);
				wonOpportunities.add(o);
			}
		}
	}

	//query forecast records to update
	Map<Id, Forecast__c> forecastMap = new Map<Id, Forecast__c>([SELECT Id, Q1_Bookings__c, Q2_Bookings__c, Q3_Bookings__c, Q4_Bookings__c FROM Forecast__c WHERE Id IN :forecastIds]);

	//iterate through the won opportunities and increment the quarterly bookings count
	for(Opportunity o : wonOpportunities)
	{
		//TODO: Write additional logic to determine which quarter it is based on your
		//companies fiscal calendar
		{
			//increment the quarterly bookings count
			Forecast__c f = forecastMap.get(o.Forecast__c);
			f.Q1_Bookings__c = f.Q1_Bookings__c + 1;
		}
	}

	if(forecastMap.size() > 0) update forecastMap.values();
}

 

Its also worth noting that you may want to consider expanding the trigger to update the related forecast records on delete and undelete as well.

 

Let us know if you have any questions!

This was selected as the best answer
TeashaTeasha
Thank you so much!!! I will let you know how it goes. Teasha Gable Manager of Sales Planning & Analysis Sensys Networks 510.205.2020 teasha@sensysnetworks.com