# Help to improve my script step. Cut down on IF-statements

**URL:** https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664
**Category:** Flow Classic
**Tags:** portal
**Created:** [April 16, 2024, 9:09am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664 "2024-04-16T09:09:57Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![vikolo01](https://avatars.discourse-cdn.com/v4/letter/v/7C9FD7/32.png) [@vikolo01](https://forum.novacura.com/u/vikolo01)
#### Post date: [April 16, 2024, 9:09am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/1 "2024-04-16T09:09:57Z")

</div>

Hi! I need tips on improving my script step for handling a large number of tasks, and I want to avoid using a lot of if statements as much as possible. Can someone point me in the right direction for assigning my tasks and their status to a table in a more efficient way. This just a small part of the code. It’s 28 tasks…

Really appreciate the help!

Code:

// declare table  
let tasks = table(taskName, taskId, activitySeq);

// Loop through my tasks and check if it’s 1 or 0.  
FOR i IN InputTable DO  
let activitySeq = i.Aktivitetsekven;

```
IF i.task1 IN (1,0) THEN
    let taskName = i.task1;
    let taskId = i.task1_ID;
    set tasks = tasks & [taskName: taskName, taskId: taskId, activitySeq: activitySeq];
END    

IF i.task2 IN (1,0) THEN
    let taskName2 = i.task2;
    let taskId2 = i.task2_ID;
    set tasks = tasks & [taskName: taskName2, taskId: taskId2, activitySeq: activitySeq];
END

IF i.task3 IN (1,0) THEN
    let taskName3 = i.task3;
    let taskId3 = i.task3_ID;
    set tasks = tasks & [taskName: taskName3, taskId: taskId3, activitySeq: activitySeq];
END

IF i.task4 IN (1,0) THEN
    let taskName4 = i.task4;
    let taskId4 = i.task4_ID;
    set tasks = tasks & [taskName: taskName4, taskId: taskId4, activitySeq: activitySeq];
END

IF i.task5 IN (1,0) THEN
    let taskName5 = i.task5;
    let taskId5 = i.task5_ID;
    set tasks = tasks & [taskName: taskName5, taskId: taskId5, activitySeq: activitySeq];
END

IF i.task6 IN (1,0) THEN
    let taskName6 = i.task6;
    let taskId6 = i.task6_ID;
    set tasks = tasks & [taskName: taskName6, taskId: taskId6, activitySeq: activitySeq];
END

IF i.task7 IN (1,0) THEN
    let taskName7 = i.task7;
    let taskId7 = i.task7_ID;
    set tasks = tasks & [taskName: taskName7, taskId: taskId7, activitySeq: activitySeq];
END

```

DONE  
// return table  
return tasks;

---

<div class="post-metadata">

### Author: ![Alluse](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.novacura.com/alluse/32/1013_2.png) [@Alluse](https://forum.novacura.com/u/Alluse)
#### Post date: [April 16, 2024, 9:35am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/2 "2024-04-16T09:35:18Z")

</div>

What does your InputTable look like?

---

<div class="post-metadata">

### Author: ![vikolo01](https://avatars.discourse-cdn.com/v4/letter/v/7C9FD7/32.png) [@vikolo01](https://forum.novacura.com/u/vikolo01)
#### Post date: [April 16, 2024, 10:36am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/3 "2024-04-16T10:36:40Z")

</div>

Here it is.

 ![Namnlös](https://europe1.discourse-cdn.com/flex013/uploads/novacuratalk/original/1X/06633f5d4b77f792f076572627e31c1cfde650e8.png)

---

<div class="post-metadata">

### Author: ![Alluse](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.novacura.com/alluse/32/1013_2.png) [@Alluse](https://forum.novacura.com/u/Alluse)
#### Post date: [April 17, 2024, 1:15pm UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/4 "2024-04-17T13:15:35Z")

</div>

So basically a table with a two columns formatted all weird. 🙂 . Isn’t it possible to fetch the data in a correct way from the start?

---

<div class="post-metadata">

### Author: ![vikolo01](https://avatars.discourse-cdn.com/v4/letter/v/7C9FD7/32.png) [@vikolo01](https://forum.novacura.com/u/vikolo01)
#### Post date: [April 17, 2024, 1:43pm UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/5 "2024-04-17T13:43:45Z")

</div>

Yes, I understand that it doesn’t look professional. But if we put that aside for now, the data comes from an SQL query with a pivot where you should be able to update information via the portal. Therefore, I need ‘task’ = what is the name of the task, ‘task\_id’ = what is the ID of the task, and ‘activity sequence’ = a unique number for those tasks.

I need to group them in a table and then check if the input is 1 or 0. Then do a modify update on the task table.

---

<div class="post-metadata">

### Author: ![Alluse](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.novacura.com/alluse/32/1013_2.png) [@Alluse](https://forum.novacura.com/u/Alluse)
#### Post date: [April 17, 2024, 2:14pm UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/6 "2024-04-17T14:14:19Z")

</div>

I guess you have a good reason for structuring the data this way; but I’m curious on how your Portal looks.

---

<div class="post-metadata">

### Author: ![OlaCarlander](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.novacura.com/olacarlander/32/205_2.png) [@OlaCarlander](https://forum.novacura.com/u/OlaCarlander)
#### Post date: [April 17, 2024, 2:21pm UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/7 "2024-04-17T14:21:01Z")

</div>

Hi,

Not sure if this helps, if not please ignore. But I have found use to do this below to unpivot in the update when working with pivoted data. Like this:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/novacuratalk/original/1X/0013a46ed79de522c3dea8c8c67fd03957b8f576.png)

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/novacuratalk/original/1X/e96c5c821c1317abd9652264515f973aa76ab25c.png)

---

<div class="post-metadata">

### Author: ![vikolo01](https://avatars.discourse-cdn.com/v4/letter/v/7C9FD7/32.png) [@vikolo01](https://forum.novacura.com/u/vikolo01)
#### Post date: [April 18, 2024, 4:43am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/8 "2024-04-18T04:43:38Z")

</div>

It’s my first attempt at creating a portal, so I understand that it may not be perfect. The portal consists of a table that retrieves data from a database as its data source. This table is populated with tasks that will be later checked and updated within our ERP system, IFS.

I’m open to suggestions for improvement as I continue to develop and refine the portal.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/novacuratalk/original/1X/7c30eba024b375148615d517845b60a144b51a24.png)  
Database query.

SELECT  
\*  
FROM (  
SELECT  
ifsapp.SUB\_PROJECT\_API.Get\_Description(a.project\_id,a.sub\_project\_id) as “Beskrivning”,  
a.name as “Namn”,  
a.status AS “Status”,  
a.activity\_seq “Aktivitetsekven”,  
a.task\_id “Uppgiftsid”  
FROM ifsapp.ACTIVITY\_TASK\_EXT2 a  
WHERE  
ifsapp.activity\_api.Get\_Activity\_Responsible(activity\_seq) = ‘:UserName’  
AND a.project\_id = ‘7700’  
AND a.activity\_no = ‘010’  
AND a.sub\_project\_id LIKE ‘400%’  
AND a.activity\_no IS NOT NULL  
AND a.early\_start \> ‘2023-10-01’  
AND a.status IN (0, 1)  
AND a.project\_id IN (  
SELECT project\_id  
FROM ifsapp.activity b  
WHERE b.objstate = ‘Released’  
AND upper( b.C\_PROCESS\_FLOW ) \<\> upper( ‘Fakturerad’)  
AND trunc(b.date\_created) \>= ‘2023-01-01’  
AND a.activity\_no = b.activity\_no  
)  
)  
PIVOT (  
MAX(“Status”),  
MAX(“Uppgiftsid”) AS “ID”  
FOR “Namn” IN (  
‘1.01.1 - Platsbesök Bokad & DK Signal 1’ as “task1”,  
‘1.01.2 - Platsbesök’ as “task2”,  
‘1.02.1 - Slot-Begäran Skickad’ as “task3”,  
‘1.02.2 - Slot-Begäran Godkänd’ as “task4”,  
‘1.03.1 - FBL & DK Signal 2’ as “task5”,  
‘1.04.1 - GVB’ as “task6”,  
‘1.05.1 - Mängdning’ as “task7”,  
‘1.06.1 - AMP’ as “task8”,  
‘1.07.1 - Arbetshandlingar Skapade’ as “task9”,  
‘1.07.2 - Arbetshandlingar Delade’ as “task10”,  
‘1.08.1 - Materiel Beställt’ as “task11”,  
‘1.08.2 - Materiel Levererat’ as “task12”,  
‘1.09.1 - Tillstånd Ansökt’ as “task13”,  
‘1.09.2 - Tillstånd Godkänd & DK Signal 4’ as “task14”,  
‘1.10.1 - Avtal Fastighetsägare Ansökt’ as “task15”,  
‘1.10.2 - Avtal Fastighetsägare Godkänd & DK Signal 5’ as “task16”,  
‘1.11.1 - TA-plan Ansökt’ as “task17”,  
‘1.11.2 - TA-plan Godkänd’ as “task18”,  
‘1.12.1 - Utförande Resurser Bokade’ as “task19”,  
‘2.01.1 - Schakt & DK Signal 6’ as “task20”,  
‘2.02.1 - Geodetisk Mätning Beställd’ as “task21”,  
‘2.02.2 - Geodetisk Mätning Mottagen’ as “task22”,  
‘2.03.1 - Dagböcker’ as “task23”,  
‘2.04.1 - Dokumentation’ as “task24”,  
‘2.05.1 - Fakturor’ as “task25”,  
‘2.06.1 - Återställning Beställd’ as “task26”,  
‘2.06.2 - Återställning G01-order Begärd’ as “task27”,  
‘2.07.1 - Avsluta Aktivitet & Delprojekt’ as “task28”  
)  
)

---

<div class="post-metadata">

### Author: ![OlaCarlander](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.novacura.com/olacarlander/32/205_2.png) [@OlaCarlander](https://forum.novacura.com/u/OlaCarlander)
#### Post date: [April 18, 2024, 6:08am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/9 "2024-04-18T06:08:07Z")

</div>

Did you consider un-pivoting the input table in the PL SQL window? Or if you are using Odata, maybe just un-pivot it in a SQL-step and then work with the outgoing unpivoted table?

---

<div class="post-metadata">

### Author: ![Alluse](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.novacura.com/alluse/32/1013_2.png) [@Alluse](https://forum.novacura.com/u/Alluse)
#### Post date: [April 18, 2024, 6:36am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/10 "2024-04-18T06:36:47Z")

</div>

Yes, I would also advice the unpivot approach or rework the Portal and keep the data structure intact.

---

<div class="post-metadata">

### Author: ![ivstde](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.novacura.com/ivstde/32/146_2.png) [@ivstde](https://forum.novacura.com/u/ivstde)
#### Post date: [April 18, 2024, 10:43am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/11 "2024-04-18T10:43:06Z")

</div>

Hi,

I had a…questionable 🙂 …idea how you can evaluate data pairs per row of a table by concatenating them to a string and then using flow string functions to hack away at them by data separator.  
(Only 2 IFs…were harmed during the making of this flow 🙂 )

So i played around with it…and here is a test flow.  
[Testa.flow](https://forum.novacura.com/uploads/short-url/1z8dPbtqLu1hmAl59jlTWqm8DKD.flow) (20.2 KB)

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/novacuratalk/original/1X/f71b353963006f266f05347e386456f5939a7ffc.png)

(Not exactly an easier way of doing it…or the best optimized way…but its a way 🙂 …maybe you can also automate the concatenation by including the JSON.encode… )

Hope this helps!

B R  
Ivan

---

<div class="post-metadata">

### Author: ![vikolo01](https://avatars.discourse-cdn.com/v4/letter/v/7C9FD7/32.png) [@vikolo01](https://forum.novacura.com/u/vikolo01)
#### Post date: [April 22, 2024, 7:28am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/12 "2024-04-22T07:28:18Z")

</div>

Hello everyone!

I just wanted to express my gratitude for the solutions provided by @ivstde. That have significantly improved my workflow, making it faster and more user-friendly. A big thank you to Ivan for his contributions!

Best regards,

Viktor

---

<div class="post-metadata">

### Author: ![ivstde](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.novacura.com/ivstde/32/146_2.png) [@ivstde](https://forum.novacura.com/u/ivstde)
#### Post date: [April 26, 2024, 10:33am UTC](https://forum.novacura.com/t/help-to-improve-my-script-step-cut-down-on-if-statements/664/13 "2024-04-26T10:33:55Z")

</div>

Glad i could help!  
(Also i find other peoples issues are a welcome distraction from my own issues and also sometimes turn out to be my future issues…so best to be proactive at helping to solve those 🙂 )
