0

Note: This specific code is being executed on ignition perspective platform, but the problem I am having is more related to logic than platform.

Problem Description:

I'm encountering a problem with updating entries in an array named Asset_Insert within an Ignition Perspective project. When I modify a cell value in a table, the Asset_Insert array should be updated with the new data. However, instead of updating the existing entry, a new entry is being added each time. (The method will be executed every time on each commit/value + enter)

def runAction(self, event):
    col = event.column
    row = event.row
    BASE_ASSET_NAME = self.getSibling("Txt_Selected_BT").props.text
    plantName = self.props.data[0].Plant_Name.value
    assetInsert = self.custom.Asset_Insert
     
    if 'value' in self.props.data[row][col]:
        self.props.data[row][col]['value'] = event.value
  
    
    # Flag to check if we found the row to update
    updated = False

    # Iterate and update existing entry
    for i, item in enumerate(assetInsert):
        if item["ASSET_NAME"] == asset_data["ASSET_NAME"]:
            # Update existing entry
            assetInsert[i] = asset_data
            updated = True
            break
    
    # Append the asset_data if not updated
    if not updated:
        assetInsert.append(asset_data)

Request for Help:

How can I modify the code to ensure that the existing entry in Asset_Insert is updated rather than creating a new one?

Any suggestions or insights on how to resolve this issue would be greatly appreciated.

I have used for loop to iterate through the items in Asset_Insert array, but it only works if there is only 1 row in the table if there are more than 1 rows in the table then it will first create the number of objects as per the number of rows then will keep updating the last object.

3
  • The only way you'll add a new entry is if the loop doesn't find a matching ASSET_NAME. Then it will go to if not updated: and insert a new row. So the problem is that the asset name doesn't match. Commented Aug 29, 2024 at 18:45
  • What is asset_data? Maybe you should be using BASE_ASSET_NAME? Commented Aug 29, 2024 at 18:46
  • Please provide a minimal reproducible example. We need to see the actual values of all the variables you're using. And the code that's unrelated to updating assetInsert can be left out. Commented Aug 29, 2024 at 18:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.