0

I'm working in Python with the simple_salesforce library.

I can create new fields on my Account and Contact objects, based on info in How to update object description using simple_salesforce.

Basically, I'm doing the following for each of my new fields:

    mdapi = sf.mdapi
    custom_object = mdapi.CustomObject.read("Account")
    custom_object.fields = [
        {"fullName": "my_field__c", "label": "My Field", "type": "Text", "length": 20},
    ]
    mdapi.CustomObject.upsert(custom_object)

This works, except that I need to set the field level security to "visible" manually. How can I do this in my code too?

1 Answer 1

1

Give this a try:

profile = sf.mdapi.Profile('Profile Name')

profile.fieldPermissions = (
    [{
        "field": "Account.my_field__c",
        "readable": True,
        "editable": True,
        "hidden": False
    }]
)

query_response = mdapi.Profile.update(profile)
4
  • Thanks, that worked! Mar 20 at 11:22
  • To help the next person reading this: note that names of common profiles include "Admin" and "Standard". I found some useful related notes at salesforcehacker.com/2013/09/… and roycon.com/who-can-see-what-in-salesforce Mar 20 at 11:24
  • (or anyone else)... Some things that are still not clear to me, which would help flesh out this answer: - Where is the best documentation re profiles, permissions, and managing them via simple_salesforce? - I'm an Admin user on this project. Adding readable/editable to the Admin profile gave my code these abilities, as expected. I thought adding readable and editable to Standard instead would have also given me these rights (assuming Admin has a superset of Standard's powers). But this did not work. Why? - Is there an API that returns all of the profile names? Mar 20 at 11:29
  • 1
    Glad it worked! I referenced the simple_salesforce GitHub repo readme and the SF Metadata documentation profile -developer.salesforce.com/docs/atlas.en-us.api_meta.meta/… - and then just a lot of trial and error. The Admin profile is a separate profile, not a superset. To get a list - check out the repo readme section on Metadata - you're after the describe method.
    – resedasue
    Mar 21 at 17:15

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.