Skip to main content

Anyone have a good way to conditionally render a component based on whether the user has access to edit a particular field?

I have buttons in a button set that edit a field, but if the user doesn’t have permission to edit that field, they don’t need the buttons…

Using a ‘snippet returns true’ rendering condition, with a snippet like the following works for me.


var model = skuid.$M('Comments'),<br>&nbsp; &nbsp; field = model.getField('Pinned__c');<br>&nbsp;<br>return field.editable; 


Technically you could make that a one-liner like so:


return skuid.$M('Comments').getField('Pinned__c').editable;

Joining the schizophrenic champions club I see.



To extend the thought here, here’s a snippet to evaluate if a user has access to query an object for using on rendering logic.  This is handy when you have tabs that have page includes in them where you can disable the tab if the underlying include would be inaccessible.  For example, on record detail pages, we have a tabsets to keep the related lists organized and each of these are their own page so we can reuse related lists on many pages. 

The other benefit of this approach is that you don’t need knowledge of the specific skuid Model name.  With page includes, you have to maintain uniqueness of model names so you don’t run into conflicts with your models, which implies a lot of model names to keep track of in snippets.  Moving the snippet logic to the general sObject means only one snippet per object.  Ideally It would be nice to pass the SObject name as a parameter into the snippet from the pagebuilder so there’s only one snippet, but we’re not there yet with snippet parameters.


if(skuid.utils.getAPIDescribeSObject(‘Account’) !== null)
    { 
        if( skuid.utils.getAPIDescribeSObject(‘Account’).queryable == ‘true’ ) 
        {
            return true;
        }
    }
else{
    return false;
}


Likewise, you could disable a new record button if the user doesn’t have permission to crate a target object record:

if(skuid.utils.getAPIDescribeSObject(‘Account’) !== null)
    { 
        if( skuid.utils.getAPIDescribeSObject(‘Account’).createable == ‘true’ ) 
        {
            return true;
        }
    }
else{
    return false;
}


A couple details of interest.  Unlike the skuid.model API, the values in skuid.utils.getAPIDescribeSObject return as a string, which is why the return is wrapped in the if statement.  Also, there’s a null check, which handles script errors if the user has no access to the object.


Thanks, John.

Is there a way to extend your logic to a specific field? What if the user has access to edit an object but not a specific field?


Matt - Just worked out this snippet for you.  The API call returns the fields of the SObject as an array.  You can loop through that list for the field you’re interested in and work with any of the attributes of the field.  

SObj = skuid.utils.getAPIDescribeSObject(‘Account’);

if(SObj !== null)
    { 
        var fields = SObj.fields;
        $.each(fields,function(e,field){
            if(field.name == ‘LastName’ && field.updateable == ‘true’){
                result = true ;
            }
        });
    }
return result;


Now if we could just get this feature, we’d be golden:

https://community.skuid.com/t/provide-context-parameter-to-conditional-rendering-snippet-…