Hi guys, Is there any way to do stuff with a curr...
# suitescript
r
Hi guys, Is there any way to do stuff with a current record object in a Suitelet? Basically, I am calling from a client script a Suitelet, in order to do some logic from the server side, within the Suitelet, for performance reasons. I was wondering whether instead of extracting the information in the client script, and then sending the information to the suitelet to do some logic, I could directly send the whole current record of the client script to the suitelet to do both the extraction of the information + the logic Is it possible? Or I really need to do that first part in the client script? Please note that I do not want to load the record in the Suitelet or anything like that, just wanna use the record in the state that it is after triggering an entry point, like for example, fieldChanged() Thanks!
Any idea whether that's possible? Otherwise I will do as usual and extract the information about the current record in the Suitelet.
s
What performance gain are looking to get from calling a suitelet from a client, sounds like bad user experience if you are doing that much on a client interaction anyway.
r
Basically from a change on a given body field, there are a few checks to perform for each item of the sublist on that record. Might be multiple lines to do some logic that implies searches, etc. And based on the result of that information there are some message to display, that's why a client script is being use. So, I could do all from the client script but I believe (and I had that feeling when I took the same approach) that is quite faster vs doing all from the client script. So that's why this doubt came to me.
e
Perhaps I'm not understanding the architecture or the question completely, but: 1. If you have a Client Script attached to a Suitelet form, you can use the
N/currentRecord
module to retrieve the values currently selected in the form fields 2. If you are only trying to leverage the Client Script because it has existing logic in it that you need, then the existing logic should instead be moved to a common custom module; both your Client Script and your Suitelet can then pull in the custom module and leverage it separately
r
Thank you Eric for your reply and apologies if my message was not clear enough. The point 1st that you have described does not apply for my case, as my client script is deployed to regular NetSuite records, such as Sales Orders. And for your 2nd point, I am indeed using a custom module that both scripts are using for different functions. But, because of the nature of the triggering (Client script, body field changed), and the input needed for the logic - multiple values of the record where the fieldChange was triggered - currentRecord - that's why I was thinking whether I could just send the whole currentRecord as a param to the SuiteLet, and the Suitelet to take care of getting the values in the current record besides doing the logic needed. In other words - even thought both scripts are using the same module, each script is calling different functions within that module, so I want to move all the extraction + logic to the functions to the Suitelet that is being called. And is the extraction part of a currentRecord object in a Suitelet where I was wondering whether is possible at all.
b
no choice really, the suitelet input basically only accepts a form post or a query string, both of which are transformed into a regular javascript object, or a string body
the current record has no serialization/stringify mechanism
you would have to implement both the serialization and deserialization of current record yourself
r
Yes, indeed, I am implementing the serialization and deserialization of the current record. I have use this similar code in the past with success to pass data between both scripts:
Copy code
// client script

 var data = JSON.stringify(currentRecord);	
 data = window.btoa(data);


//Then send currentRecord to the suitelet via the client script

scCreateRecordURL = url.resolveScript({
        	                scriptId: 'scriptidSuitelet',
        	                deploymentId: 'deploymentIdSuitelet',
        	                params: {
        	                    data: data
        	                }
        	            });
        	            response = <http://https.post|https.post>({
        	                url: scCreateRecordURL,
        	                body: data
        	            });



   // Suitelet 

   if(scriptContext.request.method === 'POST') {
            var params = scriptContext.request.parameters;
            var response = scriptContext.response;
            var data = params.data || ''; 
            
            if(data !== '') {
                data = encode.convert({
                    string: data,
                    inputEncoding: encode.Encoding.BASE_64,
                    outputEncoding: encode.Encoding.UTF_8
                });
                data = JSON.parse(data);
            }
            
            
            // and by here is when I try to do any operations with the current record, that I am getting errors

            ....
I believe that's what you are referring to regarding serialization and deserialization. So the thing is that I believe I have access from the Suitelet to exactly the same object as in the client script, the current record, yet I cannot do operations with it as a normal record. Maybe there is where it lays the question: Is there any difference of a current record object with a record object? Because I know currentRecord library is not available for server side scripts, but yet somehow I was expecting I would be able to work on it as a normal record
b
i wouldnt expect
JSON.stringify(currentRecord);
to work
currentRecord doesn't implement the toJSON method required to stringify useful information
its why i said you would need to write code to change it into a string yourself
same for the suitelet, there is nothing to change a string back into a currentRecord (or at least an object that implements the currentRecord interface)
you would have to do that yourself
again, you don't really have a choice in the matter, you either have to do lots of work, or change the interface of your custom module
r
Ah, I see. I think I still do not fully understand what a currentRecord object is in fact. Because I thought I could apply methods such as stringify to it. But either way, I will then simply extract the information from the currentRecord in the client script and then sent it out to the Suitelet to process. I was trying to explore other ways to see what could be done to gain some performance, and learn a bit more on the way Thank you very much, @battk
b
JSON.stringify stringifies keys on the object, the data of the currentRecord are not contained within keys on the object
most obviously seen by taking a look at the current record in the console or debugger