Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Ui action script doesn´t work

Big Alex
Tera Contributor

I'm creating a UI action (button) in the ServiceNow sc_task table called "Create Story". The button should trigger a process to create a story record in the rm_story table and copy fields and attachments from the related Requested Item (RITM) to the new story. However, the button isn't working as expected because the script isn't even being called. I need guidance on how to implement the script so that clicking the button starts the flow or executes the necessary actions. We've already tried all approaches and configurations, and since the script isn't running, even looking at the log table is pointless. 

 

The script runs on background scripts interface, so the script is ok. But why on earth he doesn´t run in the UI action is a mystery!

 

Thanks in advance!

11 REPLIES 11

@Big Alex 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Big Alex 

Use this simplified code (remove the function wrapper):

if (current && current.assigned_to != '570743494740765045a1fb72036d431f') {
    current.assigned_to = '570743494740765045a1fb72036d431f';
    current.update();
    action.setRedirectURL(current);
    gs.addInfoMessage('Atribuído ao usuário padrão com sucesso.');
} else {
    gs.addInfoMessage('Registro já atribuído ou inválido.');
}

 

Happy to help! If this resolved your issue, kindly mark it as the correct answer   and Helpful and close the thread 🔒 so others can benefit too.

Warm Regards,

Deepak Sharma

Community Rising Star 2025


 

Laveena-Agarwal
Kilo Sage

Hi @Big Alex 

 

Remove "current &&" from your If statement and it should work.

MaxMixali
Kilo Sage

Step 1: Verify UI Action Configuration

Navigate to System UI > UI Actions and open your "Create Story" UI Action. Confirm these settings:

  1. Table: sc_task
  2. Action name: create_story (no spaces, lowercase)
  3. Active: Checked
  4. Form button: Checked
  5. Show insert: Unchecked
  6. Show update: Checked
  7. Client: Unchecked (this is critical - must be false for server-side execution)
  8. Onclick: Leave blank (do NOT put code here if Client is unchecked)
  9. Condition: Leave blank initially for testing
  10. Script field: This is where your server-side code goes

Step 2: Correct Script Implementation

Since your background script works, place it in the Script field (NOT the Onclick field):

 

(function() {
// Get the current sc_task record
var task = current;

// Get the related RITM
var ritm = task.request_item.getRefRecord();

if (!ritm || !ritm.isValidRecord()) {
gs.addErrorMessage('No valid Requested Item found for this task');
return;
}

// Create the story record
var story = new GlideRecord('rm_story');
story.initialize();

// Copy fields from RITM to Story
story.short_description = ritm.short_description;
story.description = ritm.description;
// Add other field mappings as needed

var storyID = story.insert();

if (storyID) {
// Copy attachments from RITM to Story
var sa = new GlideSysAttachment();
sa.copy('sc_req_item', ritm.sys_id, 'rm_story', storyID);

gs.addInfoMessage('Story ' + story.number + ' created successfully');

// Optional: Redirect to the new story
action.setRedirectURL(story);
} else {
gs.addErrorMessage('Failed to create story');
}

})();

 

 

Step 3: Critical Settings for Server-Side Execution

The Client checkbox determines execution context:

  • Client = false (unchecked): Script runs on SERVER → Use the Script field → Can access GlideRecord, current, action
  • Client = true (checked): Script runs in BROWSER → Use the Onclick field → Cannot access server objects directly

 

 

prajaktajga
Tera Expert

Hello @Big Alex ,
You can check the below points one more time for debugging

1.Ensure the client check box not checked
2.Check the Condition field if your condition returns false, the UI Action loads but the script will never run.
3.Check Form button and Show insert, Show update ensure the button is visible under the correct conditions.