0

I'm encountering an issue modifying a Salesforce Validation Rule via the API. My attempt to update the rule with the ID '03dDn000001evPOIAY' results in the error message:

"Cannot modify managed object: entity=ValidationFormula, component=03dDn000001evPO, field=FieldEnumOrId, state=installed: newValue='null', oldValue='00NDn00000YwUYt'", with errorCode 'CANNOT_MODIFY_MANAGED_OBJECT'.

Here's my code snippet

from simple_salesforce import Salesforce

try:
    # Establish Salesforce connection
    sf = Salesforce(username='***********', password='********', security_token='********')

    # Query using the Tooling API to get the validation rule
    tooling_query = ("SELECT Id, CreatedDate, CreatedBy.Name, EntityDefinition.DeveloperName, ValidationName, Active "
                     "FROM ValidationRule WHERE ValidationName = 'Require_Default_Consignor'")
    response = sf.restful('tooling/query', params={'q': tooling_query})

    # Check if the query returned any records
    if response['records']:
        record = response['records'][0]
        validation_rule_id = record['Id']
        print("Connection successful. Retrieved validation rule:")
        print(f"ID: {validation_rule_id}")
        print(f"Created Date: {record['CreatedDate']}")
        print(f"Validation Name: {record['ValidationName']}")
        print(f"Active: {record['Active']}")

        # Prepare the metadata for update
        metadata = {
            "description": "Updated via Python Script",  # Include your desired description here
            "active": True,  # Set the active state
            "errorConditionFormula": "NOT(ISNULL(ValidationName))",
            "errorMessage": "Error updating somecolumn."
        }

        # Update the Validation Rule using PATCH method with the metadata
        update_response = sf.restful(f'tooling/sobjects/ValidationRule/{validation_rule_id}',
                                     method='PATCH', json={'Metadata': metadata})

        print("Validation rule updated successfully.")
    else:
        print("Connection successful but no validation rules found.")
except Exception as e:
    print("Error occurred:", e)

Any guidance on resolving this CANNOT_MODIFY_MANAGED_OBJECT error would be greatly appreciated.

1 Answer 1

2

It appears that this validation rule is part of a managed package. If so, you cannot change most aspects of the validation rule, whether through the API or through the UI. When trying to update a validation rule, you are only permitted to change the Active status for that validation rule. See this document.

5
  • The one thing you can do with such validation rules is disable them (or re-enable). Find the details here.
    – Phil W
    Commented Jul 16 at 21:16
  • @PhilW You're right, of course. I was mostly focused on the formula. I'll edit to clarify.
    – sfdcfox
    Commented Jul 16 at 21:29
  • Thank you for the clarification. Does this mean I can't disable or enable managed validation rules via the API as I tried above? If so, how can I do that? Any advice?
    – vw96
    Commented Jul 17 at 10:18
  • @vw96 It seems you can only change this state either through the metadata API or the UI. I tried this in the Tooling API, and it required the formula to be passed in, yet trying to do so would result in an error.
    – sfdcfox
    Commented Jul 17 at 13:45
  • Updating via metadata API worked, Thank you !
    – vw96
    Commented Jul 18 at 17:18

You must log in to answer this question.

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