Page 1 of 1

Save all case variables and set them back later

Posted: Sun Aug 13, 2017 2:53 am
by mustapah
In the testing phase, triggers can go wrong. I want to recover case variable state before trigger execution so I don't have to create a new case for every change in trigger's code of a task. To solve the problem, I'm thinking of a simple work around: save all case variables before triggers execution, and set them back at the beginning of the task.

How can I save all case variables at once, and set them back later? Any suggestions?

Re: Save all case variables and set them back later

Posted: Tue Aug 15, 2017 9:08 pm
by amosbatto
First, read this about how PM stores case variables:
http://wiki.processmaker.com/3.0/Trigge ... le_Storage
You also might want to read this:
viewtopic.php?f=47&t=713218&p=794510#p794510

You can create a table which has a backup copy of APP_DATA field. For example, if you have a PM Table named "VAR_BACKUP" with the fields APP_UID and APP_DATA:
BackupVarsPMTable.png
BackupVarsPMTable.png (32.08 KiB) Viewed 3544 times
Then, use this trigger code to store the data:
Code: Select all
$caseId = @@APPLICATION;
//delete any existing records for this case in the backup table:
$sql = "DELETE FROM PMT_VAR_BACKUP WHERE APP_UID='$caseId'";
executeQuery($sql);

$sql = "INSERT INTO PMT_VAR_BACKUP (APP_UID, APP_DATA) VALUES ('$caseId', (SELECT APP_DATA FROM APPLICATION WHERE APP_UID='$caseId'))";
executeQuery($sql);
Then you can use this trigger code to later restore the APP_DATA:
Code: Select all
$caseId = @@APPLICATION;
$sql = "UPDATE APPLICATION SET APP_DATA=(SELECT APP_DATA FROM PMT_VAR_BACKUP WHERE APP_UID='$caseId') WHERE APP_UID='$caseId'";
executeQuery($sql);
Also, if you are using a recent version of PM, then you will need to edit the blacklist file so you can write to the
APPLICATION table. See: http://wiki.processmaker.com/3.0/Consul ... ore_Tables