Expand my Community achievements bar.

SOLVED

Can use JavaScript to set a start date for scheduler ?

Avatar

Level 2

Hello.

 

I'd like to know if we have the possibility to make the configuration of the "scheduler" activity dynamic by a variable

for exemple, set up a start date or end date dynamically

 

Thank you.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

6 Replies

Avatar

Community Advisor

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

Avatar

Level 2

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:)

Avatar

Level 2

Hello @_Manoj_Kumar_ 

 

After a few tries, I can change the dates, but only if I write them down directly. When I create a variable and call that variable in the m_tmStartDate and m_tmStopDate parameters, it doesn't work. I tried adding this (directly and as a variable): 

 

inst.activities.schedule[0].period.m_tmStartDate = '2023-12-12';
inst.activities.schedule[0].period.m_tmStopDate = '2023-12-12';

 

 

But it doesn't work either.

 

I also add that if I want to change the dates of a scheduler in the same workflow, I will have to run it once for it to update the dates, and then a second time for it to be taken into account, am I right? If that's the case, I'll have to find a way to restart the workflow automatically

 

Any idea ?

Avatar

Level 2

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

Avatar

Correct answer by
Community Advisor

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

Avatar

Level 2

Thank you very much, it works now,

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

Thanks again