0

I'm running some validations and the validations are that: Every Employee should have two languages (English & Spanish) associated with it language type. (Lang_Type__c)

Otherwise user should not able to save the record and throw the validation errors, the below code works but not all the time, sometimes the validations pass even though the validation should failed. To give you an example, the user able to Save the record just by adding the English (should add both English/Spanish for that specific lang_type__c)

Lang_type__c = picklist (first language, home language)

So if I select lang_type__c = 'first language' or 'home language' then I should have both Language__c = English and Spanish

Here is my code:

public static boolean ValidateLanguage(Id empId)  
{
    List<Emp_Languages__c> langList =  [SELECT Id, Name, Language__c, Lang_Type__c FROM Emp_Languages__c WHERE Id =: empId];

    map<string, boolean> mapLang = new map<string, boolean>();

    for(Emp_Languages__c l : langList) 
    {
       if(String.isNotEmpty(l.Lang_Type__c)) {
          mapLang.put(l.Lang_Type__c, true);  
       }
       else {
          mapLang.put(l.Lang_Type__c, false);  
       } 
    }

    boolean validationPass = true;
    boolean hasEnSp = false;
    String employeeLang = ''; 
    boolean langEnglish = false;
    boolean langSpanish = false;

    for(Emp_Languages__c lang : langList) 
    {
       hasEnSp = mapLang.get(lang.Lang_Type__c);  
       if(!hasEnSp ) 
       {
         employeeLang = '';              
         if(lang.Language__c == 'English') 
         { 
           employeeLang = 'English';
           langEnglish = true;
         }
         if (lang.Language__c == 'Spanish') 
         { 
            employeeLang = 'Spanish';
            langSpanish = true;
         }
         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'There should be two language English/Spanish');
         ApexPages.addMessage(myMsg); 

         validationPass = false;

   }
     return validationPass && langEnglish && langSpanish ;      
    }

just to give you an sample data:

should validation fail: 

Id     Name         Language__c       Lang_Type__c
1      test         English           first language
2      abc          Spanish           home language 

should validation pass:

Id     Name         Language__c       Lang_Type__c
1      test         English           first language
2      abc          Spanish           first language 
3
  • can you verify your sample data you have provided? Commented Aug 3, 2016 at 23:26
  • Is that Language__c is multi-select picklist? Commented Aug 3, 2016 at 23:36
  • Language__c is a Text
    – Nick
    Commented Aug 4, 2016 at 0:20

1 Answer 1

0

From the langList which is returned from your SOQL, prepare a Set<String> which will contain only Language__c. Set will not contain duplicate.

Verify the Size of Set <2, it will throw the error message.

Sample code will look like this:

List<Emp_Languages__c> langList =  [SELECT Id, Name, Language__c, Lang_Type__c FROM Emp_Languages__c WHERE Id =: empId];

Set<String> LangSet = new Set<String>();
String prevLangType=''; 
for(Emp_Languages__c l : langList) 
{   
   if(String.isNotEmpty(l.Lang_Type__c) && (prevLangType=='' || l.Lang_Type__c == prevLangType)) {
        LangSet.add(l.Language__c);
        prevLangType = l.Lang_Type__c;
   }       
}

if(LangSet.size()<2)
{
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'There should be two language English/Spanish');
    ApexPages.addMessage(myMsg);
}
12
  • that logic will not work, how do you know which languages the user have added? i can add two "English" language
    – Nick
    Commented Aug 4, 2016 at 0:18
  • Let me replicate it Commented Aug 4, 2016 at 0:28
  • @NickKahn updated my answer Commented Aug 4, 2016 at 0:43
  • I did not think of Set thanks for that, but I see that potential there is still an issue and the issue is Lang_Type__c so as i have said in my question every Lang_Type__c will have two language associated with it, based on the code you posted and the Lang_Type__c I have now is only two but this will increase up to 6 type, so how would you handle that? if i have select lang_type='first language' and lang='English' and again lang_type = 'home language' and lang = 'Spanish' will the above code will break
    – Nick
    Commented Aug 4, 2016 at 0:54
  • I think you want distinct languages irrespective of lang_type__c Commented Aug 4, 2016 at 0:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .