How to trigger the execution of a NetSuite script after a flow completes

Comments

4 comments

  • Tyler Lamparter Principal Product Manager
    Awesome Follow-up
    Engaged
    Top Contributor
    Answer Pro
    Celigo University Level 4: Legendary

    Sandra Hill there are a few ways you could handle this:

    1. Instead of a scheduled script, you can just have a postSubmit suitescript hook that IO triggers. The input you are given is and array of records that were processed and you need to respond back with an array of the same length.
    2. You could just not use IO at all and put a userEvent script on the custom record with an afterSubmit function.
    3. If you really want this scheduled script route, then you'd need to make a second flow in IO that has a "dummy" export that exports 1 record and then have an import step that calls to a RESTlet and within that RESTlet you trigger the scheduled script task. You could either establish a proxy connection to the RESTlet or you could make a HTTP connection using oauth 1.
    0
  • Sandra Hill

    Hi Tyler

    Thanks for responding so fast. I justed edited the query to include an overview of the flow.

    Due to limitations on what I can define for the supplier lookup I have to start from a item search in Netsuite (so an array of all items that we purchase from this supplier). This might be say 3000 items (whereas the supplier has 100,000). I then look up each of our 3000 records and poll back anything that has changed in say the last 24 hours), so the results might be 100 records at the lookup stage. For each of these 100 records I update the supplier data held in a custom record in NetSuite. I then need to update our own Inventory Item records in netSuite (and this refreshed data is then available for use in our Shopify/NetSuite IA flows). Therefore the script I am trying to trigger is based on a different record type to that which is updated by the flow (items and the custom record respectively). I would like this to trigger straight away rather than being on a schedule as I want to ensure the refreshed data is available for the next IA item update flow into Shopify.

    Therefore in terms of your suggestions:

    1. I think the arrays will always be different? So not sure this will work if I understand correctly

    2. I don't think I can do this as the script is over Items whereas the UserEvent is an update to a customer record?

    3. I have tried adding another flow that executes after this one completes, and this simply updates a timestamp field on a dummy item I created for the purpose. I still had the same error: expecting canonical data response with same size. This is the restlet I used:

    /**
     * @NApiVersion 2.x
     * @NScriptType Restlet
     */
    define(['N/search', 'N/record', 'N/log'], function(search, record, log) {

        function post(context) {
            var response = {
                success: true,
                message: 'Execution completed successfully',
                data: []
            };

            try {
                // Run the update process for search ID 4815
                var searchId = '4815';
                var enableOutSto = true;
                var updatedCount = updateItems(searchId, enableOutSto);

                response.data.push({
                    message: 'Updated ' + updatedCount + ' items in search ID ' + searchId + ' setting custitem_celigo_shopify_enable_out_sto to ' + enableOutSto + '.',
                    status: 'SUCCESS'
                });
            } catch (e) {
                log.error('Error executing search and updates', e);
                response.success = false;
                response.message = e.message;
                response.data.push({
                    message: e.message,
                    status: 'ERROR'
                });
            }

            return response;
        }

        function updateItems(searchId, enableOutSto) {
            var savedSearch = search.load({
                id: searchId
            });

            var searchResultCount = 0;
            savedSearch.run().each(function(result) {
                try {
                    var itemId = result.id;

                    // Load the item record
                    var itemRecord = record.load({
                        type: record.Type.INVENTORY_ITEM, // Use the correct item type
                        id: itemId,
                        isDynamic: true
                    });

                    // Set the custom field to the desired value
                    itemRecord.setValue({
                        fieldId: 'custitem_celigo_shopify_enable_out_sto',
                        value: enableOutSto
                    });

                    // Save the record
                    itemRecord.save();

                    searchResultCount++;
                } catch (e) {
                    log.error('Error updating item', 'Item ID: ' + itemId + ', Error: ' + e.message);
                    // You may choose to handle errors differently based on your requirements
                }
                return true; // Continue iterating through the search results
            });

            return searchResultCount;
        }

        return {
            post: post
        };
    });

    From what you say it sounds like this might be the way to go but instead of trying to execute the script in the restlet, I use a restlet to execute another script?

    Are you able to give me an example of how that script might look and I'll give it a go.

    Thank you!

    Sandra

    PS You could either establish a proxy connection to the RESTlet or you could make a HTTP connection using oauth 1. - I am not sure what this means or how to execute this, apologies?

    0
  • Tyler Lamparter Principal Product Manager
    Awesome Follow-up
    Engaged
    Top Contributor
    Answer Pro
    Celigo University Level 4: Legendary

    Sandra Hill within suitescript, you can shove additional search criteria into a saved search. Why can't you add criteria into the search so it only returns results for the item skus that match the custom record skus you are importing? By doing this, you could go with option 1 in my first response.

    https://stackoverflow.com/questions/47578686/suitescript-2-0-add-filters-to-saved-search-in-script 

    0
  • Sandra Hill

    Just a quick update on this: I am currently looking at building this as a scheduled script within NetSuite instead of trying to trigger the execution on demand post completion of the GET stock from from our supplier API.

    I gave a simplified background to what I was trying to achieve as it was just the final piece that was alluding me (to trigger the execution of a script). What I didn't explain is that there is no 1-1 correlation between the items that are polled via the Celigo flow and those that would be updated via the subsequent script. The script will update inventory items based on changes from other API flows that also run and other direct changes on the item record itself. The GET stock flow is the key component, but due to the other changes that are also happening, on reflection, a scheduled script makes more sense.

    Thank you for you input and suggestions.

       

    0

Please sign in to leave a comment.