In my Managed Package I have a Flow template which subscriber can clone/reuse and customize to their needs. I now want to provide a bite more guidance on such a customization and relate a packaged Custom Object to such a flow including code which should do the following.

Is that possible with Apex and/or any API?

  • Clone a packaged Flow
  • related it to a Custom Object record
  • also related the Custom Object to the flow instance
  • Modify some variable values in the flow

Salesforce has exposed tooling api that can help you clone the Managed Package flow if it is packaged as template.

Note you can wrap this tooling api via an apex class and achieve everything that API can do.

To clone the flow via the api follow the below steps.

PREREQUSITE : Note that for experiment i have installed a flow package here from appexchange that comes with a template flow.

Step 1 - You will need to query the flow tooling object with the template using the tooling api

The query would look something like below


I found a very weird thing that it does not let me filter by the flow api name and had to use Master Label. I suggest if you are using Master label have a namespace prefix of the package to the label of flow. You can also use the IsTemplate boolean flag to filter for Template one and also use ManageableState field for filter

The response from the API would look like below

The key is to understand the Metadata field. The Metadata will be of the Structure as documented here

Step 2 - Create a new Flow Sobject with the Metadata similar to the one that you queried.

You can make a POST call to an endpoint as below


A sample JSON to post will be is as shown here

A successful POST would response as below

  "id" : "301B00000006siGIAQ",
  "success" : true,
  "errors" : [ ],
  "warnings" : [ ],
 "infos" : [ {
    "message" : "Example info message"
   } ]

Note that you will need to copy Metadata field from previous, however make sure that isTemplate boolean is false and also you provide a FullName field. Do not provide Master Label or the Description field here.

Relating to Custom Object

This can be done by setting a variable $Flow.CurrentRecord to the Id of the record. This is documented here.

There is a whole Data Model around this, FlowInterview object is the Instance of the flow. The FlowInterview object is documented here

You cannot create these as this are auto created. However you can query them via apex.

FlowRecordRelation ties flowInterviews and the the Salesforce RecordIds. You can create these records via apex.

Relating to Custom Object and Modify Variables

You can update the variables using the PATCH request to the end point as below with the additions,


flowID you can get from the step 2

NOTE: PATCH in apex could be challenging so may require a workaround as mentioned here.

Flow metadata variables and reference to sobject actions can be updated as well.

  • Did your ever make this work using the Apex wrappers that exist for the Tooling github.com/afawcett/apex-toolingapi or Metadata APIs github.com/financialforcedev/apex-mdapi? – Robert Sösemann Jun 16 '20 at 22:18
  • I tested this using the workbench tool. I think you can simply write a https callout in apex to these endpoint! – Mohith Shrivastava Jun 16 '20 at 22:57
  • Sure but the parsing, modifying and sending back changes flow metadata is something I have to implement on my own 🙁 – Robert Sösemann Jun 16 '20 at 22:59
  • 2
    That is true you need an apex wrapper similar to metadata structure. I think you can easily generate one from the JSON using json to apex tool. – Mohith Shrivastava Jun 16 '20 at 23:01

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.