UI Builder: Custom modal not updating record fields using client script/data resource
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
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).
The state I've defined like this, under the properties of the dropdown:
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:
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!