How can I call a restlet from a userevent script? ...
# suitescript
r
How can I call a restlet from a userevent script? Userevent script will trigger when a record is created through another restlet. https.request and http.post both are giving "The url must be a fully qualified url". Using restlet internal url. Even tried using the url which we got from using resolveurl (scriptid, deployment I'd). All of them giving the same error.
n
Try https.requestRestlet(options)
But why do you want to call a RESTlet from the UE? What is your use case?
r
Basically we have some integration with sales force and the current flow is when account is created/ updated there based on certain conditions One restlet is used to create prospect record or update prospect/ customer record in Netsuite. Then updating some fields on all the project records belonging to that customer. Script execution time is hitting more then 40+ seconds as project count for few customer is way too much. So I have been asked to create a custom record, store the json along with some other info. And then use a userevent script on that record to trigger another restlet to update the projects. Instead of updating project fields in the first restlet
b
that makes it slower, not faster
you still need to wait for the record to finish saving
r
That's what I was thinking as well. But my seniors are saying otherwise.
b
although theoretically you can ignore the timeout error you would get if the request takes longer than 45 seconds
though that usually considered a win
r
Even when the userevent script that is deployed on custom record is getting triggered on after submit to trigger the second suitelet to update project records. It will still not finish the process of first restlet?
b
the first restlet is making a http request that returns when it receives a response from a second restlet
n
1. Create customer from the RESTlet. 2. Add a UE on the customer record: Add the customer to the custom record for updating the project fields. 3. Use scheduled/MR script to update the project fields for each customer in custom record.
b
the second restlet is saving multiple records, its returns when all the records have been saved
n
@battk what do you think about my suggested solution above?
b
i wouldnt have done the user event script at all, make the custom record and task from the restlet
n
UE is just so RESTlet can return response after creating the customer.
r
Will it not work like this? Userevent triggers saved the record and triggers second restlet at after submit. In first restlet we get the custom record id. And return the response. What's happening in second restlet is backend processes and no data of it is required in first restlet.
b
the problem is here
Copy code
So I have been asked to create a custom record, store the json along with some other info.
And then use a userevent script on that record to trigger another restlet to update the projects.
r
They want real time sync of data. Suitelet is not an option because of governance usage. And scheduled MR if scheduled to run every day will not be a real time sync. If we are calling the MR/scheduled script through script then we will run out of deployments.
b
the second restlet is saving that custom record to trigger the user event, which does all the record saving
that second restlet will get the record id after the after submits finish
r
Got it. I was guessing the same thing and said the same. But wasn't sure. And they said otherwise, so didn't argue..
Could you provide an optimal approach for flow of data for my use case?
b
what @NickSuite, without the user event script inbetween
there are various methods of using a map/reduce or a scheduled script as a queue
the most simple of which is to have it run on a schedule every 15 minutes to process records
there are various ways of making that run faster by using N/task, but the simplicity of a schedule is great
r
System is overloaded with scheduled script and we are trying to reduce scheduled scripts. So running every 15 minutes is not optimal scenario for us. It will happen at max once every day. Which the end user is not willing to accept. Sync needs to be real time.
b
real time implies they are willing to pay the cost of waiting to do all that work now rather than later
r
Hmm. Will see. At the end I can provide only so much of suggestions.
what are the things I need to pass in header.
Copy code
var restUrl = url.resolveScript({
                scriptId: 'customscript_rest_project_update',
                deploymentId: 'customdeploy1',
                returnExternalUrl: true,
            });

var request_output = https.request({ 
                    method: 'POST', 
                    url: restUrl, 
                    body: 'hi', 
                    headers: s_headers
                });
What are the key value pairs I need to pass in s_headers when calling restlet from uservent ?
b
expect to use at least the Content-Type header
r
can you tell me what are all the parameters (keys) we have available in header? Something for sure I am missing in headers. Could use a link for all the header parameters that I need to provide when calling the restlet.
Currently https.requestRestlet is responding back with this {"type":"http.ClientResponse","code":500,"headers":{"Akamai-GRN"
b
look at the response body
r
At the moment, response body is nothing but a simple "Hi" Rest of the code in restlet is commented. Restlet is not getting triggered it self. At the very top, i have put the log with "script started" as title.
b
its unlikely that you are actually getting back your code's response
log the actual response body received
r
Yeah, putting some incorrect headers for sure. Will check