27
Views
9
Comments
Solved
How to pass dynamic values as array from list to javascript to run a task
Application Type
Mobile

Hello ,

I have one javascript code in which i want to pass dyanamic time (which is stored in list)in array,How can i pass this array values.

Below is my Javascript code.

$parameters.TimeoutId = 0;

 $parameters.SetInterval = 0;

//var interval=0;


// Schedule specific task at n times every day

function scheduleTasks(times, task) {

    const now = new Date();


    times.forEach((time) => {

        const [hours, minutes, seconds] = time.split(":").map(Number);


        const scheduleTime = new Date();

        scheduleTime.setHours(hours, minutes, seconds, 0);


        // If the schedule time is in the past for today, schedule for tomorrow

        if (scheduleTime < now) {

            scheduleTime.setDate(scheduleTime.getDate() + 1);

        }


        const delay = scheduleTime - now;


        // Schedule the task

        setTimeout(() => {

            task();

            // Reschedule for the next day

            setInterval(task, 24 * 60 * 60 * 1000);

        }, delay);

    });

}


// Example usage

const taskTimes = ["16:26:00", "16:29:00", "16:31:00"]; // Times in HH:mm:ss format

Here i want to pass dynamic values coming through list in outsystems in same format.

This hardcoded values works fine 

const myTask = () => {

      $actions.TriggerOfflineDataSync("gate-logs");

//$actions.TriggerOfflineDataSync()

    alert("Task executed at", new Date().toLocaleTimeString());

};



scheduleTasks(taskTimes, myTask);



Yours help will be appreciated.

Thanks in advance



 

2024-12-02 13-16-47
Vipin Yadav
Solution

Hi,

Convert the List to JSON -

  • Inside your OutSystems logic, use ListToJSON or prepare the list in a JSON format:
    • If you have a list of times like ["13:26:00", "13:29:00", "13:31:00"], you will create a string:

    • Assign the JSON string to a local variable called TaskTimesJSON.

Pass JSON to JavaScript -

var taskTimes = JSON.parse($parameters.TaskTimesJSON); // Parse the JSON string into an array


function scheduleTasks(times, task) {

    const now = new Date();


    times.forEach((time) => {

        const [hours, minutes, seconds] = time.split(":").map(Number);


        const scheduleTime = new Date();

        scheduleTime.setHours(hours, minutes, seconds, 0);


        if (scheduleTime < now) {

            scheduleTime.setDate(scheduleTime.getDate() + 1);

        }


        const delay = scheduleTime - now;


        setTimeout(() => {

            task();

            setInterval(task, 24 * 60 * 60 * 1000);

        }, delay);

    });

}


const myTask = () => {

    $actions.TriggerOfflineDataSync("gate-logs");

    alert("Task executed at " + new Date().toLocaleTimeString());

};


// Schedule tasks using dynamic times

scheduleTasks(taskTimes, myTask);



Thanks,

Vipin Yadav

2024-12-02 13-16-47
Vipin Yadav

I have added another screenshot for your references -


Thanks,

Vipin Yadav

2023-02-28 05-45-34
Bharat koshti

Hey thanks Vipin, for the reply

but i get another error msg .

Plz check the below screenshot.

2023-02-28 05-45-34
Bharat koshti
2023-02-28 05-45-34
Bharat koshti
Solution

Thanks vipin for your valuable response but i achieved this with another way by using javscript.

var nestedJsonArray = JSON.parse($parameters.In1);

var namesArray = nestedJsonArray.map(item => item.SyncTime2.Time);

alert(namesArray); 

$parameters.Out1 = namesArray;

-bind this output to output variable which object data type n passed this to my next javascript as input variable with same data type .



2024-12-02 13-16-47
Vipin Yadav

Ok thanks 

2024-12-02 13-16-47
Vipin Yadav
Solution

Hi,

Convert the List to JSON -

  • Inside your OutSystems logic, use ListToJSON or prepare the list in a JSON format:
    • If you have a list of times like ["13:26:00", "13:29:00", "13:31:00"], you will create a string:

    • Assign the JSON string to a local variable called TaskTimesJSON.

Pass JSON to JavaScript -

var taskTimes = JSON.parse($parameters.TaskTimesJSON); // Parse the JSON string into an array


function scheduleTasks(times, task) {

    const now = new Date();


    times.forEach((time) => {

        const [hours, minutes, seconds] = time.split(":").map(Number);


        const scheduleTime = new Date();

        scheduleTime.setHours(hours, minutes, seconds, 0);


        if (scheduleTime < now) {

            scheduleTime.setDate(scheduleTime.getDate() + 1);

        }


        const delay = scheduleTime - now;


        setTimeout(() => {

            task();

            setInterval(task, 24 * 60 * 60 * 1000);

        }, delay);

    });

}


const myTask = () => {

    $actions.TriggerOfflineDataSync("gate-logs");

    alert("Task executed at " + new Date().toLocaleTimeString());

};


// Schedule tasks using dynamic times

scheduleTasks(taskTimes, myTask);



Thanks,

Vipin Yadav

2024-12-02 13-16-47
Vipin Yadav

I have added another screenshot for your references -


Thanks,

Vipin Yadav

2023-02-28 05-45-34
Bharat koshti

Hey thanks Vipin, for the reply

but i get another error msg .

Plz check the below screenshot.

2023-02-28 05-45-34
Bharat koshti
2023-02-28 05-45-34
Bharat koshti

i got this json output from outsystems after serialize but i want only time like ["16:26:00", "16:29:00", "16:31:00"];

[{"SyncTime2":{"Id":25,"Time":"17:27:00"}},{"SyncTime2":{"Id":26,"Time":"17:31:00"}}]

2024-12-02 13-16-47
Vipin Yadav

Hi @Bharat koshti,

You can create a structure with a single attribute, then assign a local variable to this structure in the form of a list. Set the 'Time' variable to "17:27:00", serialize it to JSON, and then pass the value to JavaScript. 

Thanks,

Vipin Yadav

2023-02-28 05-45-34
Bharat koshti
Solution

Thanks vipin for your valuable response but i achieved this with another way by using javscript.

var nestedJsonArray = JSON.parse($parameters.In1);

var namesArray = nestedJsonArray.map(item => item.SyncTime2.Time);

alert(namesArray); 

$parameters.Out1 = namesArray;

-bind this output to output variable which object data type n passed this to my next javascript as input variable with same data type .



2024-12-02 13-16-47
Vipin Yadav

Ok thanks 

2021-09-06 15-09-53
Dorine Boudry
 
MVP

The idea of using JSON is the more important part of this solution, how exactly you use it in your javascript is an implementation detail.

So I also marked the answer of @Vipin Yadav as solution.

Dorine

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.