How to trigger a Scheduled Job based on the value given on the form

User177031
Kilo Guru

Hi All,

I was given a requirement to trigger scheduled job based on the value given by user on a field in the form. 

Can someone help on how to validate the value and get the scheduled job triggered.

Requirement : On a custom table 'u_abc', I have a field 'u_due_date' where user can give only numbers 1-21 if 'run_on' (choice field on u_abc table) was selected as Monthly and if the 'run_on' was selected as Quarterly then user can give numbers between 1-63 on 'u_due_date'. 

If the User select 'run_on' as Monthly and provide 20 on the 'u_due_date' then the scheduled job should trigger only when the day is 21.

If the User select 'run_on' as Quarterly and provide 59 on the 'u_due_date' then the scheduled job should trigger only when the day is 59.

I tried below code for weekly schedule but not sure how to get this done for Monthly, Quarterly & Yearly.

Scheduled Job : Runs daily at 12:01 AM EST

Script : 

function checkFirstMonday(){
var answer = false;
var ordinals = ["", "1", "2", "3", "4", "5"];
var date = String(new Date());
var tokens = date.split(" ");
for(i = 0; i< tokens.length; i++){
gs.log('tokens[i]== ' + tokens[i]);
}

var floatNumber = ordinals[Math.ceil(tokens[2]/7)];
gs.log( "floatNumber == " + floatNumber);

var abc = new GlideRecord('u_abc');
abc.addEncodedQuery('u_due_dateISNOTEMPTY^u_run_on=month^u_active=true');
abc.query();
while (abc.next()){
if(abc.u_due_date == floatNumber)
answer = true;
else
answer = false;

return answer;
}
}
checkFirstMonday();

 

- Thank you.

2 REPLIES 2

Rahul Talreja
Mega Sage
Mega Sage

Hi,

You can refer the below link to call schedule job in a script,
via this you can write a on change client script and add a conditional logic and trigger the schedule job.

https://community.servicenow.com/community?id=community_question&sys_id=ab4f4fe1dbdcdbc01dcaf3231f96...

Please mark helpful/correct whichever applicable.

Thanks and regards,
Rahul

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

User177031
Kilo Guru

@Ankur Bawiskar 

can you please help me on this?

- Thank you.