1

In some orgs I work in, there is a process setup that automates creating some objects after an object is created. This process does not exist in all orgs. I've written Apex code that imports data from a CSV file. I'd like to add code to my import routine that checks to see if the process exists. If it does not, I can add code to my import routine that replicates what the process is doing. Is there a way within Apex to query for the existence of a process by its name?

1

One cheat you may be able to use is to ask for it by name:

Boolean flowExists = false;
try {
  new PageReference('/flow/MyFlowName').getContent();
  flowExists = true;
} catch(VisualforceException e) {
}

This will work wherever callouts/getContent is allowed, so I'm not sure if that helps you in your specific use case, but it might be worth looking at.


You can also try to invoke the flow directly:

Boolean flowExists = false;
try {
    Flow.Interview.createInterview('FlowName', new Map<String, Object>());
    flowExists = true;
} catch(TypeException e) {

}
  • I tried your second solution, and it worked great! Thanks! – Michael Sobczak May 10 '18 at 17:23
2

You're going to need to call out to the Tooling API and retrieve FlowDefinition records.

You can start playing around with an anonymous script like below to get a feel for what you actually need to build:

String auth = 'Bearer ' + UserInfo.getSessionId();
String query = 'SELECT DeveloperName, ActiveVersionId FROM FlowDefinition';
String endpoint = URL.getSalesforceBaseUrl().toExternalForm() +
    '/services/data/v41.0/tooling/query/?q=' +
    EncodingUtil.urlEncode(query, 'UTF-8');

HttpRequest request = new HttpRequest();
request.setHeader('Authorization', auth);
request.setEndpoint(endpoint);
request.setMethod('GET');

HttpResponse response = new Http().send(request);
system.debug(response.getBody());
  • That definitely works. However, when I clone a Trialforce org, it doesn't have the remote site settings in it that would allow this to work without user intervention. I'm thinking if there is a way to create those remote site settings when the org is cloned... – Michael Sobczak May 2 '18 at 19:38
  • I think you got a bit literal with the word "query" in the question. – sfdcfox May 3 '18 at 1:11

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.