function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anil IngleAnil Ingle 

How to solve FLS Create and FLS Update Issues

Hello All,

I have run code scanner tool on my apex class and Visual force page. It give me FLS Create and FLS Update Issues.
I have solved these issues as per mention following URL

https://developer.salesforce.com/page/Testing_CRUD_and_FLS_Enforcement
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_perms_enforcing.htm

Again I have run code scanner tool on my apex class and Visual force page. It gives me again FLS Create and FLS Update Issue.
Let me know, if anything I miss

Following are the sample code

Boolean isContFirstNameUpsertable = Schema.sObjectType.Contact.fields.FirstName.isCreateable() &&
                                Schema.sObjectType.Contact.fields.FirstName.isUpdateable();
Boolean isContLastNameUpsertable = Schema.sObjectType.Contact.fields.LastName.isCreateable() &&
                                Schema.sObjectType.Contact.fields.LastName.isUpdateable();

List<Contact> lstContact = new List<Contact>();
if(isContFirstNameUpsertable && isContLastNameUpsertable) {
    for(AgentWrapper aw : scope) {
        Contact c = new Contact(
                                    FirstName=aw.FirstName,
                                    LastName=aw.LastName);
        lstContact.add(c);
    }
    if(lstContact.size() > 0) {
        upsert lstContact;
    }
}

Thanks in Advance
Lucas Duque 9Lucas Duque 9
Have you sure that user which you have tried to perform a Create or Update has permission in object for create or update ?
Santosh Reddy9989Santosh Reddy9989
Hi anil, try like this

List<Contact> lstContact = new List<Contact>();

    for(AgentWrapper aw : scope) {
        Contact c = new Contact(
                                    FirstName=aw.FirstName,
                                    LastName=aw.LastName);
        lstContact.add(c);
    }

    if(lstContact.size() > 0 && Schema.sObjectType.Contact.fields.FirstName.isCreateable() &&                              Schema.sObjectType.Contact.fields.FirstName.isUpdateable()  &&                                                     Schema.sObjectType.Contact.fields.LastName.isCreateable()  &&                                Schema.sObjectType.Contact.fields.LastName.isUpdateable() )
   {
      
        upsert lstContact;
    }

 
Anil IngleAnil Ingle
Hello Santosh,
Thanks for the reply.
I have updated my code as per your suggession. But still Code Scanner gives me FLS Create and FLS Update Issue.
Following are the sample code

global with sharing class AgentImportJob implements
Database.Batchable<AgentWrapper>, Database.AllowsCallouts, Database.Stateful, Schedulable {
    global Iterable<AgentWrapper> start(Database.BatchableContext BC) {
        .....
     }

    global void execute(Database.BatchableContext BC, List<AgentWrapper> scope) {
        List<Contact> lstContact = new List<Contact>();

        for(AgentWrapper aw : scope) {
                Contact c = new Contact(
                                        FirstName=aw.FirstName,
                                        LastName=aw.LastName);
                lstContact.add(c);
        }

        if(lstContact.size() > 0 && !Test.isRunningTest() && Schema.sObjectType.Contact.fields.FirstName.isCreateable() &&   Schema.sObjectType.Contact.fields.FirstName.isUpdateable()  &&   Schema.sObjectType.Contact.fields.LastName.isCreateable()  && Schema.sObjectType.Contact.fields.LastName.isUpdateable() )
       {
            upsert lstContact;
        }
    }

    global void finish(Database.BatchableContext BC) {
        ....
    }
}
Santosh Reddy9989Santosh Reddy9989
Hi Anil,
can u show me that source scanner report or drop a mail  to kbsantoshreddy@gmail.com

 
Anil IngleAnil Ingle
Hello Santosh,
I have send Scan Code file report. Please check and let me know your opinion.
Thanks