Edit an existing flow using flow scripting SDK

Hi, I'm trying to edit an existing flow using flow scripting SDK. If I use flowFactory.createFlowInboundCallAsync then it will delete the existing flow and add a new one with the same name. Instead of that, I want to edit the flow and save it as a new version.

I was able to load, checkout, and add new tasks and actions to the flow. But I want to delete all the existing tasks of the flow before adding new ones. Is there a way to do that using flow scripting SDK? I couldn't find anything in the documentation.

This is the code I'm using to edit the flow. So after I checkout the flow, I need to delete all existing tasks. That's where I'm stuck.

flowFactory.loadFlowByFlowNameAsync('Test Flow', enums.FLOW_TYPES.inboundCall, 'latest', function(inboundFlow) {
    inboundFlow.checkoutAsync(true, function() {
       // Here I need to delete all the existing tasks before adding new ones

    });
    return inboundFlow.publishAsync(true);
});

Thanks,
Athul

1 Like

Hi @athuldilip,

Thanks for the question.

Once you've loaded up the flow and checked it out, you can use this method to get a snapshot of reusable tasks within the flow:

https://mypurecloud.github.io/purecloud-flow-scripting-api-sdk-javascript/ArchBaseFlowWithTasks.html#tasksReusable

And then you can iterate the array returned by that property GET and call this method:

https://mypurecloud.github.io/purecloud-flow-scripting-api-sdk-javascript/ArchBaseFlowWithTasks.html#deleteTask

to delete each task. The array returned from the first call is a snapshot of ArchTask instances in the flow at the time your code calls the getter so your code should be able to iterate the array and delete each task without having to re-query reusableTasks after each task deletion. Obviously once the reusable tasks are deleted code should no longer attempt to work with the snapshotted task instances as they will be invalid.

Hope this helps!

Jim

1 Like

Thanks, Jim for the answer.

I see the tasksResuable method is inside the ArchBaseFlowWithTasks class. After loading the flow, I have an instance of ArchBaseFlow class, but how can I get an instance of ArchBaseFlowWithTasks?

I tried creating it using a constructor with the below code, but that didn't work.
let flowWithTasks = new flowsdk.ArchBaseFlowWithTasks(inboundFlow);
Note: Here inboundFlow is an instance of ArchBaseFlow class.

I'm sorry if it's an obvious question.

Thanks,
Athul

Hi @athuldilip!

Here, I tweaked some of the sample code above. Note that I didn't actually run the sample code below but see if this does what you need :slight_smile: :

// small update to sample above to use enums.FLOW_VERSIONS.latest to
// specify the version which yes, is equivalent to 'latest' but uses the enum instead.
flowFactory.loadFlowByFlowNameAsync('Test Flow', enums.FLOW_TYPES.inboundCall, enums.FLOW_VERSIONS.latest, (archInboundCallFlow) => {

    // Note:  Not sure but if a flow is locked to another user / OAuth client else you
    // might need to add an unlock call here too before checking out.

    // Time to check out the inbound call flow.
    archInboundCallFlow.checkoutAsync(true, () => {

        // Lets get a snapshot of the the reusable tasks from of the inbound call flow.
        // If the flow doesn't have any reusable tasks, this will be an empty array.
        let archFlowReusableTasks = archInboundCallFlow.tasksReusable;

        // Now iterate through the returned array of ArchTask instances and call 
        // deleteTask on each one.
        archFlowReusableTasks.forEach((archFlowReusableTask) => {
            archInboundCallFlow.deleteTask(archFlowReusableTask);
        });

        // Remember, don't fiddle with the reusable task array anymore since
        // those tasks are deleted.  To be safe, we'll set archFlowReusableTasks
        // to undefined.
        archFlowReusableTasks = void 0;

        // Do more stuff and things here :)
    
    });		

    // Ok sports fans, time to publish the flow!
    return archInboundCallFlow.publishAsync(true);
});

The archInboundCallFlow that's passed back by the first loadFlowByFlowNameAsync will be an ArchFlowInboundCall instance which inherits from ArchBaseFlowWithTasks so you should be able to get at the reusable tasks from it.

Hope that helps!

Jim

Hi Jim,

It worked. Thanks a lot for such a detailed answer, that was really helpful :slight_smile:

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.