onCellEdit Client Script

Mark Wood
Tera Contributor

Hello Experts,

I've written the script below to validate whether the start date is less than the end date or not. However, the script isn't functioning as expected. While I receive a proper confirmation message, when the start date is greater than the end date, it sets the start date field, which shouldn't happen. Instead, if the start date is greater than the end date, I want to revert the field to its old value. Please guide me on how to achieve this.

Thank you.

 

Oncelledit client script:

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
  var saveAndClose = true;
 
 var ga=new GlideAjax('global.onCellEdit_Start_Date_Scnarios');
 ga.addParam('sysparm_name','validateDates');//function name
 ga.addParam('sysparm_sysid',sysIDs);
ga.addParam('sysparm_start_date',newValue);
ga.getXML(callBack_Response);

function callBack_Response(response)
{
    var answer=response.responseXML.documentElement.getAttribute('answer');
    if(answer=='Do Not Submit')
    {
alert('please enter start date smaller than end date');
saveAndClose=false;
//newValue=oldValues;

    }
    else if(answer=='Submit it')
    {
var confirmation=confirm("Are You Sure");
saveAndClose=confirmation;

    }
}

 
 callback(saveAndClose);
}

 

Script Include:

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

validateDates:function()
{
var newDate=this.getParameter('sysparm_start_date');
var gr=new GlideRecord('incident');
gr.addQuery('sys_id',this.getParameter('sysparm_sysid'));
gr.addNotNullQuery('end_date');
gr.query();
if(gr.next())
{
if(newDate>gr.u_end_date)
{

return"Do Not Submit";

}
else if(newDate<=gr.u_end_date)
{
return "Submit it";

}

}
},


    type: 'onCellEdit_Start_Date_Scnarios'
});

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Mark Wood 

using onCell edit you cannot set field value.

you can simply stop the update

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

@Ankur Bawiskar 

I am not able to stop the update using above script.

Could you please advise on what mistakes I might be making here?

Thank you.

@Mark Wood 

script looks fine to me. what debugging have you done so far?

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 @Mark Wood ,

Did you manage to find a solution to this? If yes, can you please recall and provide some insights?