You need to sign in to do that
Don't have an account?
Katherine Rowe
Using a loop with a custom setting in a trigger
Can someone help me with the for loop I'm trying to put in my trigger?
I want to use a custom setting to hold mapping for a field. For example, if the user selects the Industry field as "Utilities & Water", then I want the trigger to go to the Custom Setting to find the appropriate value for that industry and put it in the field LOB, which in this example would be "Utilities & Communications". I started off not using a loop, and just using getInstance(), like below but then I realized some of my industry values are more than 40 characters which won't work with the Custom Setting name field.
So now I have tried added a custom field called Industry on my custom setting to use instead of the name field. But I think I've been told I can't use getInstance() and I have to instead use getAll() and a loop to sort through all the values in the Custom Setting? Maybe a List?
I want to use a custom setting to hold mapping for a field. For example, if the user selects the Industry field as "Utilities & Water", then I want the trigger to go to the Custom Setting to find the appropriate value for that industry and put it in the field LOB, which in this example would be "Utilities & Communications". I started off not using a loop, and just using getInstance(), like below but then I realized some of my industry values are more than 40 characters which won't work with the Custom Setting name field.
trigger MapLOBTrigger on Account (before update, before insert) { for(Account a1: Trigger.new){ if(MappingLOB__c.getInstance(a1.Industry) != null){ a1.Industry=MappingLOB__c.getInstance(a1.Industry).LOB__c; } } }
So now I have tried added a custom field called Industry on my custom setting to use instead of the name field. But I think I've been told I can't use getInstance() and I have to instead use getAll() and a loop to sort through all the values in the Custom Setting? Maybe a List?
Try this code :
Please let me know how it worked out for you.
Thanks,
Apoorv
All Answers
Try this code :
Please let me know how it worked out for you.
Thanks,
Apoorv
Would you say this is the best way to do what I'm doing? To handle mapping values where some are longer than 40 characters (and thus you can't use the Name field on the Custom Setting)?
Also doing it this way, the name field is annoying when inserting values to the custom setting... because name has to be unique and I have to go check to see what I've used in the name field to ensure what I'm adding is something different, when I don't even care what is stored in that field. There's not a way to make that an autonumber or something?
If you have to map values that are greater than 40 characters, then this approach is fine.
Please mark this question as solved if it answers your question so that others can view it as a proper solution.
Thanks,
Apoorv