How to Invoke Button Function with Client Script

I’ve added a button with a beforeLoad UE script. When the user clicks the button, it should create a new instance of a custom record. As I understand, you need a client script to handle the button click. I seem to be having trouble invoking my button function, though. I set the name of the function when I created the button in the UE script. Then I defined the named function in my client script. But when the button is clicked, the function in the client script is never invoked. Anyone know what I’m missing?

Beginner Asked on September 16, 2019 in SuiteScript.
Add Comment
8 Answer(s)

You have to open the Console on your browser as it seems to me your client script is throwing an error, particularly on this line:


record.create({

type : 'custrecord_trainevent'

});

‘custrecord_trainevent’ is not a record type, it is a field name. You have to change it to the correct ID of the Custom Record type.

I tried mimicking your script and it returned this error on the Console: uncaught exception: {“type”:”error.SuiteScriptError”,”name”:”INVALID_RCRD_TYPE”,”message”:”The record type [CUSTRECORD_TRAINEVENT] is invalid.

If the client function is still not getting called even after correcting this line, you have to continue debugging until the client script is 100% error free.

 

Beginner Answered on September 16, 2019.
Add Comment

Can you share the simplified code of your User Event and Client Script? Are you sure that you are setting this line properly? It should reference the correct directory of the file.


context.form.clientScriptModulePath = './LEACC_CL_for_Suitelet.js';

Beginner Answered on September 16, 2019.
Add Comment

Initially, i figured out the problem was i wasn’t specifying the client script. I tried adding it with context.form.clientScriptFileId, but i kept getting an API version error. Maybe it’s deprecated? At any rate, i switched to context.form.clientScriptModulePath, like in your example. There’s no error anymore, but still, it’s not invoking the function in the client script when the button is clicked. I know because I have a log entry when it first enters the function in the client script, but the log never occurs.

Here’s the user event script:

RE: How to Invoke Button Function with Client Script

And here’s the client script:

RE: How to Invoke Button Function with Client Script

Any thoughts? At this point, I’m just trying to get it to step into the function for the button.

Beginner Answered on September 16, 2019.
Add Comment

Right. Referencing custom records can be funky sometimes, and I knew that line could cause problems. But, that still doesn’t explain why it’s not executing the log statement before it gets to the record creation. I should at least get a log, if the function is actually being invoked, then the script throws an error for the record creation. Follow me? Since I’m not even getting that initial log, that leads me to believe the function is not being invoked at all for some reason, and I can’t figure out why.

Beginner Answered on September 16, 2019.
Add Comment

To demonstrate my point, I commented out everything in the client script function except the log. I don’t get any error, but I’m also not getting the log, which means the function is not being invoked.

RE: How to Invoke Button Function with Client Script

Beginner Answered on September 16, 2019.
Add Comment

The log wouldn’t show up in the User Event script record, as you’re on the client-side perspective when you’re expecting this. You’re better off using console.log if troubleshooting is your goal.

Beginner Answered on September 16, 2019.
Add Comment

Yeah but you should still be able to see it in the logs for the client script. I can do a log for the pageInit function and see it just fine.

Beginner Answered on September 16, 2019.
Add Comment

It looks like you’re right. You do have to use console.log to see any of the logs.

From there, i figured out the type property of record.create should be the custom record ID (e.g. ‘customrecord_trainevent’). I was getting an error with this for a while because i had the prefix wrong. In the end, though, I realized what I’m actually looking to do is not use record.create, but rather redirect the user to the entry form so they can enter the data and create the new record in the UI.

Beginner Answered on September 16, 2019.

Moving forward, avoid asking follow up questions as much as possible as it makes the thread harder to read. For example, you entitled the question “How to Invoke Button Function with Client Script” and then later on you asked about viewing the logs from the client script.

on September 17, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.