4

I created a custom field through this tooling REST API /services/data/v54.0/tooling/sobjects/CustomField The API call succeed and I could see the custom fields under Setup > Object Manager > Lead.

I tried to add a lead using this REST API /services/data/v54.0/sobjects/Lead It says the fields does not exist. I am aware that we need to add field level Security for the custom field to make it work. It is mentioned in this thread that it can be done via REST API. Can someone please share the query on how to give access to System Administrator via REST API?

1
  • I can understand what they are referring to, but I need more details regarding the actual query, I don't know what query I need to use. Do I need to insert a new record? or Do I need to update a record? I don't know which table I need to take the action on
    – Sam
    Commented Apr 20, 2022 at 15:24

1 Answer 1

2

As sfdcfox mentioned in this answer that you can now Create, Read, Update, and Delete FieldPermissions via the REST API, you need to do a REST API call to insert the field permission for the newly created field via tooling API.

The FieldPermissions Object has mainly 5 fields ParentId, SobjectType, Field, PermissionsEdit and PermissionsRead. The ParentId field contains the Id of the PermissionSet. In Your case it is Id of the PermissionSet attached to the System Admin Profile.

You can do multiple REST API calls to get the PermissionSet Id from the Profile and the insert FieldPermissions or do a single composite request. The composite request Payload looks like this.

URL: /services/data/v54.0/composite/
Method: POST
Body:
{
    "allOrNone": true,
    "compositeRequest": [
        {
            "referenceId": "Profile",
            "url": "/services/data/v54.0/query/?q=SELECT+Id+FROM+Profile+Where+Name='System Administrator'",
            "method": "GET"
        },
        {
            "referenceId": "PermissionSet",
            "url": "/services/data/v54.0/query/?q=SELECT+Id+FROM+PermissionSet+WHERE+ProfileId='@{Profile.records[0].Id}'",
            "method": "GET"
        },
        {
            "referenceId": "NewFieldPermission",
            "body": {
                "ParentId": "@{PermissionSet.records[0].Id}",
                "SobjectType": "Lead",
                "Field": "Lead.My_NewField__c",
                "PermissionsEdit": "true",
                "PermissionsRead": "true"
            },
            "url": "/services/data/v54.0/sobjects/FieldPermissions/",
            "method": "POST"
        }
    ]
}
2
  • 2
    Thanks a lot Saroj :) it worked
    – Sam
    Commented Apr 21, 2022 at 4:22
  • If I have an integration that has full REST API access to customer's Salesforce data, will this work every time fetching the System Admin profile or are there cases where this might be somehow restricted for API clients? Commented Apr 4, 2023 at 10:07

Not the answer you're looking for? Browse other questions tagged .