how to trigger client script function on record lo...
# ask-stanley-ai
d
how to trigger client script function on record load
c
AI generated answer (using internal Slack threads):
To trigger a client script function on record load, you can use the
pageInit
function in your client script. This function is automatically triggered when a record is loaded. Here is an example of how you can define this function in your client script:
Copy code
javascript
/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(['N/record'],
  function(record) {
    function pageInit(scriptContext) {
      // Your code here
    }
    
    return {
      pageInit: pageInit,
    };
});
In the
pageInit
function, you can add the code that you want to execute when the record is loaded. However, if you want to trigger a function on a button click, you can add a button in the
beforeLoad
function of your User Event script and set the
functionName
property to the name of the function in your client script that you want to trigger. Here is an example:
Copy code
javascript
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record'],
  function(record) {
    function beforeLoad(context) {
      if(context.type != context.UserEventType.VIEW) {
        var newSoRecord = context.newRecord;
        context.form.clientScriptFileId = 427704;
        context.form.addButton({
          id: "custpage_calculatecommission",
          label: "Calculate Commission",
          functionName: "calculatecommission"
        });
      }
    }
    
    return {
      beforeLoad: beforeLoad,
    };
});
In this example, when the button with the id
custpage_calculatecommission
is clicked, the
calculatecommission
function in your client script will be triggered. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.823576
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.818756461
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.816433489
Please provide your feedback on this answer.