Trigger a flow action from UI Action for configurable workspace

Marina1
Tera Contributor

Hi!

 

I'm trying to trigger an action in Flow Designer from a UI Action that will be used in CSM Workspace. I've reproduced this functionality by following this video How to trigger a subflow from a client side script? | GlideFlow | Flow Designer - YouTube, which works fine. The difference here is that I'd like to trigger the action from the Workspace Client Script, but without success. 

 

Currently I'm trying to call the onClick function from the Workspace Client Script, but that does not seem to work either.

 

Marina1_0-1674129610716.png

Here I want to call the action

 

Marina1_1-1674129679683.png

Here I want to call the function that triggers the action

 

function createPDF() {
    var inputs = {};

    inputs['aktivitetsrapport'] = { // GlideRecord 
        table: 'x_arams_kont_arb_aktivitetsrapport',
        sys_id: g_form.getUniqueValue()
    };
    inputs['kontrollarende'] = { // GlideRecord 
        table: 'x_arams_kont_arb_kontrollarenden',
        sys_id:'c108ff1c5c3b1d902eebc455b63bd94b'
    };

    GlideFlow.startAction('x_arams_kont_arb.skapa_pdf_frn_html', inputs)
        .then(function(execution) {
            return execution.awaitCompletion();
        }, errorResolver)
        .then(function(completion) {

            var status = completion.status;

        });

    function errorResolver(error) {
        // Handle errors in error resolver
    }
}

Script (not workspace)

function onClick(g_form) {

    var fields = [];
    var ga = new GlideAjax('x_arams_kont_arb.createPdfUtils');
    ga.addParam('sysparm_name', 'getCases');
    ga.getXMLAnswer(populateChoiceField);

    function populateChoiceField(response) {
        var msg = response;
        var answer = JSON.parse(msg);
        fields.push({
            type: 'choice',
            name: 'arenden',
            label: getMessage('Välj det kontrollärende du vill spara ned PDF:en på'),
            choices: answer.choices,
            mandatory: true
        });

        g_modal.showFields({
            title: "Spara som PDF",
            fields: fields,
            size: 'lg'
        }).then(function(fieldValues) {
            //g_form.setValue('description', fieldValues.updatedFields[0].displayValue); //kalla istället på flöde som sparar ner som PDF
            //alert(fieldValues.updatedFields[0].value); //hämtar sys_id på det ärende som väljs
			createPDF();
        });
    }
}

Workspace Client Script

 

Appreciate all help I can get!

Marina

2 REPLIES 2

Prachi_Jain
Tera Contributor

Hi Marina,

 

To trigger a flow or subflow from the workspace client script within a UI Action, please consider the following:

  • Ensure that the client checkbox is unchecked.
  • Utilize API classes or functions that are supported within the Workspace, such as g_modal.

 

Please try the below code:

function onClick(g_form) {
var fields = [];
var ga = new GlideAjax('x_arams_kont_arb.createPdfUtils');
ga.addParam('sysparm_name', 'getCases');

// Asynchronous call to get choices
ga.getXMLAnswer(function(response) {
var msg = response;
var answer = JSON.parse(msg);

// Push choices field to fields array
fields.push({
type: 'choice',
name: 'arenden',
label: getMessage('Välj det kontrollärende du vill spara ned PDF:en på'),
choices: answer.choices,
mandatory: true
});

// Show modal to user with options
g_modal.showFields({
title: "Spara som PDF",
fields: fields,
size: 'lg'
}).then(function(fieldValues) {
// Prepare inputs for flow
var inputs = {};

inputs['aktivitetsrapport'] = {
table: 'x_arams_kont_arb_aktivitetsrapport',
sys_id: g_form.getUniqueValue()
};
inputs['kontrollarende'] = {
table: 'x_arams_kont_arb_kontrollarenden',
sys_id: 'c108ff1c5c3b1d902eebc455b63bd94b'
};

// Start Flow Action
GlideFlow.startAction('x_arams_kont_arb.skapa_pdf_frn_html', inputs)
.then(function(execution) {
// Await the completion of the execution
return execution.awaitCompletion();
})
.then(function(completion) {
// Handle completion status
var status = completion.status;
// Optionally, you can log or handle the status here
})
.catch(function(error) {
// Handle any errors in execution or awaitCompletion
errorResolver(error);
});
});
});
}

// Error handling function
function errorResolver(error) {
// Log the error or show a notification to the user
console.error("Error encountered: " + error.message);
// Optionally, display an error message to the user
g_form.showFieldMsg('arenden', 'An error occurred while processing the request.', 'error');
}

 

I hope this was helpful. If so, I would appreciate it if you could mark this answer as the 'correct solution' or provide a thumbs up. Thank you!


Prachi

 

Prachi_Jain
Tera Contributor

Hi Marina

To trigger a flow or subflow from the workspace client script within a UI Action, please consider the following:

  • Ensure that the client checkbox is unchecked.
  • Utilize API classes or functions that are supported within the Workspace, such as g_modal.

 

 

Please try with the below code for workspace client :

function onClick(g_form) {
var fields = [];
var ga = new GlideAjax('x_arams_kont_arb.createPdfUtils');
ga.addParam('sysparm_name', 'getCases');

ga.getXMLAnswer(function(response) {
var msg = response;
var answer = JSON.parse(msg);

fields.push({
type: 'choice',
name: 'arenden',
label: getMessage('Välj det kontrollärende du vill spara ned PDF:en på'),
choices: answer.choices,
mandatory: true
});

g_modal.showFields({
title: "Spara som PDF",
fields: fields,
size: 'lg'
}).then(function(fieldValues) {

var inputs = {};

inputs['aktivitetsrapport'] = {
table: 'x_arams_kont_arb_aktivitetsrapport',
sys_id: g_form.getUniqueValue()
};
inputs['kontrollarende'] = {
table: 'x_arams_kont_arb_kontrollarenden',
sys_id: 'c108ff1c5c3b1d902eebc455b63bd94b'
};

// Start Flow Action
GlideFlow.startAction('x_arams_kont_arb.skapa_pdf_frn_html', inputs)
.then(function(execution) {
return execution.awaitCompletion();
})
.then(function(completion) {
var status = completion.status;

})
.catch(function(error) {
errorResolver(error);
});
});
});
}

function errorResolver(error) {
g_form.showFieldMsg('arenden', 'An error occurred while processing the request.', 'error');
}

 

 

I hope this helped. If yes, I would appreciate if you could mark this answer as "correct solution" or give a thumbs up. Thank you!

Prachi