Conditional mandatory field

Riya17
Giga Contributor

Hi,

I have to make 'justification' mandatory if the 'planned_start_time' is less than 3 hours from the change is requested.  I've tried with business rule, it didn't work.  What is the best way to achieve this.

 

Thanks

Riya

1 ACCEPTED SOLUTION

Same code on form load and Planed Date change

 

Thanks

View solution in original post

22 REPLIES 22

Upender Kumar
Mega Sage

Dubz
Mega Sage

Think you'll need to use an onChange client script on the planned start date field. You'll need to make an ajax call to the server to do the time calculation and return a true or false, you can then use g_form.setMandatory() to make the justification field mandatory.

Upender Kumar
Mega Sage

Hi,

 

To meets this:

1. Create a Ajax Client Script.

find_real_file.png

 

2. Call on form load and Planed Date change

var end=g_form.getValue('u_planned_start_time');
var start=g_form.getValue('opened_at');
var answer;
var ga=new GlideAjax('GetDateDifference');
ga.addParam('sysparm_name','hours');
ga.addParam('sysparm_start',start);
ga.addParam('sysparm_end',end);
ga.getXMLWait();
answer=ga.getAnswer();
if(answer<3)
g_form.setMandatory('u_justification',true);

 

thanks

Hope this will help you

 

Please Hit Correct, ️Helpful depending on the impact of the response

 

Hi Upender,

 

Could you please copy and paste the client script, I can't see anything from the screenshot.

 

 

Thank you

Riya

var end=g_form.getValue('u_planned_start_time');
var start=g_form.getValue('opened_at');
var answer;
var ga=new GlideAjax('GetDateDifference');
ga.addParam('sysparm_name','hours');
ga.addParam('sysparm_start',start);
ga.addParam('sysparm_end',end);
ga.getXMLWait();
answer=ga.getAnswer();
if(answer<3)
g_form.setMandatory('u_justification',true);

 

Thanks