Can use JavaScript to set a start date for scheduler ? | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by _Manoj_Kumar_

Hello @ragsthenos 

 

The correct way to pass dynamic values would be this:

var newStartDate="2023-10-24"; var newEndDate="2023-10-29"; var inst=xtk.workflow.load(instance.id); inst.activities.schedule[0].period="m_abDay='7' m_abDay[0]='0' m_abDay[1]='0' m_abDay[2]='0' m_abDay[3]='0' m_abDay[4]='0' m_abDay[5]='0' m_abDay[6]='0' m_abMonth='12' m_abMonth[0]='0' m_abMonth[10]='0' m_abMonth[11]='0' m_abMonth[1]='0' m_abMonth[2]='0' m_abMonth[3]='0' m_abMonth[4]='0' m_abMonth[5]='0' m_abMonth[6]='0' m_abMonth[7]='0' m_abMonth[8]='0' m_abMonth[9]='0' m_iDayMode='1' m_iMaxIter='0' m_iMonthDay='0' m_iMonthMode='0' m_iPosDay='0' m_iSpanDay='0' m_iSpanWeek='0' m_iTimeMode='1' m_iValidMode='2' m_iWeekDay='0' m_iWeekMode='0 m_tmFixedDay='' m_tmFixedTime='06:00:00.000' m_tmIterDate='' m_tmIterTime='00:00:00.000' m_tmOrgDate='' m_tmSpanTime='0s' m_tmStartDate='"+ newStartDate +"' m_tmStartTime='00:00:00.000' m_tmStopDate='"+ newEndDate +"' m_tmStopTime='00:00:00.000' m_vtmTime='0'"; inst.save();

 

Notice how I am passing the values in the code.

2 replies

_Manoj_Kumar_
Community Advisor
Community Advisor
October 24, 2023

Hello @ragsthenos 

 

You can use the following code to change the scheduler start and end date dynamically.

 

var inst=xtk.workflow.load(instance.id); inst.activities.schedule[0].period="m_abDay='7' m_abDay[0]='0' m_abDay[1]='0' m_abDay[2]='0' m_abDay[3]='0' m_abDay[4]='0' m_abDay[5]='0' m_abDay[6]='0' m_abMonth='12' m_abMonth[0]='0' m_abMonth[10]='0' m_abMonth[11]='0' m_abMonth[1]='0' m_abMonth[2]='0' m_abMonth[3]='0' m_abMonth[4]='0' m_abMonth[5]='0' m_abMonth[6]='0' m_abMonth[7]='0' m_abMonth[8]='0' m_abMonth[9]='0' m_iDayMode='1' m_iMaxIter='0' m_iMonthDay='0' m_iMonthMode='0' m_iPosDay='0' m_iSpanDay='0' m_iSpanWeek='0' m_iTimeMode='1' m_iValidMode='2' m_iWeekDay='0' m_iWeekMode='0 m_tmFixedDay='' m_tmFixedTime='06:00:00.000' m_tmIterDate='' m_tmIterTime='00:00:00.000' m_tmOrgDate='' m_tmSpanTime='0s' m_tmStartDate='2023-10-24' m_tmStartTime='00:00:00.000' m_tmStopDate='2023-10-28' m_tmStopTime='00:00:00.000' m_vtmTime='0'"; inst.save();

 

You can change the values of m_tmStartDate and m_tmStopDate to your desired values.

 

Also, the attribute "m_tmFixedTime" value is set to 06:00:00.000 in this example. It means the workflow will run every day at 6 AM.

 

 

     Manoj     Find me on LinkedIn
Level 2
October 24, 2023

Thank you very much it works well like this, I will have to adapt it to meet our need but you gave me the answer I needed:)

Level 2
October 26, 2023

Maybe it will be clearer if I show you the code. It's a start but the goal is to be able to ensure that the user only needs to enter a single date to configure schedulers. In this code there is currently only one scheduler because I am trying to assign the variables as parameters but eventually the code will update the following scheduler with 24h, 48, 72, etc depending on of the date entered. 

 

// Fonction pour formater une chaîne de caractères 'AAAA-JJ-MM' en objet Date function formatDateToJSDate(inputDate) { var dateParts = inputDate.split('-'); var year = parseInt(dateParts[0], 10); var day = parseInt(dateParts[1], 10); var month = parseInt(dateParts[2], 10); return new Date(year, month - 1, day); } // Fonction pour formater une date en 'AAAA-JJ-MM' function formatJSDateToString(jsDate) { return jsDate.getFullYear() + '-' + ('0' + (jsDate.getMonth() + 1)).slice(-2) + '-' + ('0' + jsDate.getDate()).slice(-2); } // Fonction pour ajouter un nombre d'heures à une date function addHoursToDate(jsDate, hoursToAdd) { var newDate = new Date(jsDate); newDate.setHours(newDate.getHours() + hoursToAdd); return newDate; } // Date d'origine au format 'AAAA-JJ-MM' var inputDate = '2023-29-10'; // Formater la date d'origine en objet Date var formattedDate = formatDateToJSDate(inputDate); // Formater la date en 'AAAA-JJ-MM' var formattedDateString = formatJSDateToString(formattedDate); logInfo("La date du jour est : " + formattedDateString); // Calculer la date du lendemain en ajoutant 24 heures var nextDay = addHoursToDate(formattedDate, 24); // Formater la date du lendemain en 'AAAA-JJ-MM' var formattedNextDateString = formatJSDateToString(nextDay); logInfo("La date de demain est : " + formattedNextDateString); // Mise à jour des paramètres du planificateur var inst = xtk.workflow.load(instance.id); var period = inst.activities.schedule[0].period; logInfo("chaîne de caractère des paramètres du planificateur" +inst.activities.schedule[0].period); logInfo("valeur de m_tmStartDate : "+period.m_tmStartDate); period.m_tmStartDate = formattedDateString; logInfo("valeur de m_tmStartDate après traitement : "+period.m_tmStartDate); period.m_tmStopDate = formattedNextDateString; inst.activities.schedule[0].period = period; inst.save();

 

 The problem I currently have is that the values of inst.activities.schedule[0].period.m_tmStartDate and inst.activities.schedule[0].period.m_tmEndDate are undefined. whereas they should be formattedDateString and formattedNextDateString respectively

 

Thank you for your help

_Manoj_Kumar_
Community Advisor
_Manoj_Kumar_Community AdvisorAccepted solution
Community Advisor
October 26, 2023

Hello @ragsthenos 

 

The correct way to pass dynamic values would be this:

var newStartDate="2023-10-24"; var newEndDate="2023-10-29"; var inst=xtk.workflow.load(instance.id); inst.activities.schedule[0].period="m_abDay='7' m_abDay[0]='0' m_abDay[1]='0' m_abDay[2]='0' m_abDay[3]='0' m_abDay[4]='0' m_abDay[5]='0' m_abDay[6]='0' m_abMonth='12' m_abMonth[0]='0' m_abMonth[10]='0' m_abMonth[11]='0' m_abMonth[1]='0' m_abMonth[2]='0' m_abMonth[3]='0' m_abMonth[4]='0' m_abMonth[5]='0' m_abMonth[6]='0' m_abMonth[7]='0' m_abMonth[8]='0' m_abMonth[9]='0' m_iDayMode='1' m_iMaxIter='0' m_iMonthDay='0' m_iMonthMode='0' m_iPosDay='0' m_iSpanDay='0' m_iSpanWeek='0' m_iTimeMode='1' m_iValidMode='2' m_iWeekDay='0' m_iWeekMode='0 m_tmFixedDay='' m_tmFixedTime='06:00:00.000' m_tmIterDate='' m_tmIterTime='00:00:00.000' m_tmOrgDate='' m_tmSpanTime='0s' m_tmStartDate='"+ newStartDate +"' m_tmStartTime='00:00:00.000' m_tmStopDate='"+ newEndDate +"' m_tmStopTime='00:00:00.000' m_vtmTime='0'"; inst.save();

 

Notice how I am passing the values in the code.

     Manoj     Find me on LinkedIn
Level 2
October 26, 2023

Thank you very much, it works now,

I couldn't remember the syntax to use variables in this case.

Thanks again