0

I am working on updating a Salesforce flow using a Python script. The script connects successfully and retrieves the flow details, but fails to update the flow with the following error message:

Failed to update flow: 400 - [{"message":"The version of the flow you're updating was active and can't be overwritten.","errorCode":"INVALID_STATUS","fields":[]}]

The flow metadata retrieved is as follows:

Flow ID: 301Dn000001lroxIAA
Status: Obsolete
MasterLabel: CQ Update Internal Comments
Flow Metadata:
{
  'actionCalls': [], 'apexPluginCalls': [], 'apiVersion': 51.0, 'assignments': [...],
  ...
  'status': 'Obsolete',
  ...
}

Despite the flow status being 'Obsolete', the update attempt results in the INVALID_STATUS error. The relevant portion of my Python script is:

import requests
from tkinter import messagebox

class SalesforceFlowUpdater:
    def __init__(self):
        # Salesforce credentials and OAuth2 parameters
        self.client_id = 'your_client_id'
        self.client_secret = 'your_client_secret'
        self.username = 'your_username'
        self.password = 'your_password'  # Salesforce password + security token
        self.login_url = 'https://your_salesforce_instance.my.salesforce.com'

    def get_access_token(self):
        # Salesforce OAuth2 endpoint
        auth_url = f'{self.login_url}/services/oauth2/token'

        # POST request for access token
        response = requests.post(auth_url, data={
            'grant_type': 'password',
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'username': self.username,
            'password': self.password
        })

        if response.status_code == 200:
            json_res = response.json()
            return json_res['access_token'], json_res['instance_url']
        else:
            messagebox.showerror("Error", f"Failed to authenticate: {response.status_code} - {response.text}")
            return None, None

    def update_flow(self, access_token, instance_url):
        # Authorization header with access token
        auth_header = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json'}

        # Salesforce REST API endpoint to query Flow metadata
        query_url = f'{instance_url}/services/data/v52.0/tooling/query/?q=' \
                    f'SELECT+Id,MasterLabel,Status,Metadata+FROM+Flow+WHERE+MasterLabel+=+\'CQ Update Internal Comments\''

        # GET request to fetch Flow metadata
        response = requests.get(query_url, headers=auth_header)

        if response.status_code == 200:
            metadata = response.json()

            if 'records' in metadata and metadata['totalSize'] > 0:
                record = metadata['records'][0]
                flow_id = record['Id']
                flow_metadata = record['Metadata']

                print("Connection successful. Retrieved flow details:")
                print(f"Flow ID: {flow_id}")
                print(f"Status: {record['Status']}")
                print(f"MasterLabel: {record['MasterLabel']}")
                print("Flow Metadata:")
                print(flow_metadata)

                # Update the necessary fields in the flow metadata
                flow_metadata['status'] = 'Active'  # Adjust status or other fields as needed

                # Salesforce REST API endpoint to update Flow metadata
                update_url = f'{instance_url}/services/data/v52.0/tooling/sobjects/Flow/{flow_id}'

                # PATCH request to update Flow metadata
                update_response = requests.patch(update_url, headers=auth_header, json={'Metadata': flow_metadata})

                if update_response.status_code == 204:
                    print("Flow updated successfully.")
                else:
                    print(f"Failed to update flow: {update_response.status_code} - {update_response.text}")
            else:
                print("Connection successful but no flow found with the specified MasterLabel.")
        else:
            print(f"Failed to fetch metadata: {response.status_code} - {response.text}")

    def main(self):
        access_token, instance_url = self.get_access_token()
        if access_token and instance_url:
            self.update_flow(access_token, instance_url)

if __name__ == "__main__":
    updater = SalesforceFlowUpdater()
    updater.main()

Question:

How can I successfully update the flow in Salesforce without encountering the INVALID_STATUS error? Any insights on the correct procedure or additional steps required to handle this situation would be greatly appreciated.

2

0

You must log in to answer this question.

Browse other questions tagged .