Client script in portal widget

abhaysingh98
Tera Contributor

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. 

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@abhaysingh98 

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.

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

Hi @Ankur Bawiskar , not working

the second function for record save is not executing only first function for field change is working.

yuvarajkate
Giga Guru

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:

  1. Listen to the Save Event: Use the appropriate event triggered when the record is saved in the Service Portal.

  2. 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.");
    }
});