How can I give field level security to the fields on profile basis

I have referred sample on how to update the records but I'm not sure how this helps me on giving FLS to the fields. Because I need to handle multiple conditions over here for granting FLS to filed.

private static void updateAccounts() {
            System.out.println("Update the 5 new test Accounts...");
            SObject[] records = new SObject[5];
            try {
              QueryResult queryResults = connection.query("SELECT Id, Name FROM Account ORDER BY " +
                    "CreatedDate DESC LIMIT 5");
              if (queryResults.getSize() > 0) {
                  for (int i=0;i<queryResults.getRecords().length;i++) {
                      SObject so = (SObject)queryResults.getRecords()[i];
                    System.out.println("Updating Id: " + so.getId() + " - Name: "+so.getField("Name"));
                    // create an sobject and only send fields to update
                    SObject soUpdate = new SObject();
                    soUpdate.setField("Name", so.getField("Name")+" -- UPDATED");
                    records[i] = soUpdate;
  • This code updates records, not FLS. You need to use the metadata API to even get started with giving FLS.
    – sfdcfox
    Commented Feb 19, 2018 at 5:31
  • The code you need to start from is here.
    – sfdcfox
    Commented Feb 19, 2018 at 5:33
  • @sfdcfox - I'm new to metadata API. One question here, Do I need to create an Apex class or do any configurations in a sandbox (where I'm going to deploy the changes) in order to use metadata API service?
    – Arosh
    Commented Feb 19, 2018 at 6:10
  • No, the Metadata API can be used directly in Java. In fact, it's easier that way; writing Apex Code would add an unnecessary level of complexity for what otherwise would be straight forward code. Note that you'll need to download the Metadata WSDL (from Develop > API) and import it in to your project.
    – sfdcfox
    Commented Feb 19, 2018 at 6:52
  • @sfdcfox - I have downloaded MetaData WSDL from Org. Now my question is, as it is a .wsdl file, how do I import this into my project
    – Arosh
    Commented Feb 19, 2018 at 8:56

1 Answer 1


I think you have to use metadata api to control FLS. The following answer may help you.

Field level security using metadata api

You must log in to answer this question.

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