Determine key prefix for reference field

Barry SchnellBarry Schnell 💎💎
edited March 2, 2017 in Questions
For a given model, skuid makes available the KeyPrefix property that provides the org specific object prefix.  This comes in very handy.

We have a scenario were we would like to build a hyperlink to the "tab/list" view for fields on a skuid page. The fields we would like this for are reference/lookup fields on the object being displayed.  For example, on our Order page, we display the Account for the order.  We would like to put a hyperlink to "/001/o" in the label area.  Getting the hyperlink in the label is easy.  Doing this dynamically to support deployments to other orgs is the challenge.

Is there a way to access the Key Prefix for a lookup field on an object?

My solution thus far is to create a remote method that gets invoked when the hyperlink is clicked that would call getDescribe() on the SObject (the name would be provided in the parameter to the remote method), return the key prefix and then redirect appropriately.  Another option might be to create VF page that does something similar.

I'm hoping there might be an easier way though.  Does skuid have this information under the covers where it might be accessible? 

Thoughts/suggestions are appreciated!

Comments

  • Zach McElrathZach McElrath Principal Software Engineer Chattanooga, TN 💎💎💎
    edited March 2, 2017
    If you go look at the field metadata for a Skuid REFERENCE field, you should be able to get what you're after:

    var someModel = skuid.model.getModel('someModel');
    var someField = someModel.getField('Some_Reference_Field__c');

    if (!someField.namePointing) {
    // For non-polymorphic reference fields,
    // just grab the first entry in referenceTo,
    // and return its key prefix
    var targetObject = someField.referenceTo[0];
    console.log('Key Prefix for ' + targetObject.objectName + ': ' + targetObject.keyPrefix);

    } else {
    // For polymorphic reference fields (e.g. WhoId, WhatId, OwnerId)
    // there are multiple possible "target objects",
    // so you'll have to loop over and find the object you're interested in
    skuid.$.each(someField.referenceTo,function(){
       if (this.objectName === 'MyCustomObject__c') {
          targetObject = this;
          console.log('Key Prefix for ' + this.objectName + ': ' + this.keyPrefix);
       }
    });
    }


  • Barry SchnellBarry Schnell 💎💎
    edited February 25, 2017
    Worked like a champ, thanks Zach!!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!