Planned End Date validation

Priyanka_77
Tera Contributor

Hi,

There is sn_risk_risk table and there are 4 risk response tables 

Risk Acceptance - sn_risk_acceptance_task
Risk Avoidance - sn_risk_avoidance_task
Risk Transfer - sn_risk_transfer_task
Risk Mitigation - sn_risk_mitigation_task

and all these 5 tables have u_planned_end_date field.

Now when I select the u_planned_end_date on the Risk response table then it should be less than the u_planned_end_date on the risk table.

Below is the script include and client script

Script include:

var IRM_RiskUtils = Class.create();
IRM_RiskUtils.prototype = {
    initialize: function() {},
   
    comparePlannedDates: function(riskSysID, response_planned_end_date) {
        var riskGR = new GlideRecord('sn_risk_risk');
        riskGR.addQuery('sys_id', riskSysID);
        riskGR.query();
       
        if (riskGR.next()) {
            var riskPlannedDate = new GlideDate(riskGR.u_planned_end_date);
            var responsePlannedDate = new GlideDate(response_planned_end_date);
           
            // Compare planned dates
            if (responsePlannedDate.after(riskPlannedDate)) {
                return false; // Planned End Date on Response Task is after Planned End Date on Risk
            } else {
                return true; // Planned End Date on Response Task is on or before Planned End Date on Risk
            }
        }
        return true; // Default to true if no risk record found (optional)
    },

    type: 'IRM_RiskUtils'
};
 
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var riskId = g_form.getValue('sn_risk_risk');
   var plannedEndDate = g_form.getValue('u_planned_end_date');
   var ga = new GlideAjax('IRM_RiskUtils');
   ga.addParam('sysparam_name','comparePlannedDates');
   ga.addParam('sysparam_riskId', riskId);
   ga.addParam('sysparam_plannedEndDate', plannedEndDate);
   ga.getXMLAnswer(function(response) {
        if (response == 'false') {
            g_form.showErrorBox('u_planned_end_date', 'Planned End Date cannot be greater than Risk Planned End Date');
            g_form.setValue('u_planned_end_date', oldValue);
        }
    });


   //Type appropriate comment here, and begin script below
   
}
this is not working
4 REPLIES 4

Community Alums
Not applicable

Hi @Priyanka_77 ,

 

Try the below code:

 

Client Script

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

    var riskId = g_form.getValue('sn_risk_risk');
    var plannedEndDate = g_form.getValue('u_planned_end_date');
    var ga = new GlideAjax('IRM_RiskUtils');
    ga.addParam('sysparam_name', 'comparePlannedDates');
    ga.addParam('sysparam_riskSysID', riskId);
    ga.addParam('sysparam_response_planned_end_date', plannedEndDate);
    
    ga.getXMLAnswer(function(response) {
        if (response === 'false') {
            g_form.showErrorBox('u_planned_end_date', 'Planned End Date cannot be greater than Risk Planned End Date');
            g_form.setValue('u_planned_end_date', oldValue);
        }
    });
}

 

Script Include

var IRM_RiskUtils = Class.create();
IRM_RiskUtils.prototype = {
    initialize: function() {},
   
    comparePlannedDates: function(riskSysID, response_planned_end_date) {
        var riskGR = new GlideRecord('sn_risk_risk');
        riskGR.addQuery('sys_id', riskSysID);
        riskGR.query();
       
        if (riskGR.next()) {
            var riskPlannedDate = new GlideDateTime(riskGR.getValue('u_planned_end_date'));
            var responsePlannedDate = new GlideDateTime(response_planned_end_date);
           
            // Compare planned dates
            if(responsePlannedDate.getNumericValue()>riskPlannedDate.getNumericValue()) {
                return 'false'; // Planned End Date on Response Task is after Planned End Date on Risk
            } else {
                return 'true'; // Planned End Date on Response Task is on or before Planned End Date on Risk
            }
        }
        return 'true'; // Default to true if no risk record found (optional)
    },

    type: 'IRM_RiskUtils'
};

 

I started answering community questions recently. If my answer helped you in any way, please mark it as helpful or correct. It would be a great boost.

 

Hi  Sai149,

I tried the above code but still I am able to set the Planned end date greater than the risk planned end date.

Community Alums
Not applicable

Can you add alert in client script to check whether is script include is returning true or false when greater date is selected.

alert(response)

Hayo Lubbers
Kilo Sage

Hi @Priyanka_77 ,

 

Why asking the same question twice within 24h and with 2 different accounts? Please check the answer(s) given on your initial question:

https://www.servicenow.com/community/virtual-agent-forum/field-validation/m-p/2938398#M7940