# Conditional Calculations in Mini App Using Dynamic Logic

This guide explains how to create dynamic, conditional-based calculations inside a Mini App using form inputs and logic.

#### 1. Use Case Overview

In this example, we are building an **event registration form** where:

* Users enter basic details (Name, District, WhatsApp Number, etc.)
* Users select the **number of packs (1–4)**
* Based on the selection, additional fields dynamically appear
* A final **summary screen calculates total payable amount**

#### 2. Dynamic Field Visibility

Field visibility is controlled using conditions:

* If **Number of Packs ≥ 2** → Show fields for Person 2
* If **Number of Packs ≥ 3** → Show fields for Person 3
* If **Number of Packs = 4** → Show fields for Person 4

> Note: Conditional visibility works only for **input components**, not for text or heading elements.

#### 3. Visitor Details Logic

Each visitor includes:

* Full Name
* Gender
* Date of Birth
* Stay Required (Yes/No)

#### Pricing Logic:

* **Stay = Yes → ₹2500**
* **Stay = No → ₹750**
* **Field not present → ₹0**

#### 4. Creating Conditional Calculations

To calculate totals dynamically:

1. Define logic using **if-else conditions**
2. Convert logic into **JavaScript ternary expressions**

**Example Logic:**

```
If Stay = Yes → 2500  
If Stay = No → 750  
Else → 0
```

**Combined Formula (for multiple users):**

* Add conditions for each person (Person 1 to Person 4)
* Sum all calculated values

#### 5. Using ChatGPT for Formula Generation

You can generate formulas using prompts like:

> “Write this logic using ternary operator in JavaScript”

Then:

* Copy only the formula (ignore extra text)
* Replace static values with **dynamic field variables**

#### 6. Implementing in Mini App

* Add calculation logic inside the **same screen (Summary Screen)**
* Replace variables with actual field mappings
* Save the calculation as a **data source**

#### 7. Important Step (Mapping Fix)

Before using the formula:

1. Add a **test calculation** (e.g., `4 + 4`)
2. Click **Run & Save**
3. This enables the output to appear in **history**
4. Now replace it with your actual formula
5. Map it inside the text/body field

> Do not click “Run & Save” again after adding the real formula (no live data available)

#### 8. Displaying Calculated Values

You can display:

* Total number of packs
* Packs with stay
* Packs without stay
* Total payable amount

All values are dynamically mapped from the calculation fields.

#### 9. Key Tips

* Always create logic first, then convert to formula
* Use **dynamic variables from previous screens**
* Keep calculations on the **same screen where output is needed**
* Use images or dotted text to visually separate sections (WhatsApp UI limitation)

**Mini App Template:** *DASHBOARD\_URL* /products/flows/share/8c7400c2-92d2-4b49-8809-dd21136c17ba

{% embed url="<https://youtu.be/dJcuRPuTojg?si=M7l2UL6fptbaoaHH>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://helpdocumentation.gitbook.io/user/whatsapp-mini-apps/conditional-calculations-in-mini-app-using-dynamic-logic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
