0

I have a code which has an upsert statement and also an insert statement. I am checking FLS on each and every field in the if loop. Is there any way where we can check the fields for which we want to check FLS rather than adding it in if loop. My code is added below.

 Contact contact = [Select c.Phone, c.OtherPhone, c.MobilePhone, c.MailingStreet, c.MailingState, 
                   c.MailingPostalCode, c.MailingCountry, c.MailingCity, c.LastName, c.Id, c.HomePhone, 
                   c.FirstName, c.Fax, c.Email_Alt_2__c, c.Email_Alt_1__c, c.Email, c.Country__c, 
                   c.Cisco_com_Login__c, c.Cisco_ID_CSCO__c, c.AssistantPhone, c.Country_Code__c, c.Area_Code__c From Contact c
                   where c.Id =: user.ContactId];   
               if (contact != null) {
                   if(Schema.SObjectType.contact.fields.FirstName.isUpdateable() && 
                     Schema.SObjectType.contact.fields.LastName.isUpdateable() &&
                     Schema.SObjectType.contact.fields.Email.isUpdateable() &&
                     Schema.SObjectType.contact.fields.Email_Alt_1__c.isUpdateable() &&
                     Schema.SObjectType.contact.fields.Email_Alt_2__c.isUpdateable() &&
                     Schema.SObjectType.contact.fields.MailingStreet.isUpdateable() &&
                     Schema.SObjectType.contact.fields.MailingCity.isUpdateable() &&
                     Schema.SObjectType.contact.fields.MailingPostalCode.isUpdateable() &&
                     Schema.SObjectType.contact.fields.MailingState.isUpdateable() &&
                      Schema.SObjectType.contact.fields.MailingCountry.isUpdateable() &&
                      Schema.SObjectType.contact.fields.Country__c.isUpdateable() &&
                      Schema.SObjectType.contact.fields.Cisco_com_Login__c.isUpdateable() &&
                      Schema.SObjectType.contact.fields.Testing_ID__c.isUpdateable() &&
                      Schema.SObjectType.contact.fields.Cisco_ID_CSCO__c.isUpdateable() &&
                     Schema.SObjectType.contact.fields.Area_Code__c.isUpdateable() &&
                     Schema.SObjectType.contact.fields.Country_Code__c.isUpdateable() &&
                     Schema.SObjectType.contact.fields.Phone.isUpdateable() &&
                     Schema.SObjectType.contact.fields.Fax.isUpdateable() &&
                     Schema.SObjectType.contact.fields.HomePhone.isUpdateable()){
                   contact.FirstName = firstname;
                   contact.LastName = lastname;
                   contact.Email = emailaddress;
                   contact.Email_Alt_1__c = altemail1;
                   contact.Email_Alt_2__c = altemail2;
                   contact.MailingStreet = street;
                   contact.MailingCity = city;
                   contact.MailingPostalCode = postalCode;
                   if (country == 'United States') {
                       contact.MailingState = stateprovince;
                   } else {
                       contact.MailingState = '';
                   }
                   contact.MailingCountry = country;
                   contact.Country__c = regioncountry;
                   //contact.Audience__c = audience;
                   contact.Cisco_com_Login__c = ciscocomlogin;
                   contact.Testing_ID__c = testingid;
                   contact.Cisco_ID_CSCO__c = ciscoid;
                   contact.Area_Code__c = Integer.valueOf(areaCode.trim());
                   contact.Country_Code__c = countryCode;
                   contact.Phone = phonenumber;
                   contact.Fax = faxPhone;

                   contact.HomePhone = homePhone;
                   upsert contact;
                   }

Thanks, Anuj

1

You do not have a loop. If you did, you could calculate the && of all the field checks before the loop.

You could also remove the repeated code by doing something like this:

    Boolean isUpdateable = allUpdateable(new SObjectField[] {
            Contact.FirstName,
            Contact.LastName,
            ...
            });

private Boolean allUpdateable(SObjectField[] fields) {
    for (SObjectField field : fields) {
        if (!field.getDescribe().isUpdateable()) return false;
    }
    return true;
}

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.