CRUD/FLS

By | November 12, 2015

Force.com allows you to control access to data at many different levels:

  • Objects (CRUD)
  • Fields (FLS)
  • Records (Sharing Rules)

So what is CRUD and FLS?

CRUD (Create, Read, Update, Delete) is the object security level within the Salesforce.com environment. CRUD can be used to restrict certain actions on standard and custom objects.

FLS (Field-Level Security) is similar to CRUD, but restrict actions on fields. This can be used on standard and custom objects as well.

You find more information here:   Enforcing CRUD and FLS.

So why do we write about CRUD and FLS? It’s important for Force.com developers to fully understand them in order to to pass the Security Review. This is because Apex code runs in system context and developers must manually enforce CRUD/FLS in their Apex controllers.

Example:

In this example, if the users did not have access to create the record he/she sees the error message ‘Insufficient access‘. But what if you need to create a record with for example 6 fields? Then you need 6 if statements. There is no way around it.

I think we agree that this is not convenient. The best practice is to use the powerful security API from Salesforce i.e. Force.com ESAPI. There are methods in the ESAPI that check CRUD and FLS and since it is open-source anyone can change the needed methods in the manner they need.

Using the ESAPI code we created a method that checks CRUD/FLS and will return a set of errors with a description, if there is a problem.

Example:

This scheme is designed to check the access to multiple fields both standard and custom objects. As the checkFields method returns a set of error messages, you can display them using a loop statement.

Example from the MbO application by CloudWharf:CRUD_FLS

You can find the full code of the ‘checkCRUDandFLS’ realization on our wiki: CloudWharf wiki.

Leave a Reply

Your email address will not be published. Required fields are marked *