+ Start a Discussion
Travis WrightTravis Wright 

Account territories classification

I am trying to create an APEX trigger that will classify an account as Major, Mid , Inside, and Inside small. Here is the break out of filters that Major and Mid have. 

Major = Accounts with Employee Count >= 10,000
Accounts with Annual Revenue >=500,000,000
Accounts where F500 is checked
Accounts Where F501- 1000 is checked and Employee Count is >=10,000 and Annual Revenue >= 500,000,000
Accounts where industry  =  Pharma/Biotech, Energy or Fed - Gov

Mid = Accounts with Employee Count 4000<=9999
Accounts with Annual Revenue 250,000,000 >= 499,999,999
Accounts Where F501- 1000 is checked 
Accounts where industry  =  Education and Employees >= 8,000

Inside = Accounts with Employee Count  3999<=1500

Inside Small  = Accounts with Employee Count <1499

I have 4 checkboxs that will classify the type and allow me to write workflow rules to assign correctly. Below is the start of my trigger but I feel like I am doing it wrong and would love to see what you guys think?

trigger AssignTerritoryType on Account (before insert, before update) {
For (Account a :trigger.new){
  if (a.NumberOfEmployees >= 10000){
   (a.major__c = True);
  }
  else if (a.AnnualRevenue >= 500000000){
   (a.major__c = True);
  }
  else if (a.Fortune_500__c = true){
   (a.major__c = True);
  }
  else if (a.Fortune_500_1000__c = True){
   if (a.NumberOfEmployees >=10000){
    if(a.AnnualRevenue >= 500000000){
     (a.major__c =true);
    }
   }
  }
  else if (a.Industry = 'Pharma/BioTech'){
   (a.major__c = True);
  }
  else if (a.Industry = 'Energy'){
   (a.major__c = True);
  }
  else if (a.Industry = 'Fed - Gov'){
   (a.major__c = True);
  }
}

}
Best Answer chosen by Travis Wright
Eli FloresEli Flores
Hmmm... Since you are just updating a field, you'd really be much better served by using workflows.

You can simplify some of the as well though as stuff like:
Accounts with Employee Count >= 10,000
and
Accounts Where F501- 1000 is checked and Employee Count is >=10,000 and Annual Revenue >= 500,000,000
is redundant since both have the Employeed Count >=10000. The F501-1000 and Annual Revenue in a scenario with over 10000 is irrelevant.

Technically, though, your code is sound. I'd clean it up a little to make it more readable though:

Set<string> majorIndustries = new Set<string>{'Pharma/BioTech', 'Energy', 'Fed - Gov' };
if ( (a.NumberOfEmployees >= 10000) ||
      (a.AnnualRevenue >= 500000000) ||
   (a.Fortune_500__c = true) ||
   ( (a.Fortune_500_1000__c = True) && (a.NumberOfEmployees >=10000) && (a.AnnualRevenue >= 500000000) ) || //note that even if a.Fortune_500_1000__c = False it'll fall if a.NumberOfEmplyees >=10000 is true or A.AnnualRevenue>=500000000 since it will meet a previous condidtion
   majorIndustries.contains(a.Industry) = 'Pharma/BioTech')
  ) {
  a.major_c=true;
}

you'll thank yourself later for taking the time make it readable later on when you get the inevitable future change request :)

Though really, unless you are going to touch a different record based on this info, you're much better off using a workflow because then less technical folks (or someone on a power user level) can update it if the requirements changes.

All Answers

Eli FloresEli Flores
Hmmm... Since you are just updating a field, you'd really be much better served by using workflows.

You can simplify some of the as well though as stuff like:
Accounts with Employee Count >= 10,000
and
Accounts Where F501- 1000 is checked and Employee Count is >=10,000 and Annual Revenue >= 500,000,000
is redundant since both have the Employeed Count >=10000. The F501-1000 and Annual Revenue in a scenario with over 10000 is irrelevant.

Technically, though, your code is sound. I'd clean it up a little to make it more readable though:

Set<string> majorIndustries = new Set<string>{'Pharma/BioTech', 'Energy', 'Fed - Gov' };
if ( (a.NumberOfEmployees >= 10000) ||
      (a.AnnualRevenue >= 500000000) ||
   (a.Fortune_500__c = true) ||
   ( (a.Fortune_500_1000__c = True) && (a.NumberOfEmployees >=10000) && (a.AnnualRevenue >= 500000000) ) || //note that even if a.Fortune_500_1000__c = False it'll fall if a.NumberOfEmplyees >=10000 is true or A.AnnualRevenue>=500000000 since it will meet a previous condidtion
   majorIndustries.contains(a.Industry) = 'Pharma/BioTech')
  ) {
  a.major_c=true;
}

you'll thank yourself later for taking the time make it readable later on when you get the inevitable future change request :)

Though really, unless you are going to touch a different record based on this info, you're much better off using a workflow because then less technical folks (or someone on a power user level) can update it if the requirements changes.

This was selected as the best answer
Travis WrightTravis Wright
Eli,

Thanks for the quick response, I will give this a try as the base of the trigger I am trying to write. Also thanks for cleaning it up I did try it that way but the IDE was thrown errors when I used the || and &&. I will post final code when I am done writing it for a better understanding. 
Travis WrightTravis Wright
Last validation check before I write in the other 2 sections. Would like this only to check one of the checkbox which I don't think is correct. 

trigger AssignTerritoryType on Account (before insert, before update) {
For (Account a :trigger.new){
  Set<string> majorIndustries = new Set<string>{'Pharma/BioTech', 'Energy', 'Fed - Gov' ,'State - Mid','Education'};
if ( (a.NumberOfEmployees >= 10000) ||
      (a.AnnualRevenue >= 500000000) ||
   (a.Fortune_500__c = true) ||
   ( (a.Fortune_500_1000__c = True) && (a.NumberOfEmployees >=10000) && (a.AnnualRevenue >= 500000000) ) || //note that even if a.Fortune_500_1000__c = False it'll fall if a.NumberOfEmplyees >=10000 is true or A.AnnualRevenue>=500000000 since it will meet a previous condidtion
   majorIndustries.contains(a.Industry) = 'Pharma/BioTech')
   {
  a.major_c=true;
}
if ( ((a.NumberOfEmployees > 10000) && (a.NumberOfEmployees <=4000)) ||
     ((a.AnnualRevenue > 500000000) && (a.AnnualRevenue <=250000000)) ||
     ((a.Fortune_501_1000__c = true) &&(a.NumberOfEmployees > 10000) && (a.AnnualRevenue > 500000000))||
     (majorIndustries.contains(a.Industry) = 'State - mid')||
     ((a.NumberOfEmployees >=8000) && (majorIndustries.contains(a.Industry) = 'Education')
     )
   )
   {  
  a.mid_c=true;
}
}