4

Is it possible to retreive translated picklist values using VF Remote Objects?

For example, I have a Solution and I'm trying to get Solution.Status.

So I have the following:

  <apex:remoteObjects >
    <apex:remoteObjectModel name="Solution" fields="SolutionName,Id,Status" jsShorthand="Service"/>
  </apex:remoteObjects>

And then in JS, I do something like this:

new SObjectModel.Service().retrieve(
    {
      where: { .... criteria... },
      orderby: [{ CreatedDate: 'ASC' }],
      limit: 20
    },
    function (err, results, evt) {
      if (err) {
        onError(err);
      } else {
        results.forEach(function (sol) {
          console.log(sol.SolutionName, sol.Status);
        });
      }
    }
);

Right now, my console shows only untranslated values, and TOLABEL(Status) does not compile. How can I get the translated versions?

0

I don't like this solution, but this is what I've done. I haven't tested this for Mutliselect Picklists.

JS Translator function:

var getTranslator = function (translations) {
  return {
    _translationMap: translations,
    translate: function (objName, fieldName, picklistValue) {
      if (typeof picklistValue == 'string' || picklistValue instanceof String) {
        return (this._translationMap &&
            this._translationMap[objName] &&
            this._translationMap[objName][fieldName] &&
            this._translationMap[objName][fieldName][picklistValue]) ?
              this._translationMap[objName][fieldName][picklistValue] : picklistValue;
      } else { // array
        return picklistValue.map(function (val) {
          return this.translate(objName, fieldName, val);
        }, this);
      }
    }
  };
};

So to use it, I add in the following controller code:

Controller Logic:

public static List<Schema.DescribeFieldResult> getPicklistsFor(Schema.DescribeSObjectResult type) {
    List<Schema.DescribeFieldResult> rs = new List<Schema.DescribeFieldResult>();
    List<Schema.SObjectField> results = type.fields.getMap().values();
    for (Schema.SObjectField fld : results) {
        if(fld.getDescribe().isAccessible() &&
           (fld.getDescribe().getType() == Schema.DisplayType.Picklist ||
            fld.getDescribe().getType() == Schema.DisplayType.MultiPicklist)) {
            rs.add(fld.getDescribe());
        }
    }
    return rs;

}

public static List<Schema.DescribeFieldResult> getSolutionPicklists() {
    return getPicklistsFor(Schema.SObjectType.Solution);
}

and then in JS, I do something a little more elegant than this (in reality I use a some binds):

  var translator = getTranslator(
      {
        'Account': {<apex:repeat value="{!accountPicklists}" var="fld">
          '{!fld.name}': {<apex:repeat value="{!fld.picklistValues}" var="pv">
            '{!JSENCODE(pv.value)}': '{!JSENCODE(pv.label)}',
          </apex:repeat>},
        </apex:repeat>},
        'Solution': {<apex:repeat value="{!solutionPicklists}" var="fld">
          '{!fld.name}': {<apex:repeat value="{!fld.picklistValues}" var="pv">
            '{!JSENCODE(pv.value)}': '{!JSENCODE(pv.label)}',
          </apex:repeat>},
        </apex:repeat>}
      }
    )
  });

 //  and when I want to use it, I do something like:
 translator.translate('Solution', 'Status', sol.get('Status'));

If there's a more natural way to do this (or a better way to write this bit up), I'd be very happy to hear it.

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.