How to trigger the execution of a NetSuite script after a flow completes
Hi all
I am trying to execute a script in NetSuite after a flow completes. I have been trying to use a postSubmit hook but get the following error: "invalid postSubmit hook response: expecting canonical data response with same size"
Very simplistically, the flow in question looks up data in our supplier API (http) and writes this to a custom record in NetSuite (where each record is the suppliers ItemCode). This data includes stock availability quantities, future stock-in dates, if the item can be exported to the UK (we deal with plants so this can change for each item, ie it is not a constant). After the data has been refreshed, I want to execute a script that determines if we can offer the item for sale on our website, estimated lead time and toggle on/off various field settings accordingly. I want to execute this script as soon as the flow has completed rather than running it on a scheduled basis.
Here's the flow
I have tried writing the script as a Restlet, map/reduce and also scheduled type but with no schedule (my understanding being that we should still be able to call this to execute on demand)
Here is a simplified version of the type of script I need to execute
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/search', 'N/record', 'N/log'], function(search, record, log) {
function execute(context) {
try {
var searchId = '4815';
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,
id: itemId,
isDynamic: true
});
// Set the custom field to true
itemRecord.setValue({
fieldId: 'custitem_celigo_shopify_enable_out_sto',
value: true
});
itemRecord.save();
searchResultCount++;
} catch (e) {
log.error('Error updating item', 'Item ID: ' + itemId + ', Error: ' + e.message);
}
return true;
});
log.audit('Scheduled Script Execution Completed', 'Updated ' + searchResultCount + ' items.');
} catch (e) {
log.error('Error executing scheduled script', e.message);
}
}
return {
execute: execute
};
});
I am therefore looking for a way to simply trigger this script to run after the flow completes. I do not need it to reference back to any of the records in the flow, ie I don't think I need a response, but suspect Celigo is looking for one and hence the error message?
Any input gratefully received.
Tagging Lakhan Bhagnani as requested
Thanks all - Sandra
Comments
Sandra Hill there are a few ways you could handle this:
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:
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?
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
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.
Please sign in to leave a comment.