How to pass variable values to a process to use, using either field workflow variable or using JSON API?

Confluence User - 19 Mar, 2025

Context

I have a form called allocate lecturer, where the user will select a class and assign a lecturer to that class, currently the system doesn't require the lecturers to have an account in the system so I am fetching their emails from an external API. The process I have only has an email tool. The form uses a JSON API to initiate/start the process, and it's supposed to send an email to the respective lecturer.

Issue



In the email tool, I tried to call the workflow variable as such above, to fetch the lecturer email from the form, but it doesn't seem to work, I am assuming that the way I am trying to get the variable value is incorrect.

I made sure the variable was added to the process also and everything else that is required, but not even the testing email is sending to the respective email.


I tried to use the JSON API that passes variables to the process. I don't think it is working, I assume that is because I logged in as a different role other than "admin"???

Image related > reference.

Below is how I implemented the JSON 

            const testingEmail = "comeonbro1233@gmail.com";  

            function setProcessVariable(processInstanceId, variableName, callback) { 
                // Construct the URL to set the variable using query parameter format  
                const setVariableUrl = `https://joget-iis-dev.apu.edu.my/jw/web/json/workflow/process/variable/${processInstanceId}?${variableName};  
                console.log('Process Variable Set URL:', setVariableUrl);  

                fetch(setVariableUrl, {  
                    method: 'POST',  
                    credentials: 'include', // Important for maintaining session authentication  
                    headers: {  
                        'Content-Type': 'application/json'  
                    }  
                })  
                .then(response => {  
                    // Check if the response is OK  
                    if (!response.ok) {  
                        throw new Error(`HTTP error! status: ${response.status}`);  
                    }  
                    return response.json();  
                })  
                .then(data => {  
                    console.log('Process Variable Set Response:', data);  
                    
                    // Call the callback function if provided  
                    if (typeof callback === 'function') {  
                        callback(null, data);  
                    }  
                })  
                .catch(error => {  
                    console.error('Error setting process variable:', error);  
                    
                    // Call the callback with error if provided  
                    if (typeof callback === 'function') {  
                        callback(error, null);  
                    }  
                });  
            }  



            function startProcess(processDefId, callback) {  
                const startUrl = `https://joget-iis-dev.apu.edu.my/jw/web/json/workflow/process/start/${processDefId}`;  

                fetch(startUrl, {  
                    method: 'POST',  
                    credentials: 'include', // Important for maintaining session authentication  
                    headers: {  
                        'Content-Type': 'application/json'  
                    },  
                    body: JSON.stringify({  
                        callback: null  
                    })  
                })  
                .then(response => {  
                    if (!response.ok) {  
                        throw new Error(`HTTP error! status: ${response.status}`);  
                    }  
                    return response.json();  
                })  
                .then(data => {  
                    console.log('Process Start Response:', data);  
                    if (typeof callback === 'function') {  
                        callback(null, data);  
                    }  
                })  
                .catch(error => {  
                    console.error('Error starting process:', error);  
                    if (typeof callback === 'function') {  
                        callback(error, null);  
                    }  
                });  
            }  





                function saveEdit() {
                    const processInstanceId = "apiit_iis:8:assign_lecturer"; // Replace with actual ID
                    
                    // Set process variable, but continue with process start regardless of outcome
                    setProcessVariable(processInstanceId, testingEmail, (error) => { / / Testing Email is just an example of how I am assuming I should add the variable to the JSON API.
                        if (error) {
                            console.error("Failed to set process variable:", error);
                            // Continue with process start despite error
                        }
                        
                        // Start process regardless of setProcessVariable outcome
                        startProcess(processInstanceId, (error, data) => {
                            if (error) {
                                console.log("Failed to start process:", error);
                            } else {
                                console.log("Process started successfully:", data);
                            }
                        });
                    });
                }



I just hope to find a way to pass the variable to process, I might want to pass other variables not just the email. but related data that I get from the form to pass to the email and send it as content etc...

forms;process;email;api;json

1


09 Apr, 2025
confluenceUser
confluenceUser

Hello, regarding this issue, you don't need to actually use the "Set Process Variable"  json API, you can just use the start process URL and add the parameters as such:

In a nutshell, if this is the url

https://joget-iis-dev.apu.edu.my/jw/web/json/workflow/process/start/${processId}

You just add parameters

https://joget-iis-dev.apu.edu.my/jw/web/json/workflow/process/start/${processId}variable=example&variable2=example


In this specific case for example: it would look like:

            function startProcess(processId, variables) {
                // Encode variables as URL parameters
                const params = new URLSearchParams(variables).toString();
                const url = `---dev.apu.edu.my/jw/web/json/workflow/process/start/${processId}?${params}`;
               
                console.log("Process Start URL:", url);


                fetch(url, {
                    method: 'POST',
                    credentials: 'include',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': 'Basic ' + btoa('admin:admin' //you need this in order to have the correct authentication to make it work.),
                        'Accept': 'application/json'
                    },
                    body: JSON.stringify({
                        variables: variables


Calling the function above and assigning the variable values as such below:


               

                // Define process variables
                const processId = "apiit_iis:18:assign_lecturer"; // Use the correct process ID
                const variables = {
                    email: "lectureremail@----"

v,
                };               
                // Start the workflow process
                startProcess(processId, variables);
            }

Then in the process you call the stated variable values as such:

#requestParam.email#
                    })
                })

 

RELATED QUESTIONS

Your answer


To answer a question you'll need an account.

Print