Hello everyone, would anyone know if it's possible...
# general
u
Hello everyone, would anyone know if it's possible to pass data from a User Event to a Client script? Thanks in advance.
b
use the user event script to write data to a field on the record
👀 1
read the data from the field in the client script
1
u
hi @battk, thanks for responding. I got a bit confused by your second reply, but I'm assuming you meant "data" instead of "date". While your response did work upon testing, it required me to create another field on the record, which I thought was too much work for passing a value through. I wanted to keep everything contained within the script, since the value I was trying to pass was already in the record anyway; I just needed a way to access it since N/currentRecord can't retrieve field values from Client Scripts while in View mode. I eventually figured out a cleaner solution by utilizing the keys of the context parameter in the beforeLoad of the UE, then passing that to the CS through the addButton method. Here's the code snippet for anyone who might need it in the future (and for me): UE
Copy code
function beforeLoad(context) {
      	var rec = record.load({type:record.Type.ESTIMATE, id:context.newRecord.id});
		var chk = rec.getValue({fieldId: "fieldInternalId"});

      	context.form.addButton({
			id:'custpage_makebutton',
			label:'Button',
			functionName:'onClickTheButton(' + chk + ')'});

		context.form.clientScriptModulePath = "SuiteScripts/Scripts/CS_ButtonFunc.js";
}
CS
Copy code
function onClickTheButton(param) {  
	  var gencheck = param;
      if(param == true)
      {
		//code goes here
      } else if(param == false) {
        //code goes here
      } else {
        //code goes here
      }

}
b
i personally wouldnt expect that code to work as written most of the time
when used that way, functionName behaves like a string used for eval
unless the chk variable holds something like a boolean or a number, the javascript engine will evaluate the value as a variable identifier
🙌 1
u
Surprisingly, it worked for me. Granted I only pasted the functions that mattered instead of the entire script, it successfully passes the value of a checkbox just fine. For my specific use-case of needing to send the value of a bool, it works. Without a doubt there's definitely a much cleaner approach to it, but this is the one that worked seamlessly for me. There's likely a cleaner way of doing this for other value types, but since the boolean value converts to "true" or "false" in text form anyway, it goes just fine with single quotes.
b
if its a string, you need to add quotes
and theoretically escape any quotes in the value of chk
u
I cleaned up my code and went back here to re-read what you meant. I think I understand what you mean now. Looking closer, I made the oversight of encapsulating the if-condition in the CS with quotes, treating it as a string instead of a proper true/false value. Not quite sure why it's working on my end, but for properly-written code's sake, I've edited mine and the code I wrote in this thread to reflect it. For clarity, the value I was trying to pass was a checkbox boolean. My solution was to load the record using the context.newRecord.id as a point of reference, then retrieve the value from there. Since Client Scripts cannot (to my current knowledge) load field values via currentRecord.getValue while in View mode, this is the cleanest method I've ended up with. I'd like to know if there's anything else I could improve with this. I understand you (and many others) are more versed in this, so if you could offer any advice to better this code, I'd appreciate it. Thanks.
b
its the double equals you used instead of triple
does conversion between types
first person to run your code through a linter would probably fix that with === and possibly cause an error
i personally dont like generating eval strings
i would just load the record or do a lookup from the client script to get whatever value im looking for from the record
in your particular case, a boolean is simple enough that you may simply want 2 different functions in your client script
otherwise your other option is reading the dom to find the field yourself
what i originally suggested, add a field to the form (via script if you want) works if you use an linline html field, gives you slightly better control over the query selector you use to read the dom
1