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
K SrikanthK Srikanth 

Creating custom field and FLS permission using Apex

Hi,

I am creating a custom field using apex metadata api call for Standard user (non admin) profiles. But not able to give edit access for below code.

 String strRequestBody1 = '<?xml version="1.0" encoding="UTF-8"?>'
                + '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
                + '<soapenv:Header>'
                + '<ns1:SessionHeader soapenv:mustUnderstand="0" xmlns:ns1="http://soap.sforce.com/2006/04/metadata">'
                + '<ns1:sessionId>' + UserInfo.getSessionId() + '</ns1:sessionId>'
                + '</ns1:SessionHeader>'
                + '</soapenv:Header>'
                + '<soapenv:Body>'
                + '<upsertMetadata xmlns="http://soap.sforce.com/2006/04/metadata">'
                + '<metadata xsi:type="ns2:Profile" xmlns:ns2="http://soap.sforce.com/2006/04/metadata">'
                + '<fullName>Standard</fullName>'
                + '<custom>false</custom>'
                + '<fieldPermissions>'
                + '<field>'+strObjectName + '.' +strField2API+' </field>'
                + '<editable>true</editable>'
                + '</fieldPermissions>'
                + '</metadata>'
                + '</upsertMetadata>'
                + '</soapenv:Body>'
                + '</soapenv:Envelope>';
    
                HTTP httpService1 = new HTTP();
                HTTPRequest request1 = new HTTPRequest();
                request1.setMethod('POST');
                request1.setHeader('Content-Type', 'text/xml');
                request1.setHeader('SOAPAction', 'upsertMetadata');
                request1.setBody(strRequestBody1);
                request1.setCompressed(false);
                request1.setEndpoint(ENDPOINT_URL);
                
                if(!Test.isRunningTest()) {
                    HTTPResponse response1 = httpService.send(request1);
                    System.debug('Response ::: '+response1.getBody());
                }

I am getting error ==> 
Admin.userCanEditFieldPermissions</message><statusCode>INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY</statusCode></errors><fullName>Admin

Can anyone help me to fix this for standard user how to give edit access for field using apex

ShivankurShivankur (Salesforce Developers) 
Hi Srikanth,

Please try to check with below run code via Anonymous window before trying this further, for standard user how to give edit access for field using apex:

For a Specific Profile:
List<FieldPermissions> fpList = [SELECT SobjectType, Field, PermissionsRead, PermissionsEdit, Parent.ProfileId FROM FieldPermissions WHERE SobjectType = 'Account' and Field='Account.Customer_Priority__c' AND Parent.ProfileId IN (SELECT Id FROM PermissionSet WHERE PermissionSet.Profile.Name = 'System Administrator')]; if(!fpList.isEmpty()){ Boolean hasReadPermission = fpList[0].PermissionsRead; Boolean hasEditPermission = fpList[0].PermissionsEdit; system.debug('Read Permission - ' + hasReadPermission); system.debug('Edit Permission - ' + hasEditPermission); }

For Current User:
List<FieldPermissions> fpList = [SELECT SobjectType, Field, PermissionsRead, PermissionsEdit, Parent.ProfileId FROM FieldPermissions WHERE SobjectType = 'Account' and Field='Account.Customer_Priority__c' AND Parent.ProfileId=:Userinfo.getProfileId()]; if(!fpList.isEmpty()){ Boolean hasReadPermission = fpList[0].PermissionsRead; Boolean hasEditPermission = fpList[0].PermissionsEdit; system.debug('Read Permission - ' + hasReadPermission); system.debug('Edit Permission - ' + hasEditPermission); }
Also, check with below thread to get more insight from the discussion:
https://developer.salesforce.com/forums/?id=9060G0000005hnvQAA

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
 
K SrikanthK Srikanth
Hi Shivan,

thanks for reply,

To query that field also we need access right. When I query not getting that field due to no access.
Is there any way to assigin access.