Merge 2 flow scripts into one

test3445
Tera Contributor

Dear all,

 

I have two catalog items (one to add apples and one to delete apples) that I am merging into one and I want to know how I can combine (or what parameters to use) the add script and the delete script so that I have only one script that includes the add and the delete:

 

Add action:

    var aliasArr = fd_data._1__get_catalog_variables.list_of_apples_to_add || [];

    var description = "Comments:  " + fd_data._1__get_catalog_variables.comments + "\n" +

        "List of apples to add:  " + "\n";

    var j = 0 ;

    for (var i = 0; i < aliasArr.length; i++) {

        j = i + 1;

        description += "Size["+j+"]:  " + aliasArr[i].size + "\n" +

            "Color["+j+"]:  " + aliasArr[i].color + "\n" +

            "Weight["+j+"]:  " + aliasArr[i].weight;

    }

    return description;

 

Delete action :

    var aliasArr = fd_data._1__get_catalog_variables.apple_to_remove;

    var description = "Comments:  " + fd_data._1__get_catalog_variables.comments + "\n" +

        "List of apples to remove:  " + "\n";

    var j = 0 ;

    for (var i = 0; i < aliasArr.length; i++) {

        j = i + 1;

        description += "Size["+j+"]:  " + aliasArr[i].size_to_remove + "\n" +

            "Color["+j+"]:  " + aliasArr[i].color_to_remove + "\n" +

            "Weight["+j+"]:  " + aliasArr[i].weight_to_remove;

    }

    return description;

 

Thanks 

1 REPLY 1

Marcos Kassak
Kilo Sage
Kilo Sage

Hi @test3445,

 

I would recommend you to add a parameter to differentiate the action, something along these lines:

 

 

// Parameter to differentiate actions - DONT FORGET TO EDI
var isAddAction = fd_data._X__get_catalog_variables.is_add_action || false;

// Here I'll use the same variable as you are using, but isAddAction will help us to understand if we are adding or deleting
var aliasArr = isAddAction ? fd_data._X__get_catalog_variables.list_of_apples_to_add || [] : fd_data._X__get_catalog_variables.apple_to_remove || [];

// Start building the description based on the action type
var actionType = isAddAction ? "add" : "remove";
var description = "Comments: " + fd_data._X__get_catalog_variables.comments + "\n" +
                  "List of apples to " + actionType + ": " + "\n";

var j = 0;
for (var i = 0; i < aliasArr.length; i++) {
    j = i + 1;
    if (isAddAction) {
        // Add action
        description += "Size[" + j + "]: " + aliasArr[i].size + "\n" +
                       "Color[" + j + "]: " + aliasArr[i].color + "\n" +
                       "Weight[" + j + "]: " + aliasArr[i].weight + "\n";
    } else {
        // Delete action
        description += "Size[" + j + "]: " + aliasArr[i].size_to_remove + "\n" +
                       "Color[" + j + "]: " + aliasArr[i].color_to_remove + "\n" +
                       "Weight[" + j + "]: " + aliasArr[i].weight_to_remove + "\n";
    }
}

return description;

 

 

 

If you found my answer helpful or correct ✔️ in any way, please don't forget to mark it to help future readers! 👍

 

--

 

Kind regards,


Marcos Kassak
Solution Consultant  🎯