Client script in portal widget
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2025 09:33 AM
Hi All,
I am new to service portal, as I tried to get a field value using client script on portal widget I am able to display message from html when field value is changed but not able to display anything when record is saved without changing the field value.
$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'is_empty1')
c.data.is_empty1 = parms.newValue;
});
This is the code I have used its working fine when I change the value and save the record but its not working when I save the record without changing the value. when I save the record without changing the value then current message does not display I want to show that message when record is saved without changing the value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2025 07:44 PM
try to update as this
// Listen for field change event
$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'is_empty1') {
c.data.is_empty1 = parms.newValue;
}
});
// Listen for record save event
$scope.$on("record.save", function(evt, parms) {
if (!parms.changes['is_empty1']) {
// Display message if 'is_empty1' field value has not changed
c.data.is_empty1 = current.is_empty1; // Assuming 'current' holds the original record data
// Add your message display logic here
alert("Record saved without changing the 'is_empty1' field value.");
}
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2025 10:14 PM - edited 01-01-2025 10:14 PM
Hi @Ankur Bawiskar , not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 08:16 AM
the second function for record save is not executing only first function for field change is working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2025 09:41 PM
To handle the scenario where the record is saved without changing the field value, you need to listen for the record save event rather than relying solely on field change events. The code you provided listens to the "field.change" event, which only triggers when a field value changes. To display the message upon saving regardless of field changes, you can use a different approach:
Listen to the Save Event: Use the appropriate event triggered when the record is saved in the Service Portal.
Add a Listener for the Save Event: Modify your code to include a listener for the record save event and include logic to display your message regardless of field changes.
// Listen for field change events
$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'is_empty1') {
c.data.is_empty1 = parms.newValue;
}
});
// Listen for the record save event
$scope.$on("record.saved", function(evt, parms) {
// Check the value of the field you are monitoring
if (c.data.is_empty1 !== undefined) {
// Display the message when the record is saved
$scope.showMessage("Record saved successfully. Current value: " + c.data.is_empty1);
} else {
$scope.showMessage("Record saved successfully. No value set for the field.");
}
});