Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Abhijeet_Pawar
Tera Guru
Tera Guru

Hello @Jeff42 ,

Please use modified code I hope this should work for you.

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 = g_request.getParameter('u_contract_name');
        var date = g_request.getParameter('u_contract_start_date');
        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;
   }

   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 exists, please adjust');
        }
   });
}

 

Thanks @Abhijeet_Pawar 

var name = g_request.getParameter('u_contract_name'); --- not working
I think the key point is Date Value. Client get_velue don't match Server site Value.
because if I remove Contract Start Date(Date) Parameter, just have Contract Name(String), the script is good.