Help on Celledit client script

Naga Ravindra R
Kilo Sage

I have two date fields, start date and end date.

I have written oncell edit client script like below:

 

 

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
    var saveAndClose = true;
    //Type appropriate comment here, and begin script below

    var strDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');

    if (endDate < strDate) {

        alert("st= " + strDate + '  ' + "end= " + endDate);
    }


    callback(saveAndClose);
}

 

 

 

 

But it is not working. It working fine on onchnge client script.

Any suggestions?

1 ACCEPTED SOLUTION

Rutuja Khalate
Tera Guru

Hi @Naga Ravindra R 

You can add an alert in the client script like the below:

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose=true;
var daAjax = new GlideAjax('DateValidate');
daAjax.addParam('sysparm_name', 'datevalidation');
daAjax.addParam('sysparm_sys_ids', sysIDs);
daAjax.addParam('sysparm_end_date', newValue);
daAjax.getXMLAnswer(dateResult);

function dateResult(answer){
if(answer=='false')
alert('End date should not be before start date');
callback(answer === "true");
}
}
 
And remove last 'return true; ' from script include:
 
var DateValidate = Class.create();
DateValidate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
datevalidation: function(){
 
var sys_id = this.getParameter('sysparm_sys_ids');
var endDate = new GlideDateTime(this.getParameter('sysparm_end_date'));
var events = sys_id.split(',');

for (var i = 0; i < events.length; i++) {
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', events[i]);
gr.query();
if (gr.next()) {
var strDate = new GlideDateTime(gr.u_start_date_2);
var comp_date = endDate.compareTo(strDate);
if (comp_date == -1) {
return false;
} else {
return true;
}

}
}
//return true;
},
type: 'DateValidate'
});

Please mark this response as correct or helpful if it assisted you with your question.

Best Regards,
Rutuja Khalate

View solution in original post

9 REPLIES 9

Rutuja Khalate
Tera Guru

Hi @Naga Ravindra R 

when you are in a list, g_form (which works on the FORM) isn't available, that's why it doesn't work.

onCellEdit doesn't have access to the form, it gives you some control over fields on a list but you can't do conditional stuff like this. 

There are 2 ways as below:

1. You can use a GlideAjax call in your onCellEdit() client script. You can retrieve the current value of your field.

 

2. To fulfill your requirement you can use a before insert/update business rule, set the condition and then in the actions tab tick the abort action box and add a message to your users.


Please mark this response as correct or helpful if it assisted you with your question.

Best Regards,
Rutuja Khalate

Rutuja Khalate
Tera Guru

Can you try below:

 

Client script on End Date field:

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose=true;
var daAjax = new GlideAjax('DateValidate');
daAjax.addParam('sysparm_name', 'datevalidation');
daAjax.addParam('sysparm_sys_ids', sysIDs);
daAjax.addParam('sysparm_end_date', newValue);
daAjax.getXMLAnswer(dateResult);

function dateResult(answer){
callback(answer === "true");
}
}
 
 
 
 
And script include as below:
 
var DateValidate = Class.create();
DateValidate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
datevalidation: function(){
 
var sys_id = this.getParameter('sysparm_sys_ids');
var endDate = new GlideDateTime(this.getParameter('sysparm_end_date'));
var events = sys_id.split(',');

for (var i = 0; i < events.length; i++) {
var gr = new GlideRecord('x_diocg_service_al_event');
gr.addQuery('sys_id', events[i]);
gr.query();
if (gr.next()) {
var strDate = new GlideDateTime(gr.start_date);
var comp_date = endDate.compareTo(strDate);
if (comp_date == -1) {
return false;
} else {
return true;
}

}
}
return true;
},
type: 'DateValidate'
});

Please mark this response as correct or helpful if it assisted you with your question.

Best Regards,
Rutuja Khalate

Hi @Rutuja Khalate 

 

This is working fine, but I have to show error message or alert message. If user enters end date before start date.

If I add alert, it is popping up in both scenarios. (+ve and -ve)

Rutuja Khalate
Tera Guru

Hi @Naga Ravindra R 

You can add an alert in the client script like the below:

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose=true;
var daAjax = new GlideAjax('DateValidate');
daAjax.addParam('sysparm_name', 'datevalidation');
daAjax.addParam('sysparm_sys_ids', sysIDs);
daAjax.addParam('sysparm_end_date', newValue);
daAjax.getXMLAnswer(dateResult);

function dateResult(answer){
if(answer=='false')
alert('End date should not be before start date');
callback(answer === "true");
}
}
 
And remove last 'return true; ' from script include:
 
var DateValidate = Class.create();
DateValidate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
datevalidation: function(){
 
var sys_id = this.getParameter('sysparm_sys_ids');
var endDate = new GlideDateTime(this.getParameter('sysparm_end_date'));
var events = sys_id.split(',');

for (var i = 0; i < events.length; i++) {
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', events[i]);
gr.query();
if (gr.next()) {
var strDate = new GlideDateTime(gr.u_start_date_2);
var comp_date = endDate.compareTo(strDate);
if (comp_date == -1) {
return false;
} else {
return true;
}

}
}
//return true;
},
type: 'DateValidate'
});

Please mark this response as correct or helpful if it assisted you with your question.

Best Regards,
Rutuja Khalate

Rutuja Khalate
Tera Guru

Hi @Naga Ravindra R , 

 

Please mark this response as correct or helpful if it assisted you with your question.


Please mark this response as correct or helpful if it assisted you with your question.

Best Regards,
Rutuja Khalate