Backing up and restoring system variables in triggers

Unofficial documentation how to do interesting things and work around problems in ProcessMaker

Moderator: amosbatto

Forum rules
Unofficial documentation for features which have not been tested by Quality Assurance or may change in future versions of ProcessMaker
amosbatto
Posts: 5566
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

Backing up and restoring system variables in triggers

Unread postby amosbatto » Tue Aug 15, 2017 8:26 pm

Many ProcessMaker functions will change the system variables if they are executed for another case or with another user. For example, if executing PMFDerivateCase() with a case which isn't the current case or with a user who isn't the current logged-in user, then the system variables will be changed, which can cause problems executing the next step in the task or routing to the next task after the trigger finishes. To avoid these problems, it is necessary to call G::sessionVarSave() before executing the ProcessMaker function and then call G::sessionVarRestore() before the trigger ends.

G::sessionVarSave()
G::sessionVarSave() stores the session variables so they can be restored later using G::sessionVarRestore(). It may be necessary to use this function when calling PM Functions for cases which aren't the current case or for users which are the current logged-in user. For example, it is necessary to use this function if wishing to call PMFDerivateCase() for a case which isn't the current case, send an email in another case with PMFSendMessage(), or use PMFGenerateOutputDocument() to generate an Output Document file in another case assigned to a different user.

Definition:
void G::sessionVarSave()

Return Value:
None.

Note: This function saves the following session variables, which correspond to the system variables:
$_SESSION["APPLICATION"], $_SESSION["INDEX"], $_SESSION["PROCESS"], $_SESSION["TASK"],
$_SESSION["USER_LOGGED"], $_SESSION["USR_USERNAME"], $_SESSION["STEP_POSITION"]


Example:
Generate an Output Document for a case which is selected in a previous DynaForm.

Code: Select all

if (isset(@@caseId) and !empty(@@caseId)) {
   $g = new G();
   $g->sessionVarSave();
   PMFGenerateOutputDocument(@@outputDocId, @@caseId, @%index, @@userId);
   $g->sessionVarRestore();
}


G::sessionVarRestore()
G::sessionVarRestore() restores the session variables that were previously saved with the G::sessionVarSave() function.

Definition:
void G::sessionVarRestore()

Return Value:
None.

Note: This function restores the following session variables, which correspond to the system variables:
$_SESSION["APPLICATION"], $_SESSION["INDEX"], $_SESSION["PROCESS"], $_SESSION["TASK"],
$_SESSION["USER_LOGGED"], $_SESSION["USR_USERNAME"], $_SESSION["STEP_POSITION"]


Example:
Route on all the other open tasks in the current case, except the current task:

Code: Select all

$caseId = @@APPLICATION;
$index = @%INDEX;
$sql = "SELECT * FROM APP_DELEGATION WHERE APP_UID='$caseId' AND
   DEL_INDEX<>$index AND DEL_THREAD_STATUS='OPEN'";
$aTasks = executeQuery($sql);
$g = new G();
$g->sessionVarSave();
foreach ($aTasks as $aTask) {
   PMFDerivateCase($aTask['APP_UID'], $aTask['DEL_INDEX'], true); 
}   
$g->sessionVarRestore();
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

ElizabethMireles
Posts: 1
Joined: Wed Nov 08, 2017 4:06 am
Country: United States of America
Location: USA, New York
Contact:

Re: Backing up and restoring system variables in triggers

Unread postby ElizabethMireles » Wed Nov 08, 2017 4:08 am

Thank you for the notification! I will ask you sooner or later, if any questions arise.

User avatar
programerboy
Posts: 68
Joined: Mon Aug 28, 2017 4:22 am
Country: Iran
Location: Iran, Mashhad

Re: Backing up and restoring system variables in triggers

Unread postby programerboy » Fri Nov 10, 2017 10:10 am

Thanks for your tips...


Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests