+ Start a Discussion
sdavis1528sdavis1528 

Need a code!

Hi there - I work in the publishing industry where we use letter territories to determine which reps can call which companies.  (A - gets to call ABC company).  I have created custom fields on the campaigns page for each letter (A, B, C, D, so on and so forth) and I would like to - as reps make closed won sales, each letter to total up the total value won opportunities automatically for that letter.

 

i.e.


Alliance Company - $2490.50

ABC Company - $1549.50

Another Company - $449.50

 

 

The custom field A would tell me that the total value won opportunity for the particular campaign your on is...

 

A - $4,489.50

 

So to pull this it would be related to the "Campaign Name" and that particular campaigns total value won opportunities.

 

Is this possible?

Thanks.

venturecventurec

sdavis1528,

 

Here is some code that worked for me. I didn't write any test code, and I left out updating territories C and D because I got lazy, but A and B are working and it is a copy and past.

 

I hope thiis is what you were looking for and will work for you:

 

  • Created 4 custom fields in the Campaign object
  • Created an after update trigger on the Opportunity object that passes in all Opportunities as a Map to a Class
  • Created a class / method to handle the bulk of the processing.

 

Here's the code:

 

Trigger -

trigger Opportunity_AU on Opportunity (before update) {

 

Campaigns.updtCampaignTerrTotals(trigger.newMap);

 

}

 

 

Class / Method:

 

public class Campaigns {

 

// When an Oppotunity's status changes to Won Closed, the item(s) will come into this method to update the Campaign's Territory totals.

// This class does not reverse the totals on the Campaign screen in the case where an Opportunity status might change back from won/closed.

 

public static void updtCampaignTerrTotals(map<Id, Opportunity> optys) {

 

 

// Used to hold all campaigns and their total summations of Opportunities that are won/closed.

Map<Id, Campaign> campaigns = new Map<Id, Campaign>([select Id, A__c, B__c, C__c, D__c from Campaign]);

 

 

// Used for retrieving the Territory that this Account is in.

Map<Id, Account> accts = new Map<Id, Account>([select Id, Name, Territory__c from Account]);

 

 

// Get the Campaign and the Account Territory Id. Update the Territory Totals in the Map

 for(Opportunity o : optys.values()) {

 

 

// Checks if the Opportunity is closed, is won and has a Campaign associated with it.

// If a Campaign is not associed with an Opportunity a runtime error will occur.

 if(o.IsClosed && o.IsWon && campaigns.get(o.CampaignId) != null){

 

 

Campaign c = new Campaign();

Double totalA;

Double totalB;

Double totalC;

Double totalD;

 

String territory = accts.get(o.AccountId).Territory__c;

 

 

if(territory == 'A'){

 

// Get the saved totals from the Campaign

totalA = campaigns.get(o.CampaignId).A__c;

 

if(totalA == null){

totalA = 0;

}

 

Double oppTotal = o.Amount;

campaigns.get(o.CampaignId).A__c = totalA + oppTotal;

}

 

 

if(territory == 'B'){

 

// Get the saved totals from the Campaign

totalB = campaigns.get(o.CampaignId).B__c;

 

if(totalB == null){

totalB = 0;

}

 

Double oppTotal = o.Amount;

campaigns.get(o.CampaignId).B__c = totalB + oppTotal;

}

 

 

// Got lazy here, but you get the point :)

//if(territory == 'C'){

 

//}

 

 

//if(territory == 'D'){

 

//}

 

 

 

}

}

 

 

// Convert the Map to a List to use in an update

Database.update(campaigns.values(), true);

 

}

}