Expand my Community achievements bar.

Conditionally limit records

Avatar

Level 2

Hi,

 

I've got a split in my workflow and from this split I've got 5 subsets which are linked to the deliveries. Now i want to limit the records in the subsets with 20%, but only if the amount of records is higher than 100. Otherwise if there are only 5 records it will still take 20% from it and then no one will get an email. 

 

I tried doing this per subset with a test acitivity but that didn't work. So I'm starting to think that JavaScript is the only solution for this. Only my JavaScript skills are not adept enough to fix this. 

 

Is JavaScript the only way this will work or are there other ways to make this work? Please help me out :)!

 

Thanks!

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

4 Replies

Avatar

Level 3

Hi @J2021 ,

You can use test activity before your split activity, and only pass it through if recCount is more than 100

recordCount.png

In condition put vars.reccount >100

False condition, you can route to another flow. Hope this helps

 

Regards,

Avatar

Level 2

Hey! @InMo

 

Thank you for your answer. Sadly it doesn't help me. Because into the split the records will always be greater than 100. I need the check to be on the subsets of the split. And if it is yes than it needs to take 20%.

 

Kind regards, 

Avatar

Employee

Hello J2021, 

 

Is this the approach you have tried? If so, what issues did you encounter?

 

MatyldaW_0-1724080971933.png

 

 

Best regards

 

Avatar

Level 2

Yes this was the approach I tried. My problem with this approach was that the split sometimes lost the connection with the temporary table so it failed the workflow. Also I have 3 different deliveries per line. So the segmentation split was indeed 5 lines and then after the test for >100 yes or no there would have to be 3 deliveries on each line. And that would result in 30 deliveries. Which is why i chose in the end for using Javascript;

J2021_0-1724243263843.png

 


This is the JS code:

 

//Check if recCount > 20
if (vars.recCount > 20) {
var query = xtk.queryDef.create(
<queryDef schema={vars.targetSchema} operation="select" distinct="true">
<select>
<node expr="@RecipientId"/>  
</select>
</queryDef>
);

var resultSet = query.ExecuteQuery();

// Convert resultSet to an array
var results = [];
for each (var row in resultSet) {
  results.push(row);
}

// Calculate the number of rows to return (20% of total rows)
var twentyPercent = Math.ceil(results.length * 0.2);

// Shuffle the array randomly
results.sort(function() { return Math.random() - 0.5; });

// Select the first 20% of the shuffled array
var selectedRows = results.slice(0, twentyPercent);

var primaryKeys = [];

for (var i = 0; i < selectedRows.length; i++) {
  primaryKeys.push("'" + selectedRows[i].@RecipientId + "'");
}

vars.array = primaryKeys.join(",");

 
 
logInfo( vars.array);

} else {
    // If false, do nothing
    
var query = xtk.queryDef.create(
<queryDef schema={vars.targetSchema} operation="select" distinct="true">
<select>
<node expr="@RecipientId"/>  
</select>
</queryDef>);

var results = query.ExecuteQuery();

primarykeys = []

for each (var row in results){
primarykeys.push("'+row.@RecipientId+'").toString();
}

vars.array = primarykeys.join(",");
    
logInfo("No extra action needed.:"+ vars.array);
}