1

I have a code which inserts custom Note__c object by using 5 fields. I want to check FLS and CRUD permission field level wise as they are reported in the checkmarx report. I know we can write in if statement like this

if(schema.sobjectType.Note__c.fields.Message__c.isCreateable())

I wanted to know how can we check for more than one field instead of writing it as or condition in the IF statement. Below is my piece of code.

         Notes__c note= new Notes__c();  
         note.Case__c= emlist.parentid;
             IF (emlist.HTMLBody != NULL && emlist.HTMLBody != ''){
             note.Message__c = emlist.HTMLBody;
             }
             else {
             note.Message__c = emlist.TextBody;
             }    
         note.Sent_To__c = emlist.ToAddress;
         note.From__c = emlist.FromAddress;
         note.Subject__c = emlist.Subject;
         note.Datetime_Created__c = emlist.CreatedDate;
         if (emlist.Parent.Contact.Email == emlist.ToAddress || emlist.Parent.Email__c == emlist.ToAddress) // If the email is the same as Case's contact email or Email__c on Case itself, the type is a response.
         {
         note.Type__c = 'Response';
         }
         else {
         note.Type__c = 'Forward/Others';
         }
         notesList.add(note); // Add note object to the list
     }
1

You can get a list of all createable fields through this way:

public static Set<String> getCreateableFields(Schema.SObjectType objectType) {
    Set<String> createableFields = new Set<String>();
    Map<String, Schema.SobjectField> fields = objectType.getDescribe().fields.getMap();
    for (String s : fields.keySet() ) {
        if ((s != 'Name') && (fields.get(s).getDescribe().isCreateable())) {
            createableFields.add(s.toLowerCase());
        }
    }
    return createableFields;
}

Then you can check in set if field exist like this:

Set<Schema.SobjectField> createableUserFields = getCreateableFields();
if(createableUserFields.contains(User.isactive)){}
  • @Frodo, Can i write the code like this? if(!(Schema.sobjecttype.case.fields.origin.iscreateable())) { } if(language.contains('en')) cseObj.Origin = 'Web - English'; if(language.contains('es')) cseObj.Origin = 'Web - Spanish'; if(language.contains('zh')) cseObj.Origin = 'Web - Chinese'; if(language.contains('ja')) cseObj.Origin = 'Web - Japanese'; Basically do nothing if it is not createable and continue with the code – AJoshi Jun 12 '18 at 9:23
  • @AJoshi Rather perform logic only when user has these access. It will save else part of your logic. – Mr.Frodo Jun 12 '18 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.