3

we are using remoteTK on a salesforce project and its working great except for one very specific issue.

We are hitting governor limits with describes for one of the objects. IT has 150 picklists fields and the method thats getting all those picklists is returning an error.

This is the way the function is written right now is below. You can also see the full file here: https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/blob/master/RemoteTK.component

The limits are being hit on this line List picklistValues = field.getPicklistValues();

any ideas on hwo to get around it? Maybe the best way is to pass in a list of all the fields i need it ot describe and check against those? I don't need all 150 picklists in this app but not totally sure the best way to go about that?

public static String describe(String objtype) {
        // Just enough to make the sample app work!
        Schema.SObjectType targetType = Schema.getGlobalDescribe().get(objtype);
        if (targetType == null) {
            return makeError('The requested resource does not exist', 'NOT_FOUND');
        }

        Schema.DescribeSObjectResult sobjResult = targetType.getDescribe();

        Map<String, Schema.SObjectField> fieldMap = sobjResult.fields.getMap();

        List<Object> fields = new List<Object>();
        for (String key : fieldMap.keySet()) {
            Schema.DescribeFieldResult descField = fieldMap.get(key).getDescribe();
            Map<String, Object> field = new Map<String, Object>();

            field.put('type', descField.getType().name().toLowerCase());
            field.put('name', descField.getName());
            field.put('label', descField.getLabel());
            List<String> references = new List<String>();
            for (Schema.sObjectType t: descField.getReferenceTo()) {
                references.add(t.getDescribe().getName());
            }
            if (!references.isEmpty()) {
                field.put('referenceTo', references);
            }
            field.put('picklistValues', picklistOptions(descField));

            fields.add(field);
        }

        Map<String, Object> result = new Map<String, Object>();
        result.put('fields', fields);

        return JSON.serialize(result);
    }

 private static List<Object> picklistOptions(Schema.DescribeFieldResult field) {
        List<Object> picklistOptions = new List<Object>();
        Schema.DisplayType fieldType = field.getType();
        if (fieldType != Schema.DisplayType.Picklist &&
          fieldType != Schema.DisplayType.MultiPicklist &&
          fieldType != Schema.DisplayType.Combobox) {
            return picklistOptions;
        }
        List<Schema.PicklistEntry> picklistValues = field.getPicklistValues();
        for (Schema.PicklistEntry picklistValue: picklistValues) {
            Map<String, Object> picklistOption = new Map<String, Object>();
            picklistOption.put('value', picklistValue.getValue());
            picklistOption.put('label', picklistValue.getValue());
            picklistOption.put('active', picklistValue.isActive());
            picklistOption.put('defaultValue', picklistValue.isDefaultValue());
            picklistOptions.add(picklistOption);
        }
        return picklistOptions;
    }
3

As of the Summer '14 release the describe limits have been removed so this should no longer be a problem.

From the release notes:

Limits for all Apex describe calls have been removed for all API versions. Describe calls include describes for sObjects, fields, field sets, child relationships, picklists, and record types.

You’re no longer limited to describing 100 objects or to executing 100 describe statements.

0

One option would be to create a separate version of the describe remote action that doesn't return any picklist values. You could refactor the Apex a bit so that both calls use the same method body, with one passing over the field.put('picklistValues', picklistOptions(descField)); line.

Then make another remoteAction enabled Apex method that just gets the JSON formatted picklist values for the required fields.

This should help keep you within the 100 describes limit.

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.