How do I use the isAvailable() property on the Record Type Metadata Object?

  • 2
  • Question
  • Updated 3 months ago
Trying to filter record types in the record type model based on what the logged in user has access to.  We are trying to accomplish this by querying all record types in the model, then looping through each row in the model and checking to see if the user has access to that particular row.  Can we use isAvailable() in this scenario?  If so, how do we use it?  We haven't had any luck finding an example in the community or online.  

Any help is appreciated! 
Photo of Erick Hollander

Erick Hollander

  • 100 Points 100 badge 2x thumb

Posted 3 years ago

  • 2
Photo of Erick Hollander

Erick Hollander

  • 100 Points 100 badge 2x thumb
We have tried 
for(recordType of recordTypeModel.getRows()){
  if(!recordType.isAvailable){
     recordTypeModel.abandonRow(recordType);
  }
}
Photo of Erick Hollander

Erick Hollander

  • 100 Points 100 badge 2x thumb
in this case recordType.isAvailable is undefined
Photo of and thorough

and thorough

  • 788 Points 500 badge 2x thumb
Hi, Erick.  Did you ever figure this out?  I am also trying to use the isAvailable property (documented here) and getting stuck.
Photo of and thorough

and thorough

  • 788 Points 500 badge 2x thumb
So this is pretty old, but I came back to this recently and figured out how to use isAvailable to generate the correct record type picklist for a given user.  This uses a custom field renderer snippet on the Record Type field.  Sharing it here in case it helps others, or if anyone has suggestions for improving.  For example, there is probably a better way to handle the fact that this won't work unless it is called as the field renderer from the RecordTypeID field.  Also, I set the field to required, but not sure how to have it show with the red border.  I am not so experienced with custom field renderers, so I used the example here as a starting point (thanks, J!).  Suggestions welcome!

The below is from a static resource snippet setup, so might need to be adjusted for inline snippets.

( function( skuid, $ ){
    var snippet = skuid.snippet,
        ui = skuid.ui;

    snippet.registerSnippet( 'availableRecordTypesPicklist', function(){
        var field = arguments[0],
            value = arguments[1],
            fieldName = field.id,
            model = field.model,
            row = field.row;

        // Proceed if this renderer was called from record type field
        if( fieldName == 'RecordTypeId' ){

            if( field.mode === 'edit' ){
                // Build picklist options
                var availRecTypes = [];

                // Loop through each record type in the field's model
                $.each( model.recordTypeInfos, function( i, rt ){
                    // Build list of record types available to the user (except the master)
                    if( rt.isAvailable && ( rt.isMaster == undefined || !rt.isMaster ) ){
                        availRecTypes.push({   
                            value : rt.recordTypeId,    // Will be stored in target object 
                            label : rt.name             // Will display in picklist
                        });
                    }
                });

                // Render the options as a PICKLIST
                var availRecTypesPicklist = ui.renderers.PICKLIST.edit({
                    entries : availRecTypes,
                    required : true,
                    value : value
                }).change( function(){
                    // Update the row in the target object
                    model.updateRow( row, fieldName, $( this ).val() );
                });

                //  Append the PICKLIST to the DOM element
                field.element.append( availRecTypesPicklist );
            
            } else {
                //  If the mode is anything other than edit, display the field as Text
                ui.fieldRenderers.TEXT[ field.mode ]( field, model.getFieldValue( row, 'RecordType.Name' ) );
            }
        } else {
            // If renderer not called from record type field, warning message
            console.log( 'This custom field renderer must be called from record type field' );
        }
    });
}( skuid, skuid.$ ));