3

I have an Account list and I want to update it.

I want to testing object and field level security enforcement but I'm wondering what I have to test :

1 - if Account object is updatable ? (if it's possible)

2 - if the fields in my select query is updatable ?

3 - only if the Account Name is updatable ?

Here the code for these three propositions :

List<Account> acc = [SELECT Id, Name, BillingStreet FROM Account];
...
// 1 : I don't know if it's possible to test if an object is updatable ?

// 2 : test all the fields in the select query
if (!Schema.sObjectType.Account.fields.Name.isUpdateable() && !Schema.sObjectType.Account.fields.BillingStreet.isUpdateable() ){
    update acc;
}

// 3 : test only the Name
if (!Schema.sObjectType.Account.fields.Name.isUpdateable()){
    update acc;
}
1
  • You want to use the schema field describe methods on the Account object. But first you need to get the tokens. I have code someplace I could share, but its late and I don't have time to look for it. You should be able search on Schema Describe to find what you need in an existing post. If not try using this search engine: findsf.info.
    – crmprogdev
    Oct 11, 2014 at 6:03

2 Answers 2

3

So

  1. Yes its of course possible you can do it in two ways (which essentialy are the same)

    Schema.sObjectType.Account.isUpdateable()
    

    and

    Account.sObjectType.getDescribe().isUpdateable()
    
  2. if (!Schema.sObjectType.Account.fields.Name.isUpdateable() &&
        !Schema.sObjectType.Account.fields.BillingStreet.isUpdateable() ){
            update acc; }
    

    That will check actually even 3 things

    • Edit access to Name field
    • Edit access to BillingStreet field
    • Edit access to Account itself
  3. This will check Name and Account access

Also remember about with sharing and without sharing for class

With sharing checks if the user have access (read, read/write, none) to a RECORD and not object itself. So even if user have access to edit fields/object but doesnt have access to record, that user wont be able to edit record.

There are of course cases when user have view all/modify all on object or view all data/modify all data (system admins).

Without sharing ommits that restrictions but it needs to be used only when you have that kinda specific requirement

0


Yes, all three propositions are possible to update on your Account Object.
Below link will help you more.
https://developer.salesforce.com/page/Enforcing_CRUD_and_FLS

1
  • Can you please add a space before the ']', it's messing with the URL
    – d_k
    May 22, 2017 at 6:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .