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
Anuj Joshi 42Anuj Joshi 42 

Covering FLS is createable and isUpdateable conditions in test class

Hi All,

I am checking in my class whether the fields of object are createable. I am using this condition in the objects. I am using Schema.sObjectType.Case.fields.fieldname.isCreateable in the if loop. I have 10 fields like this for which I am checking. For this if loop the code is not getting covered in the test class. 
I have tried to run as system admin using system.runAs() but that ios also not covering the test class.

Thanks,
Anuj
sachin kadian 5sachin kadian 5
Hi,

I am not sure if i got your requiremnt correct but i tried below code and its working fine for me and returning 100% test coverage.
 
public class ForumQuestion {
    public ForumQuestion(){
        List<String> fieldNames = new List<String>{'FirstName','Email','LastName'};
            Map<String,Schema.SObjectField> fieldsMap= Schema.sObjectType.Contact.fields.getMap();
            for(String f : fieldNames){
                if(fieldsMap.get(f).getDescribe().isUpdateable()){
                    System.debug('field '+f+ ' is Updatable ');
                }
            }    
    }
}

Test Class- 
@isTest
public class Test_ForumQuestion {
    public static testMethod void test_forumQuestionConstructor(){
        Test.startTest();
        new ForumQuestion();
        Test.stopTest();
    }
}