Can Someone help with a scenario. I have a region picklist field with 7 to 9 and we have a sales director lookup field whenever region is selected salesdirector for that region should be auto populated. If you can help me with this scenario it would be really appreciated

  • (1) were you looking for a process builder solution? (2) is there somewhere in the database that defines the relationship between region value and sales director or do you intend to hard code that in the process builder action directly? – cropredy May 29 '16 at 0:56
  • There is no direct relationship between region value and sales director , I need to hardcode – sai May 29 '16 at 9:56

You can use before insert trigger. Your code will be something like this. I have bukified the trigger so that it wont break even if you try to insert update in chunks of 100 using data loader.

trigger OpportunityTrigger on Opportunity(before insert, before update) {
 List < Opportunity > opOpps = Trigger.new;
 //Finding regions for which we need a salesdirector.
 Set < String > regionString = new Set < String > ();
 for (Opportunity op: opOpps) {
 //Adding a SOQL to find the salesdirector from the regions we got from above code

 List < User > listSalesDirector = [Select id, name from User where region__C in : regionString];

 //Creating Map for Bulkification when using the bulk update/insert or batch

 Map < String, String > mapOfRegionVsSalesDirectorID = new Map < String, String > ();
 for (User sd: listSalesDirector) {
  mapOfRegionVsSalesDirectorID.put(sd.region__C, sd.id);

 //Now populating the opobject's Sales director field
 for (Opportunity op: opOpps) {
  op.Sales_Director__c = mapOfRegionVsSalesDirectorID.get(op.Support_Region__c);

As this code runs before update or before insert you don't have to explicitly write any DML statement.

| improve this answer | |
  • I am getting below erro Error: Compile Error: sObject type 'Sales_Director__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 10 column 43 – sai May 27 '16 at 5:24
  • I have an Opportunity object , in that Support_Region__c - picklist field and Sales_Director__c - user lookup field – sai May 27 '16 at 5:25
  • I have updated my code. Can you specify the Schema of User object? and if there exists any region field on it? – Pranay Jaiswal May 27 '16 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.