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 Builder: Custom modal not updating record fields using client script/data resource

ronro2
Tera Contributor

Hi!

 

In servicenow UI Builder I have a problem with updating two fields on a custom table (x_vgll_rail_ticket). My business need is that I have to choose state (either state 6 - resolved or state 8 - closed incomplete) and it is mandatory to leave a comment (close_notes).

I want to be able to do this in a custom modal that has a text field that is synced to close_notes and becomes mandatory + a dropdown with the different statuses synced/mapped as well (as seen in the screenshots below).

ronro2_0-1765652110304.png

 

The state I've defined like this, under the properties of the dropdown: 

ronro2_1-1765652309972.png


But it doesnt do anything. The dropdown is binded to an empty client state param called  'selectedStateValue' as seen in the screenshot above. And the textarea is binded to an empty state param called 'closeNotes'. But the data doesn't get passed down. 

I have the following client script binded to the Primary button: 

function handler({api, event, helpers, imports}) {
  // 1. Hämta sys_id för ärendet från Client State (du måste ha valt ärendet innan modalen öppnas)
  let recordId = api.state.selectedTicketId;

  // 2. Hämta värden från modalen
  let closeNotes = api.state.closeNotesValue;
  let selectedState = api.state.selectedStateValue;

  // 3. Validera att båda fälten är ifyllda
  if (!closeNotes || !selectedState) {
    alert("Du måste fylla i kommentar och välja status!");
    return;
  }

  // 4. Bygg fälten som ska uppdateras
  let fields = `state=${selectedState},close_notes=${closeNotes}`;

  // 5. Anropa data resource för att uppdatera ärendet
  api.data.updateRailTicket.execute({
    table: "x_vgll_rail_ticket",
    recordId: recordId,
    templateFields: fields,
    useSetDisplayValue: false
  });

  // 6. Logga i konsolen för felsökning
  console.log(`Ärende ${recordId} uppdaterat med state=${selectedState} och kommentar.`);
}

 

Here are the primary button properties, which is binded to the client script shown above: 

ronro2_2-1765652690891.png

 

What could be wrong? The issue today is that the record doesn't get updated. Nothing happens in other words, no matter what state I choose. 

I have a hard time with programming so please show be thoroughly. The script I have now I created with the help of AI. 


Thanks in advance! 

 

0 REPLIES 0