Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
By Throwaway
#812958
So, I have a process that prints data to PDFs. At one point, the number of forms could differ.

Ex.
Form A
Form B
Form C

Sometimes we may need two or three of form B and maybe 2 of form C. What would be the best way to go about adding this logic into ProcessMaker?

Best regards,
#812961
You can use conditions to determine which DynaForms are displayed to the user, so a form can be displayed zero times:
conditionForForm.png
conditionForForm.png (81.31 KiB) Viewed 7772 times

If you need the same form to be displayed multiple times, then you can use PMFRedirectToStep() in a trigger to redisplay a form another time.

The tricky part is storing the data, since the second time a DynaForm is submitted, the data will overwrite the data from the first time it was submitted. You can immediately call PMFGenerateOutputDocument() in a trigger to write the data to a PDF file before displaying the form a second time or you can store the data from the form in a grid for later usage. If you are immediately generating the Output Document, then you will want to turn on the versioning option for the Output Document, so you can see all the version that were created.

For example, Form A sets the variable "orderType" and a trigger after Form A sets the variables "formBTimes" and "formCTimes" which hold the number of times that Form B and Form C should be displayed to the user.
Code: Select all
if (@@orderType == "contract") {
   @%formBTimes = 2;
   @%formCTimes = 1;
}
elseif (@@orderType == "special_order") {
   @%formBTimes = 0;
   @%formCTimes = 3;
}
else {
   @%formBTimes = 1;
   @%formCTimes = 0;
}
//set form counters that are incremented each time a form is displayed:
@%formBCount = 0;
@%formCCount = 0;

//create empty grids to hold data from forms:
@=formBGrid = array();
@=formCGrid = array();
 
Then Form B has the condition:
@%formBTimes >= 1

And Form C has the condition:
@%formCTimes >= 1

And the trigger fired after Form B would be:
Code: Select all
//set to the names of the variables in Form B:
$aVars = array(
   "name"       => @@name,     //text box
   "hasOrder"  => @=hasOrder, //checkbox
   "hasOrder_label" => @=hasOrder_list,
   "orderList"  => @=orderList    //grid
);
//save variables to grid to store for later use:
@formBGrid[ @%formBCount ] = $aVars;

//If this trigger is fired immediately after Form B, then its variables are not yet saved to the case,
//so use PMFSendVariables() to save the variables so they can be used when generating the Output Document:
PMFSendVariables(@@APPLICATION, $aVars);

//generate Output Document:
$outDocId = 'abcde1234567890abcde1234567890'; //set to UID of Output Document
PMFGenerateOutputDocument($outDocId);

if (@%formBCount < @%formTimes) {
   @%formBCount++; //increment the Form B counter:

   $formId = '1234567890abcde1234567890abcde'; //set to the UID of Form B
   PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $formId);
}    

(This code shows how to both save Form B's data to a grid and generate an Output Document with the data, but only one is necessary.)

The trigger after Form C would be similar.
#812997
amosbatto wrote:You can use conditions to determine which DynaForms are displayed to the user, so a form can be displayed zero times:
conditionForForm.png

If you need the same form to be displayed multiple times, then you can use PMFRedirectToStep() in a trigger to redisplay a form another time.

The tricky part is storing the data, since the second time a DynaForm is submitted, the data will overwrite the data from the first time it was submitted. You can immediately call PMFGenerateOutputDocument() in a trigger to write the data to a PDF file before displaying the form a second time or you can store the data from the form in a grid for later usage. If you are immediately generating the Output Document, then you will want to turn on the versioning option for the Output Document, so you can see all the version that were created.

For example, Form A sets the variable "orderType" and a trigger after Form A sets the variables "formBTimes" and "formCTimes" which hold the number of times that Form B and Form C should be displayed to the user.
Code: Select all
if (@@orderType == "contract") {
   @%formBTimes = 2;
   @%formCTimes = 1;
}
elseif (@@orderType == "special_order") {
   @%formBTimes = 0;
   @%formCTimes = 3;
}
else {
   @%formBTimes = 1;
   @%formCTimes = 0;
}
//set form counters that are incremented each time a form is displayed:
@%formBCount = 0;
@%formCCount = 0;

//create empty grids to hold data from forms:
@=formBGrid = array();
@=formCGrid = array();
Then Form B has the condition:
@%formBTimes >= 1

And Form C has the condition:
@%formCTimes >= 1

And the trigger fired after Form B would be:
Code: Select all
//set to the names of the variables in Form B:
$aVars = array(
   "name"       => @@name,     //text box
   "hasOrder"  => @=hasOrder, //checkbox
   "hasOrder_label" => @=hasOrder_list,
   "orderList"  => @=orderList    //grid
);
//save variables to grid to store for later use:
@formBGrid[ @%formBCount ] = $aVars;

//If this trigger is fired immediately after Form B, then its variables are not yet saved to the case,
//so use PMFSendVariables() to save the variables so they can be used when generating the Output Document:
PMFSendVariables(@@APPLICATION, $aVars);

//generate Output Document:
$outDocId = 'abcde1234567890abcde1234567890'; //set to UID of Output Document
PMFGenerateOutputDocument($outDocId);

if (@%formBCount < @%formTimes) {
   @%formBCount++; //increment the Form B counter:

   $formId = '1234567890abcde1234567890abcde'; //set to the UID of Form B
   PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $formId);
}

(This code shows how to both save Form B's data to a grid and generate an Output Document with the data, but only one is necessary.)

The trigger after Form C would be similar.
Would the data be identical or is it using different vars?
User avatar
By amosbatto
#813006
Each time you submit the form, you are overwriting the existing variables. For this reason you have to immediately generate the output document with the variables and/or save the variables to a new row in the grid. That way you don't loose the data.

A 1xbet clone script is a pre-designed software so[…]

4rabet clone script is enabling entrepreneurs to e[…]

Parimatch clone script is enabling entrepreneurs t[…]

In the world of cryptocurrency, a wallet is an app[…]