0

I have a LWC component which get the results from apex class. Now when I login as a Italy user with locale Italian (Italy) and Language Italian, the component is not working. When i change Language to English it work fine. In apex class we don't have any language condition for display. To fix the issue I tried the same to replicate in a dev org but not able to replicate the same issue. When i change the language the component visibility is not getting affected. But when i change language in UAT it affects the visibility of the component. Do anyone know is there any other setting which need to be done to replicate the issue in a dev org.

APEX

public class cc_caseHandlingInstruction {
@auraenabled
public static knowledgeArticleData fetchKnowledgeWithCaseHandlingInstructions(string category){
    knowledgeArticleData data = new knowledgeArticleData();
    data.status = 'ERROR';
    data.articleDetails = null;
    list<user> userDataList = [SELECT Id,CC_CaseRole__c FROM User WHERE Id =: UserInfo.getUserId()]; 
    string serviceLevel = '';
    for(User u:userDataList){
        serviceLevel = u.CC_CaseRole__c ; 
    }
    system.debug(serviceLevel);
    string serviceLevelApiName = cc_caseHandlingInstruction.fetchCategoryApiName('Service_Level' , serviceLevel);
    if(!(serviceLevelApiName == 'ERROR' || serviceLevel == '')){
        try{
            Id knowledgeCaseHandlingRecordTypeId ;
            for (RecordType rc : [Select Id From RecordType where sobjecttype = 'Knowledge__kav' AND name = 'Case_Handling_Instruction' LIMIT 1]) {
                knowledgeCaseHandlingRecordTypeId =  rc.Id;
            }
            
            string query = 'SELECT id, Title, Category_Information_CaseHandlingInstr19__c,Category_Information_CategoryId__c FROM Knowledge__kav WHERE Category_Information_CategoryId__c =:';
            query   += 'category AND recordtypeid =: knowledgeCaseHandlingRecordTypeId   WITH DATA CATEGORY  Service_Level__c AT '+ serviceLevelApiName +'__c  LIMIT 1' ;
            
            
            List<Knowledge__kav> knowledgeArticleList = Database.query(query);
            
            if(knowledgeArticleList.size()>0){
                for(Knowledge__kav kav : knowledgeArticleList){
                    data.status = 'SUCCESS';  
                    data.articleDetails = kav;
                }
            }else{
                data.status = 'ERROR';  
                data.articleDetails = null;  
                data.errorMessage = 'No Articles Found';
            }
        }Catch(exception e){
            data.errorMessage = e.getMessage();
        }
    }else{
        if(serviceLevel == ''||serviceLevel == 'none'||serviceLevel == null){
            data.errorMessage = 'User Case Role Not Populated';
        }else if(serviceLevelApiName == ''){
            data.errorMessage = 'User Case Role Configuration ERROR';
        }          
    }

    return data;
}

public static string fetchCategoryApiName(string categoryGroupName ,string CategoryLabel){
    try{
        List<String> objType = new List<String>();
        List<DescribeDataCategoryGroupStructureResult> describeCategoryStructureResult;
        objType.add('KnowledgeArticleVersion');
        
        
        List<DataCategoryGroupSobjectTypePair> pairsList = new List<DataCategoryGroupSobjectTypePair>();
        DataCategoryGroupSobjectTypePair pair = new DataCategoryGroupSobjectTypePair();
        pair.setSobject('KnowledgeArticleVersion');
        pair.setDataCategoryGroupName(categoryGroupName);
        pairsList.add(pair);
        
        describeCategoryStructureResult = Schema.describeDataCategoryGroupStructures(pairsList, false);
        
        List<DataCategory> allCategories;
        
        for(DescribeDataCategoryGroupStructureResult singleResult : describeCategoryStructureResult){
            
            DataCategory [] toplevelCategories = singleResult.getTopCategories();
            allCategories = getAllCategories(toplevelCategories);
            
            
        }
        for(DataCategory category : allCategories) {
            if(category.getLabel() == CategoryLabel){
                return category.getName();
                
            }
        }
        return 'ERROR';
    }catch(exception e){
        return 'ERROR';   
    }
}


private static DataCategory[] getAllCategories(DataCategory [] categories){
    if(categories.isEmpty()){
        return new DataCategory[]{};
            } else {
                DataCategory [] categoriesClone = categories.clone();
                DataCategory category = categoriesClone[0];
                DataCategory[] allCategories = new DataCategory[]{category};
                categoriesClone.remove(0);
                categoriesClone.addAll(category.getChildCategories());
                allCategories.addAll(getAllCategories(categoriesClone));
                return allCategories;
            }
}

public class knowledgeArticleData {
    @AuraEnabled public string status ;
    @AuraEnabled public string errorMessage; 
    @AuraEnabled public Knowledge__kav articleDetails;
}
}
12
  • Did you find anything while debugging the issue in chrome dev tools and salesforce debug logs? May 11 at 13:28
  • @maniaccoder Yes in system.debug(serviceLevel); the result is null when language is changed.
    – Anu
    May 11 at 13:30
  • You mention the component is not visible - are you certain you understand why it's not visible? Is it because no data is returned from your apex controller, is it some visibility condition you have on the page, etc. You said System.debug(serviceLevel) is null - does the user record actually have a value and does your component receive the error message User Case Role Not Populated? May 11 at 14:00
  • Did you check your debug logs? P.S. It's not normal to return 'ERROR' or something when a method fails. You should throw an AuraHandledException so you can see the error the server is throwing.
    – sfdcfox
    May 11 at 14:05
  • Sorry the System.debug(serviceLevel) is getting value but serviceLevelApiName is getting null value. The user have case role assigned. As I said when i change the language it is not working. In browser console i am getting error undefined.
    – Anu
    May 11 at 14:06
1

You must have translations in your production and UAT org whereas in dev you do not. If you go to

Setup --> Translate

You can select Language (ex. Italian) and then Data Category.

enter image description here

Here you should see Data Category Translation for a given category. It's this value that is returning when you change the user's language in your call to describeDataCategoryGroupStructures and getLabel(). This doesn't match the value in the user field you're storing and checking against.

I'd recommend using the API value of the Data Category (getName()) as this does not suffer from the same situation.

In terms of documentation, it's not called out directly - but, describeDataCategoryGroupStructures does mention this bit about using getLabel()

The label for the data category group in the Salesforce user interface.

Key being Salesforce user interface. This ends up being the translated value.

I was able to quickly test that in my own dev org after adding translations to the given data categories. The translated value was returned in getLabel() once one existed.

3
  • is it possible to get the actual value in the describeDataCategoryGroupStructures instead of translated value of the user. As per the doc it only have getlabel method which gives us the label value and it contain the translated value for the particular user.
    – Anu
    May 18 at 19:26
  • The doc has all the listed things, so doesn't appear so. Any reason getName() won't work? Is CC_CaseRole__c a picklist? If it was, you could make the API name of the picklist values reflect the "name" instead of label. If it's text, you could consider making it a picklist to be able to control the values. If it must be text, you might have to resort to some sort of custom metadata to map name to label. May 18 at 21:57
  • Yes the CC_CaseRole__c is picklist and we can add translation to it. But the problem is the same field is used in couple of places for the different logic. If we change it. It will impact the currently running logic. That is the reason I was trying to find a solution using describeDataCategoryGroupStructures.
    – Anu
    May 19 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.