Newvalue and oldvalue in onsubmit Client script

Anburaj
Giga Contributor

I have requirement like in change request , while change manager is approving the change request

if proposed start date or proposed end date is changed that time, i need to throw a error message.

I need to do this scenario in onsubmit client script.

I got the proper query, but these proposed date change am having the problem.

using get value i got the proposed start date or proposed end date in a variable.

But in need to set new value of proposed start date != old value of proposed start date

or newvalue of proposed end date != old value of proposed end date.

How to achieve this in client script onsubmit?

Please help.

1 ACCEPTED SOLUTION

PriyaRanji
Tera Guru

Hi Anburaj,



Good Day!



function onSubmit() {


var approver = g_form.getValue('approver');


if(approver != ''){
validateTravelEndDate();


return false;
}


else{


return true;


}


}



//Helper function which calls a AJAX script include called "ClientDateTimeUtils" which gives the response in a callback where i am deciding whether to submit the form or not based on the status of days result.

function validateTravelEndDate() {
  var startDate = g_form.getValue('proposed_start_date'); //First Date/Time field
  var endDate = g_form.getValue('proposed_end_date'); //Second Date/Time field
  var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.

  var ajax = new GlideAjax('ClientDateTimeUtils'); // This is the script include which can be used for date validation.
  ajax.addParam('sysparm_name', 'getDateTimeDiff');
  ajax.addParam('sysparm_fdt', startDate);
  ajax.addParam('sysparm_sdt', endDate);
  ajax.addParam('sysparm_difftype', dttype);
  ajax.getXML(checkDateDiff);
}

// callback function where deciding to go ahead or not with form submission.
function checkDateDiff(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  if (answer <= 0) {
  alert("Proposed End date must be after Proposed Start date.");
g_form.setValue('proposed_end_date', '');
  g_form.showFieldMsg('proposed_end_date', 'Please provide a future date', 'error');
  return false;
} else {
  g_form.submit(); // This has some issue as it's going in the infinite loop and if we just return true/false from here as it's asynchronous call , it's not handled by the onSubmit function
  }
}



PS: hit correct/helpful....if it helps




Thanks,


Priyanka R


View solution in original post

16 REPLIES 16

Fabian Kunzke
Kilo Sage
Kilo Sage

Hello,



For accessing the previous and new/current values, use a onSubmit business rule instead. This will also prevent, if the update is done via the list.



Kind regards



Fabian


I want a error message need to pop , so its not possible to via BR.


Hello Anburaj,



That is only parcially correct. It depends on, if the onSubmit action you are reffering to triggers a loading action on the client side (which an onSubmit action normally does). In that case, you can add a message, which is displayed after the page is reloaded.



If however your Submit action is not triggering a reload of the page (nor a redirect to another page), your best chance will be an onChange Client Script. In this case you need to check for a changed value, due to hitting the submit button (e.g. include a variable which changes, when hitting submit). Then you can check for your old and new values.



Kind Regards



Fabian Kunzke


Madhu27
Tera Expert

Why don't you consider OnChange client script? It has oldvalue and newvalue.



Thanks


PS: hit correct/helpful....if it helps