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
Cris9931Cris9931 

custom setting question

Hello, I created the following custom setting:
User-added imageI have a list of translated words for German and Spanish and they are strictly dependent on what the English word will be entered in the record. On my object I have 3 fields, for English, German and Spanish.
How to make a logic in a trigger that whenever the field(which is in English) has some values than automatically the German field and Spanish field should populate with the corresponding value.
Example, let's say I change a record and I put 'Car' in the field which is used for English language.

 

User-added imageLike so... After saving it I want to populate the Task Type(DE) with 'Wagen' and Task Type(ES) with 'Automombile' because that's how I defined in the custom setting.
I tried to write the logic by myself but I failed. 

 

public static void onBeforeUpdate(Map<Id, SVMX_PS_PM_Task_Template__c> newMap)
    {
        Map<string,Translations__c > translation = Translations__c.getAll();
        
        for(SVMX_PS_PM_Task_Template__c newTemplate : newMap.values()){
            String storeValue = newTemplate.SVMX_PS_Task_Type__c;
            
         
              try{
                String germanTranslation = translation.get(storeValue).German__c;
                String englishTranslation = translation.get(storeValue).English__c;
                System.debug('english' + englishTranslation);
                String spanishTranslation = translation.get(storeValue).Spanish__c;
                newTemplate.Task_Type_DE__c = germanTranslation;
                newTemplate.Task_Type_ES__c = spanishTranslation;
              }
               catch(exception e) {
               newTemplate.Task_Type_DE__c = '';
               newTemplate.Task_Type_ES__c = '';
              }
        }
       
    }

Thanks
Best Answer chosen by Cris9931
Maharajan CMaharajan C
Hi Cristian,

You have to write a code like below:

If you are storing the English Word in Custom Setting Name field then you can directly refer the custom setting data as you coded otherwise we have to use seperate map to compare the custom setting like below.
public static void onBeforeUpdate(Map<Id, SVMX_PS_PM_Task_Template__c> newMap)
    {
        Map<string,Translations__c > translation = Translations__c.getAll();
		
		Map<String,Translations__c> tsMap = new Map<String,Translations__c>();
		for(Translations__c ts : Translations__c.getall().values()){
			tsMap.put(ts.English__c, ts);
		}
        
        for(SVMX_PS_PM_Task_Template__c newTemplate : newMap.values()){
            String storeValue = newTemplate.SVMX_PS_Task_Type__c;         
             try{
				if(tsMap.containsKey(storeValue))
				{
					newTemplate.Task_Type_DE__c = tsMap.get(storeValue).German__c;
					newTemplate.Task_Type_ES__c = tsMap.get(storeValue).Spanish__c;
				}
              }
               catch(exception e) {
               newTemplate.Task_Type_DE__c = '';
               newTemplate.Task_Type_ES__c = '';
              }
        }
       
    }

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Cristian,

You have to write a code like below:

If you are storing the English Word in Custom Setting Name field then you can directly refer the custom setting data as you coded otherwise we have to use seperate map to compare the custom setting like below.
public static void onBeforeUpdate(Map<Id, SVMX_PS_PM_Task_Template__c> newMap)
    {
        Map<string,Translations__c > translation = Translations__c.getAll();
		
		Map<String,Translations__c> tsMap = new Map<String,Translations__c>();
		for(Translations__c ts : Translations__c.getall().values()){
			tsMap.put(ts.English__c, ts);
		}
        
        for(SVMX_PS_PM_Task_Template__c newTemplate : newMap.values()){
            String storeValue = newTemplate.SVMX_PS_Task_Type__c;         
             try{
				if(tsMap.containsKey(storeValue))
				{
					newTemplate.Task_Type_DE__c = tsMap.get(storeValue).German__c;
					newTemplate.Task_Type_ES__c = tsMap.get(storeValue).Spanish__c;
				}
              }
               catch(exception e) {
               newTemplate.Task_Type_DE__c = '';
               newTemplate.Task_Type_ES__c = '';
              }
        }
       
    }

Thanks,
Maharajan.C
This was selected as the best answer
Cris9931Cris9931

Thank you very much Maharajan, works like a charm.

One question, in the future I will have over 10,000 custom settings... How can I improve this code to support such a huge data and to not wait a lot of time when a record is updated... Is there something I can improve within your code for such scenarios with huge data?
 

Thanks
Cristian