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
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); },
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0