How to Copy Reference List using Action Button

Hi,

I am attempting to use the Action Button to copy a record.

Currently I can copy the following field types successfully,

  • Standard fields using $column_name
  • Reference fields using $column_name.id

However, when attempting to copy a Reference List type field to a new record, i receive a the following errors,

  • “Experiments”: $Experiments, # {‘rowIds’: [1], ‘tableId’: ‘Experiment’} , gives this error
  • “Experiments”: $Experiments.id, # KeyError, gives this error

$Experiments field is a Reference List within the Sample table, linked to Experiment table.
The action button is linked to a field in the Sample table

#action button linked field within Sample table

actions = []

# Adding Samples
actions.append(
  ["AddRecord", "Sample", None, {
    "Experiments": $Experiments,  # {'rowIds': [1], 'tableId': 'Experiment'} , gives this error
       # or if we use *.id
    "Experiments": $Experiments.id,   # KeyError, gives this error
    "Process_ID": $Process_ID.id,  # Reference Field, works as expected
    "Tests": $Tests,   # Standard Field, works as expected 
    "Results": $Results,
    "Repeats": $Repeats,
  }])
  
return {
  "button": "Duplicate {} records".format(len(actions)-1),
  "description": 'Duplicate "{}" records from {}'.format( len(actions), $Process_ID.Process_Name),
  "actions": actions,
}

Any help would be much appreciated.

Thank you for a fantastic product and very proactive engagement!!

1 Like

Solved

Action Button’s AddRecord expects the Reference Field in the format shown here,

"[index_num_1,index_num_2,......]" 

This can be achieved by creating a list of the Reference Field and converting it to a String

str(list($Experiments.id))

with the final code looking like:

#action button linked field within Sample table

actions = []

# Adding Samples
actions.append(
  ["AddRecord", "Sample", None, {
    "Experiments": str(list($Experiments.id)),   # Reference List , works as expected
    "Process_ID": $Process_ID.id,  # Reference Field, works as expected
    "Tests": $Tests,   # Standard Field, works as expected 
    "Results": $Results,
    "Repeats": $Repeats,
  }])
  
return {
  "button": "Duplicate {} records".format(len(actions)-1),
  "description": 'Duplicate "{}" records from {}'.format( len(actions), $Process_ID.Process_Name),
  "actions": actions,
}
2 Likes

Thank you for posting the solution! I was running into this same issue using the autoaction widget, which uses the same syntax for the actions property that is returned.

For reference, here’s the code I have in the Actions column that is connected to the autoaction widget (per the docs, the field type must be Any and a “list of lists” should be returned):

# ---------------------------------------------------------------------
# Choice column: String value returned from lookupOne()
# ---------------------------------------------------------------------
choiceVal = Test_Lookup.lookupOne(LookupValue="Cat").LookupValue

# ---------------------------------------------------------------------
# Choice List column: Tuple, created by looping through records returned from lookupRecords()
# ---------------------------------------------------------------------
# NOTE: The tuple assigned to the Choice list column must have an "L" at the beginning!
choiceMultVals = Test_Lookup.lookupRecords().LookupValue

# Loop through RecordSet returned from lookupRecords() and add to list
choiceMultList = [ ]

for val in choiceMultVals:
  choiceMultList.append(val)

# We only need the first "L" item if the list has 1 or more items
if len(choiceMultList) > 0:
  choiceMultList.insert(0, "L")

# Convert list to tuple. 
# We can't use a tuple directly because you can't modify a tuple after it's created
choiceMultTuple = tuple(choiceMultList)

# This also works if you are assigning values directly
#choiceMultTuple = ( "L", "Dog", "Cat")


# ---------------------------------------------------------------------
# Reference column (single value)
# ---------------------------------------------------------------------
# Use lookupOne() with the id property to get a single Record
# This can be assigned to a Reference column
singleRec = Test_Lookup.lookupOne(LookupValue="Dog").id

# ---------------------------------------------------------------------
# Reference List column (multiple values)
# ---------------------------------------------------------------------
# Use lookupRecords() with the id property, wrapped in str(list()) to get multiple records
# This can be assigned to a Reference List column
multipleRecs = str(list(Test_Lookup.lookupRecords().id))

# Define actions to take on new or changed rows
actions = [
  [ "UpdateRecord", "Testing_Auto_Action_Target", 1, 
    {
      "StringValue": "Hi there!", 
      "SingleRec": singleRec, 
      "Choice": choiceVal,
      "ChoiceMult": choiceMultTuple,
      "MultipleRecs": multipleRecs
    }
  ]
]

return actions

A quick overview of what’s going on in the code above:

  • First I’m getting single or multiple values from the Test_Lookup table, and assigning them to variables
  • These variables are what are set in the actions[] array at the bottom, to assign the values to the fields in the Testing_Auto_Action_Target table
  • The method of dealing with the lookupRecords() RecordSet is particularly unintuitive – as shown, even for a Choice List vs a Reference List column type, the process varies.