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.