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 

isupdateable method

Hi All,

I am iserting contact record in my class. I need to check if all the fields are updateble and then i will upsert the record. I have written the following code but it is not working. Kindly provide me solution.
 
if (Schema.sObjectType.Contact.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;
                       }

 
Nisar799Nisar799
Hi Anuj,

Try checking field level access using 
Schema.sObjectType.Contact.getDescribe().fields.getMap().get('Email_Alt_1__c').getDescribe().isUpdateable();

if it return true then assign value to that field. 
sfdcMonkey.comsfdcMonkey.com
Hi try below code :
if (Schema.sObjectType.Contact.isUpdateable()){
      Contact cc = new Contact();
            	   	   cc.FirstName = firstname;
            	   	   cc.LastName = lastname;
            	   	   cc.Email = emailaddress;
            	   	   cc.Email_Alt_1__c = altemail1;
            	       cc.Email_Alt_2__c = altemail2;
            	       cc.MailingStreet = street;
            	       cc.MailingCity = city;
            	       cc.MailingPostalCode = postalCode;
            	       if (country == 'United States') {
            	           cc.MailingState = stateprovince;
            	       } else {
            	       	   cc.MailingState = '';
            	       }
            	       cc.MailingCountry = country;
            	       cc.Country__c = regioncountry;
            	       //contact.Audience__c = audience;
            	       cc.Cisco_com_Login__c = ciscocomlogin;
            	       cc.Testing_ID__c = testingid;
            	       cc.Cisco_ID_CSCO__c = ciscoid;
            	       cc.Area_Code__c = Integer.valueOf(areaCode.trim());
            	       cc.Country_Code__c = countryCode;
            	       cc.Phone = phonenumber;
            	       cc.Fax = faxPhone;
                       
            	       cc.HomePhone = homePhone;
            	       upsert cc;
}
Thanks, let us know if it helps you
 
Anuj Joshi 42Anuj Joshi 42
Hi All,

Both the solutions are not working. 

@Nisar, I need to check for all the fields

Thanks,
Anuj
Arunkumar RArunkumar R
Hi Anuj,

Please try out the below snippet and change your code accordingly,
 
Set<String> fieldNames = new Set<String>();
fieldNames.add('FirstName');
fieldNames.add('LastName');
fieldNames.add('Email');
fieldNames.add('Email_Alt_1__c');
fieldNames.add('Email_Alt_2__c');

Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map <String, Schema.SObjectField> fieldMap = schemaMap.get('Account').getDescribe().fields.getMap();
Boolean isUpdatable = true;
for(Schema.SObjectField sfield : fieldMap.Values())
{
    Schema.DescribeFieldResult fieldResult = sfield.getDescribe();
    system.debug(fieldResult.getName());
    if(fieldNames.contains(fieldResult.getName()) && !fieldResult.isUpdateable()){
        isUpdatable = false;
        break;
    }
}

if(isUpdatable){
    // do you logic
}

Add whatever field that you need the check, then add those in the fieldNames set. 
Anuj Joshi 42Anuj Joshi 42
Hi Arunkumar,

Your cosde is not fulfilling my requirement. When i run the checkmarx report again, it is hsowing me the same thing.

Thanks,
Anuj