Script Include Date Query

Jeff42
Tera Contributor

Dear all,

I would like to check if the record is exist when end user input with Onchange client script and Script include, below my script, but not working, could anyone help me:

Thank you!

 

Script Include:

var CM_Name_Date_Count = Class.create();
CM_Name_Date_Count.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    checkNameDate: function(){
        var namedate_count = new GlideAggregate('sn_hr_core_contract_management');
        var name = this.getParameter('u_contract_name');
        //gs.info('HRIS Test contract name ' + name);
        var date = this.getParameter('u_contract_start_date');
        //gs.info('HRIS Test contract start date ' + start);
        namedate_count.addQuery('u_contract_name', name);
        namedate_count.addQuery('u_contract_start_date', date);
        namedate_count.addAggregate('COUNT');
        namedate_count.query();
        var answer = false;
        while(namedate_count.next()){
            if(namedate_count.getAggregate('COUNT') >= 1){
                answer = true;
            }
        }
        return answer;
    },

    type: 'CM_Name_Date_Count'
});
 
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Type appropriate comment here, and begin script below

   var contractName = g_form.getValue('u_contract_name');
   //alert('HRIS Test contract name ' + contractName);
   var contractStartDate = g_form.getValue('u_contract_start_date');
   //alert('HRIS Test contract start date ' + contractStartDate);

   var ga = new GlideAjax('sn_hr_core.CM_Name_Date_Count');
   ga.addParam('sysparm_name', 'checkNameDate');
   ga.addParam('u_contract_name', contractName);
   ga.addParam('u_contract_start_date', contractStartDate);

          ga.getXMLAnswer(function(answer) {
        if (answer == 'true') {
            alert('This contract is exist, please adjust');
            return;
        }
    });
    return false;
}
6 REPLIES 6

Aniket Chavan
Tera Sage
Tera Sage

Hello @Jeff42 ,

 

Please give a try to the modified script below and see how it works for you.

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

    // Type appropriate comment here, and begin script below

    var contractName = g_form.getValue('u_contract_name');
    var contractStartDate = g_form.getValue('u_contract_start_date');

    g_form.addInfoMessage('Checking for contract name: ' + contractName + ' and start date: ' + contractStartDate);

    var ga = new GlideAjax('sn_hr_core.CM_Name_Date_Count');
    ga.addParam('sysparm_name', 'checkNameDate');
    ga.addParam('u_contract_name', contractName);
    ga.addParam('u_contract_start_date', contractStartDate);

    ga.getXMLAnswer(function(answer) {
        g_form.addInfoMessage('Server response: ' + answer);

        if (answer === 'true') {
            alert('This contract exists, please adjust');
            // Additional logic if needed
            // For example, you might want to clear or reset the form fields
            g_form.clearValue('u_contract_name');
            g_form.clearValue('u_contract_start_date');
        }
        // Return false to prevent form submission if needed
        return false;
    });
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

Thanks Aniket Chavan 

not working, the result I want is when user input the same value: Contract Name + Contract Start Date, system will Alert which script include answer is true.

Screenshot 2024-02-16 144703.png

 

@Jeff42 Ohh okay then lets try with the code below and see how it works for you.

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

    // Type appropriate comment here, and begin script below

    var contractName = g_form.getValue('u_contract_name');
    var contractStartDate = g_form.getValue('u_contract_start_date');

    // Debugging: Add information message to the form
    g_form.addInfoMessage('Checking for contract name: ' + contractName + ' and start date: ' + contractStartDate);

    var ga = new GlideAjax('sn_hr_core.CM_Name_Date_Count');
    ga.addParam('sysparm_name', 'checkNameDate');
    ga.addParam('u_contract_name', contractName);
    ga.addParam('u_contract_start_date', contractStartDate);

    ga.getXMLAnswer(function(answer) {
        // Debugging: Add information message to the form
        g_form.addInfoMessage('Server response: ' + answer);

        if (answer == 'true') {
            alert('This contract exists, please adjust');
            // Additional logic if needed
            // For example, you might want to clear or reset the form fields
            g_form.clearValue('u_contract_name');
            g_form.clearValue('u_contract_start_date');
        } else {
            // If the response is 'false' or anything other than 'true'
            // Add any additional logic here if needed
        }
        
        // Return false to prevent form submission if needed
        return false;
    });
}

Maddysunil
Kilo Sage

@Jeff42 

The GlideAjax callback function is asynchronous, so returning false at the end of the onChange function won't prevent the form submission. Instead, you should use the callback function to set a variable indicating whether the contract exists or not and then use that variable to decide whether to allow the form submission.

Here's an updated version of your client script:

 

 

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

    // Type appropriate comment here, and begin script below

    var contractName = g_form.getValue('u_contract_name');
    var contractStartDate = g_form.getValue('u_contract_start_date');

    var ga = new GlideAjax('sn_hr_core.CM_Name_Date_Count');
    ga.addParam('sysparm_name', 'checkNameDate');
    ga.addParam('u_contract_name', contractName);
    ga.addParam('u_contract_start_date', contractStartDate);

    ga.getXMLAnswer(function (answer) {
        if (answer == 'true') {
            alert('This contract is already exist, please adjust');
            // Set a variable indicating that the contract exists
            g_form.setValue('u_contract_already_exists', true);
        } else {
            // Set a variable indicating that the contract doesn't exist
            g_form.setValue('u_contract_already_exists', false);
        }

        // Trigger a submission only if the contract doesn't exist
        if (!g_form.getValue('u_contract_already_exists')) {
            onSubmit();
        }
    });

    // Returning false here won't prevent form submission,
    // instead, use the callback function to decide whether to submit the form
}

 

 

Kindly mark helpful/accepted if it helps you.

Thanks