• Paul McCollum 16
  • NEWBIE
  • 5 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hi All,
I am new to Platform event based flows.So I am not able to understand how the platform event flow will run once we click "Debug" button to test the flow.As per my business requirement,there are 2 flows involved one is Scheduled flow and other one is Platform event flow.
Scheduled flow will just create a record to create a platform event.
Platform event has to execute the loop and if the loop limit is reached,update the records,create a platform event again,otherwise just update the records and exit.

I am getting the following error:
The flow failed to start because the provided values were not valid: You can't launch a platform event–triggered flow from within Flow Builder. To launch this flow, activate it and then publish an event message for the platform event that's configured in the Start element.


Any help is really appreciated.

Thanks
srk
  • May 07, 2022
  • Like
  • 0
In my company I was facing a scenario where I had to chain two Apex asynchronous calls:
First I needed to call myApexMethod1 which returns a result myResult
Secondly I needed to call myApexMethod2 but needed to provide myResult as a parameter to the method.


So this is the first version I coded :
 
({
    "myFunction" : function(cmp) {


        var action = cmp.get("c.myApexMethod1");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
               myResult = response.getReturnValue();
            }
        });
        $A.enqueueAction(action);


        var action2 = cmp.get("c.myApexMethod2");
	action2.setParams({ myParam : myResult });
        action2.setCallback(this, function(response) {
            var state2 = response.getState();
            if (state2 === "SUCCESS") {
               //Do something
            }
        });
        $A.enqueueAction(action);


    }
})

This code compiles well but there is a problem:
Since the calls are asynchronous, by the time we reach :
action2.setParams({ myParam : myResult });

it is not guaranteed that myResult is filled, because at that time nothing guarantees that myApexMethod1 has executed since it is asynchronous.

To avoid this behaviour, I decided to nest the second call inside the callback of the first method.
That gives the following code :
 
({
    "myFunction" : function(cmp) {


        var action = cmp.get("c.myApexMethod1");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {

               myResult = response.getReturnValue();

               var action2 = cmp.get("c.myApexMethod2");
	       action2.setParams({ myParam : myResult });
               action2.setCallback(this, function(response) {
                   var state2 = response.getState();
                   if (state2 === "SUCCESS") {
                        //Do something
                   }
               });
               $A.enqueueAction(action);

            }
        });
        $A.enqueueAction(action);

    }
})

I have tested it and it works.

But my question is:
Is it the proper way to chain these asynchronous calls?
Next time maybe I will have to chain a lot of asynchronous calls and this pattern does not look very clean?
Is there a better practice to chain these asynchronous calls?