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?– cropredyCommented May 29, 2016 at 0:56
-
There is no direct relationship between region value and sales director , I need to hardcode– saiCommented May 29, 2016 at 9:56
Add a comment
|
1 Answer
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) {
regionString.add(op.Support_Region__c);
}
//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.
-
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– saiCommented May 27, 2016 at 5:24
-
I have an Opportunity object , in that Support_Region__c - picklist field and Sales_Director__c - user lookup field– saiCommented May 27, 2016 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? Commented May 27, 2016 at 6:36