Bulk update in the list

0
Hello,   I have a list with many items. Is it possible to update an attribute value of objects in the list without looping through each item? The value I have to update is the same; as an example, set 'customer id=12' in a list of 'merchant' objects.   Thank you in advance! Iqbal
asked
3 answers
0

Hi Muhammed Iqbal M P,

I recently faced a performance issue when trying to update a large list of records in Mendix and wanted to share a better approach that worked for me:

updating many objects usually means looping through them in a microflow — which is inefficient for large datasets.

In SQL, you'd simply do:

UPDATE Merchant SET CustomerID = 12 WHERE MerchantCategory = 'TestCategory';

 

or use a Java action to perform a direct SQL update:

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import com.mendix.systemwideinterfaces.core.IContext;

import com.mendix.core.Core;

public class BulkUpdateCustomerId {   

   public static void execute(IContext context) throws Exception {       

        Connection conn = Core.getConnection(context);       

          try (PreparedStatement stmt = conn.prepareStatement(           

                "UPDATE mymodule$merchant SET customerid = 12 WHERE merchantcategory = 'TestCategory'"        ))

                   {           

                     stmt.executeUpdate();       

                    }    }}

 

This avoids loops and updates all matching records efficiently in one go.

 

Regards

Reemali Patil

 

answered
1

Hi!

In Mendix the default way is to update multiple records by using loops. In most cases that is sufficient.

In case of performance issues, your first go-to would be batching or queueing. If that does not help, you could maybe go the java-way Reemali pointed out. The downside of that approach is that you loose the consistency guarantee of Mendix Studio Pro: one rename of a module, entity or an attribute and your code will crash without prior notice. In my experience that is not worth the risk in most cases.

There even is an article somewhere of someone suggesting using javascript to update larger sets of data client side. Under very specific circumstances that may also be a solution.

I hope this helps,

Michiel

answered
0

Hi mohammed,

                         

  • Retrieve the list of Merchant objects (via a database retrieve or passed from a previous action).

  • Add a Loop activity to iterate over the list.

  • Inside the loop:

    • Use Change Object activity on the loop variable (Merchant).

    • Set customerid=12.

  • After the loop, use Commit List to commit the updated list back to the database.

answered