1

How do I reduce servercall load/time from a community. I build a dynamic picklist component which loads on the page as checkboxes. I use this component 5 times on the page.

It takes about (3000ms) sec to load my communitypage with the picklists(checkboxes). After I select some of my checkboxes the server times out. If I load my community it also seems like my components are calling the server from the homepage where the picklists are not located.

How can ik minimize this loadingtime (3000ms) and prevent the calls from the server if a user is not on the picklist page?

Picklist

    @AuraEnabled
public static String getPicklistData(String objectName,String fieldName){          
    List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
    Map<String,String> objectMap = new Map<String,String>();
    for(Schema.SObjectType f : gd)
    {
         objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName());
    }
    String query = 'SELECT '+fieldName+' FROM '+objectName; 
    List<SOBject> lstObj = Database.query(query);        
    String selVal =  String.valueOf(lstObj[0].get(fieldName)) ; 
    Schema.SObjectField sobjField = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap().get(fieldName) ;
    Schema.DescribeFieldResult fieldResult = sobjField.getDescribe() ; 
     List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

    List<picklistData> lstRet = new List<picklistData>();        
    for( Schema.PicklistEntry f : ple){
     picklistData obj = new picklistData(); 
    obj.val = f.getLabel();
    lstRet.add(obj);
    } 
    return JSON.serialize(lstRet);
}    
public class picklistData{
    public String val{get;set;}

}

Salesforce Community optimizer Page with Picklist component enter image description here

Homepage enter image description here

Javascript
   loadPicklist : function(component, event, helper) {        
        var action = component.get('c.getPicklistData');
        var str_objectName = component.get("v.objectName");
        var str_fieldName = component.get("v.fieldName");
        action.setParams({
            objectName : str_objectName,
            fieldName : str_fieldName 
        });
        action.setStorable();        
        action.setCallback(this, function(res) {   
            if (res.getState() === 'SUCCESS') { 
                var retJSON = JSON.parse(res.getReturnValue());  
                component.set("v.records",retJSON);
            }
        }); 
        $A.enqueueAction(action);  
    },
  • There is no need of return JSON.serialize(lstRet); on server side and var retJSON = JSON.parse(res.getReturnValue()); on client side simply do return lstRet; on server and on clinet ` component.set("v.records", res.getReturnValue());` – itzmukeshy7 Mar 14 '18 at 8:17
4

There's a few things you can do here.

First, set getPicklistValue as a background task. It'll be loaded later on when nothing more important is going on, which should help loading times:

action.setBackground(true);

Second, you can optimize your Apex Code significantly:

public class PicklistValue {
    @AuraEnabled public string label, value;
    PicklistValue(String lab, String val) {
        label = lab;
        value = val;
    }
}
public class Response {
    @AuraEnabled public PicklistValue[] values = new PicklistValue[0];
}
@AuraEnabled
public static Response getPicklistData(String objectName, String fieldName) {
    Response res = new Response();
    for(PicklistEntry value: ((SObject)(Type.forName('Schema.'+objectName).newInstance())).getSObjectType()
        .getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues()) {
            res.values.add(new PicklistValue(value.getValue(), value.getLabel()));
        }
    return res;
}

Which should run in sub-second timing (this code ran on one of my fields in 14ms).

  • I was wandering I used your example and it indeed inmproved the loading time, I used the picklists in a lightining component and use copies of this component for several picklist to filter an objectlist in another component do you have an example how jou make a intersection between the selection of the different components? – Thomas Mar 15 '18 at 11:02
  • @Thomas I'm not sure I understand the question. I kind of doubt I have code that exactly matches what you're looking for, but I'd be glad to try and help if I could see more. Do you have a question on this that includes the code you've written already for this? – sfdcfox Mar 15 '18 at 14:15

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.