2

I have a set of fields in the array list which I wanted to give FLS with Read access. I'm using below piece of code to update the field level security using Salesforce API in java language.

But the problem is that for each field it is taking 4 to 5 seconds to update the FLS. Is there any way that we can have all the changes in the list and update the FLS in bulk.

I tried adding admins in the List but it doesn't help.

for(String field:updateReadList) {      
    ProfileFieldLevelSecurity fieldSec = new ProfileFieldLevelSecurity();
    fieldSec.setField(field);
    fieldSec.setReadable(true);
    fieldSec.setEditable(false);
    admin.setFieldPermissions(new ProfileFieldLevelSecurity[] { fieldSec });
    api.updateMetadata(new Metadata[] {admin});
    System.out.println("Success");
}
0

1 Answer 1

1

But the problem is that for each field it is taking 4 to 5 seconds to update the FLS. Is there any way that we can have all the changes in the list and update the FLS in bulk.

Because you are adding the FLS within the iteration and that for every iteration, you are making an API call to update the FLS on the profile.

In order to what you are trying to do, I would try something as below.

  1. Declare an array of ProfileFieldLevelSecurity [] outside the loop
  2. Add the respective ProfileFieldLevelSecurity within the loop into that array
  3. Set the array on the profile outside the loop
  4. Invoke the API just once, outside the loop

int index = 0;

// declare an array of ProfileFieldLevelSecurity outside the loop
ProfileFieldLevelSecurity [] fieldArray = new ProfileFieldLevelSecurity [updateReadList.length]; // or size() depending if updateReadList is an array or list

for(String field:updateReadList) {      
    ProfileFieldLevelSecurity fieldSec = new ProfileFieldLevelSecurity();

    fieldSec.setField(field);
    fieldSec.setReadable(true);
    fieldSec.setEditable(false);

    fieldArray[index++] = fieldSec; // add all the FLS attributes to an array
}

admin.setFieldPermissions(fieldArray); // set all field permissions added in the array out of the loop
api.updateMetadata(new Metadata[] {admin}); // call the update in bulk - just one single call
9
  • Bulk update is not happening when I followed this one
    – Arosh
    Commented Nov 5, 2018 at 11:04
  • Didn't you accept this answer earlier? So does that mean you had not verified this and it had never worked? What exactly is not working? The approach mentioned above is a way how you can group together multiple calls based on what api provides in a single call.
    – Jayant Das
    Commented Nov 5, 2018 at 13:37
  • Hey, I was able to but it was not working when I given 4 fields in a object where only 2 fields are valid and remaining 2 fields are not available in the object (means not a valid api name for the remaining 2 fields), in that case it isn't updating the whole means the correct api names for the 2 fields FLS is not getting updated.
    – Arosh
    Commented Nov 6, 2018 at 15:16
  • Basically I wanted to some get additional help for the same question and I thought, making it accept will close my question so I did undo
    – Arosh
    Commented Nov 6, 2018 at 15:18
  • If the field names are not valid in the list, wouldn't it throw an error? How will a field which does not exist on an object be expected to be updated with FLS? I am quite sure that it threw an exception and that in that scenario it just is either all or none. If you are using an API to update FLS, you need to make sure you are passing correct API names.
    – Jayant Das
    Commented Nov 6, 2018 at 15:18

You must log in to answer this question.

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