function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Katherine RoweKatherine 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.
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?


User-added image

 
Best Answer chosen by Katherine Rowe
Apoorv Saxena 4Apoorv Saxena 4
Hi Katherine,

Try this code :
 
trigger MapLOBTrigger on Account (before update, before insert) {
	for(Account a1: Trigger.new){
		for (MappingLOB__c item : MappingLOB__c.getAll().values()) {
			if(item.Industry__c==a1.Industry){
				a1.Industry=item.LOB__c;
			}
		 }
	}
}

Please let me know how it worked out for you.

Thanks,
Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Katherine,

Try this code :
 
trigger MapLOBTrigger on Account (before update, before insert) {
	for(Account a1: Trigger.new){
		for (MappingLOB__c item : MappingLOB__c.getAll().values()) {
			if(item.Industry__c==a1.Industry){
				a1.Industry=item.LOB__c;
			}
		 }
	}
}

Please let me know how it worked out for you.

Thanks,
Apoorv
This was selected as the best answer
Katherine RoweKatherine Rowe
Awesome, yes that worked. Thanks for the help.

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?
Apoorv Saxena 4Apoorv Saxena 4
Unfortunately, you can't make name field as an auto number here, and yes I know the limit of actually 38 ASCII characters is frustrating, but salesforce is working to exceed this limit to 80, until then you have to deal with it like this.
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