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
LoserKidLoserKid 

Update a value cross-object (std to custom)

I am new to salesforce and my first task has been to populate the Actual Cost on the standard object Campaign with the Cost on my custom object Campaign_Item. I know that I have to use a trigger but where I am hitting a wall is how the trigger should fire. Im really having a hard time understanding the way the logic flows with in a trigger. Things seem to be done in a special manor because of governing limits. I have read through all of the documentation on triggers and Im still lost. Any help would be greatly appreciated. Most of the samples I see are using SQL calls or are using IDs and Account info. I just want to move a value. 

 

Thanks in advance

Aaron

Best Answer chosen by Admin (Salesforce Developers) 
LoserKidLoserKid

 

trigger SendCostToCampaign on Campaign_Items__c (after insert, after update) 
{
	// This loads all the ids in Campaign_Item that references the parent (Campaign in this case)
	// Its sole purpose here is to be used in builing the campaign list
	Set<ID> campaignCost = new Set<ID>();
	
	// This loads the same ID the Set does but ties the cost to the ID
	Map<ID, Double> costInfo = new Map<ID, Double>();
	
	// Places the information into both the Set and the Map from Campaign_Item
	for(Campaign_Items__c ci : Trigger.new)
	{
		campaignCost.add(ci.Campaign__c);
		costInfo.put(ci.Campaign__c, ci.Cost__c);
	}
	
	// Builds a list of campaign IDs but only if that match the Campaign_Item
	List<Campaign> campaign = [select id from Campaign where id in :campaignCost];
	
	// this sets the value on each matching Campaign to value in Campaign_Item based in the ID match in the Map	
	for (Campaign c : campaign)
	{
		c.ActualCost = costInfo.get(c.id);
	}
	
	update campaign; // updates the campiagn object
}

 

 

All Answers

LoserKidLoserKid

Oh, and the Campaign is the parent to the Campaign_Item.

LoserKidLoserKid

 

trigger SendCostToCampaign on Campaign_Items__c (after insert, after update) 
{
	// This loads all the ids in Campaign_Item that references the parent (Campaign in this case)
	// Its sole purpose here is to be used in builing the campaign list
	Set<ID> campaignCost = new Set<ID>();
	
	// This loads the same ID the Set does but ties the cost to the ID
	Map<ID, Double> costInfo = new Map<ID, Double>();
	
	// Places the information into both the Set and the Map from Campaign_Item
	for(Campaign_Items__c ci : Trigger.new)
	{
		campaignCost.add(ci.Campaign__c);
		costInfo.put(ci.Campaign__c, ci.Cost__c);
	}
	
	// Builds a list of campaign IDs but only if that match the Campaign_Item
	List<Campaign> campaign = [select id from Campaign where id in :campaignCost];
	
	// this sets the value on each matching Campaign to value in Campaign_Item based in the ID match in the Map	
	for (Campaign c : campaign)
	{
		c.ActualCost = costInfo.get(c.id);
	}
	
	update campaign; // updates the campiagn object
}

 

 

This was selected as the best answer
LoserKidLoserKid

Let me know if my comments are correct. i dont want to give out bad info.