Need to validate the select services matches the service on incident form on submit.

Rhonda9
Tera Expert

Hello,

 

 I have a requirement to create a validation to prevent users from creating incidents with a service offering that does not match the service.  For example, from the incident form,the user selects the service and service offering that falls under the selected service.  Then the user decides to change the service again but forget to update the service offering.  The incident is saved with the the mismatched service and offering.  How can I prevent this?

 

I have tried creating a client script with a script include but it is giving the error message for every change , even if the service offering falls under the correct service. 

 

table (incident)

Client Script:

function onSubmit() {
    var service = g_form.getValue('business_service');
    var serviceOffering = g_form.getValue('service_offering');

    if (service && serviceOffering) {
        var ga = new GlideAjax('ServiceOfferingValidator');
        ga.addParam('sysparm_name', 'validateOffering');
        ga.addParam('sysparm_service', service);
        ga.addParam('sysparm_offering', serviceOffering);
        var result = ga.getXMLWait();
        var response = ga.getAnswer();
        if (response == 'false') {
            alert('Selected Service Offering does not belong to the selected Service.');
            return false;
        }
    }
    return true;
}
Script Include
var ServiceOfferingValidator = Class.create();
ServiceOfferingValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    validateOffering: function() {
        var service = this.getParameter('sysparm_service');
        var offering = this.getParameter('sysparm_offering');

        var offeringGR = new GlideRecord('service_offering');
        if (offeringGR.get(offering)) {
            return offeringGR.service == service ? 'true' : 'false';
        }
        return 'false';
    }
});
4 REPLIES 4

Mark Manders
Mega Patron

Why not just empty the service offering field on change of the service? You will force them to select it again. A client script can help you out here.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Okay, thank you... I will give it a try.

Rhonda9
Tera Expert

  I tried with this client script and now its not working at all.  

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Get the values of the Service and Service Offering fields
    var service = g_form.getValue('business_service');  
    var serviceOffering = g_form.getValue('service_offering');  

    console.log('Selected Service:', service);  // Log selected Service value
    console.log('Selected Service Offering:', serviceOffering);  // Log selected Service Offering value

    if (!serviceOffering) {
        console.log('No Service Offering selected, exiting function.');
        return;
    }

    var ga = new GlideAjax('ValidateServiceOffering');
    ga.addParam('sysparm_name', 'getServiceForOffering');
    ga.addParam('sysparm_offering_id', serviceOffering);
   
    ga.getXMLAnswer(function(response) {
        console.log('GlideAjax response:', response);  // Log response from GlideAjax

        // Ensure response is valid and compare with the selected service
        if (response && response !== service) {
            console.log('Mismatch found, clearing Service Offering field');  // Log mismatch
            g_form.clearValue('service_offering','');
            g_form.showFieldMsg('service_offering', 'Service Offering cleared due to mismatch with Service.', 'info');
        } else {
            console.log('Service Offering is valid, no action needed.');
        }
    });
}

Rhonda9
Tera Expert

I found another solution and it is working.  Thank you for your help!