Help

Re: Linked Records and updaterecordasync

Solved
Jump to Solution
1490 0
cancel
Showing results for 
Search instead for 
Did you mean: 
jacob_angelo
5 - Automation Enthusiast
5 - Automation Enthusiast

Ive looked at numerous other community posts but don’t seem to be understanding this. I want to update a linked field with a room location. The location is being provided by users selecting it from a list via recordasync, and I’m storing it as a string with .name.

This unlinked field updates without issue:

sourceTable.updateRecordAsync(recordID,{'nonlinked location': roomName});

but i can’t get the linked table to update no matter how I format the portion after the :. I’ve tried

sourceTable.updateRecordAsync(recordID,{'linked location': [{id: roomName}]})
and other variations of that to no avail. I believe it has to do with the location being passed as a string to the linked array, but I’m not sure how to format either the input or the updaterecord to accept the change.

The script is meant to accept a room choice, then the user enters an Item Number and the script updates the location of that item with the room chosen in step 1.



let location = base.getTable("Rooms");

let sourceTable     = base.getTable("Inventory");

let sourceTableName = sourceTable.name;

let sourceField = sourceTable.getField('ItemNumber');

let sourceFieldName = sourceField.name;

let locationField = sourceTable.getField("Location");

let locationFieldName = locationField.name;


// Prompt the user to pick a Room

let currentroom = await input.recordAsync('Select a Room to Scan', location);

let roomName = currentroom.name;


// get the assett tag

output.markdown("### Enter the Asset tag:");

let thisQueryValue = await input.textAsync('What is the asset tag?');

// get the records

let result = await sourceTable.selectRecordsAsync();

let sourceRecords = result.records;

let recordID;

// iterate across all the records


for (var r = 0; r < sourceRecords.length; r++)

{

    if(thisQueryValue == sourceRecords[r].getCellValue(sourceFieldName))

    {

        let currentRecord = sourceRecords[r];

        let recordID = sourceRecords[r].id;

        sourceTable.updateRecordAsync(recordID,{'nonlinked Location: roomName});

        sourceTable.updateRecordAsync(recordID,{'Location':  roomName});


    }

    

}
1 Solution

Accepted Solutions
Kamille_Parks
16 - Uranus
16 - Uranus

The proper format to update a linked record field would be:

table.updateRecordAsync(recordID, {'name of field': [{id: 'id of the record to be linked'}]})

Link to another record-type fields must receive a value in the form of an array of objects where each object has a single key-value pair for the ID for the record. You can’t pass it the name of a record, and you can’t pass an object. It has to be an array of objects, even if the array only has one object in it.

See Solution in Thread

2 Replies 2
Kamille_Parks
16 - Uranus
16 - Uranus

The proper format to update a linked record field would be:

table.updateRecordAsync(recordID, {'name of field': [{id: 'id of the record to be linked'}]})

Link to another record-type fields must receive a value in the form of an array of objects where each object has a single key-value pair for the ID for the record. You can’t pass it the name of a record, and you can’t pass an object. It has to be an array of objects, even if the array only has one object in it.

Wonderful, I tried it and it worked, thank you very much! For future people who search for this issue, here is some further clarification

currentrecord = record you want to update
currenttable = table the currentrecord resides in
linkedfield = name of the field in the linked table you want to use as the updated information
linkedrecord = ID of the record that you want to use as updated information

currenttable.updateRecordAsync(currentrecordID#, {'linkedfield': [{id: linkedrecord}]})

Here is my code for updating the room location for a piece of equipment:

// Declare Tables
let roomTable = base.getTable("Rooms");
let inventoryTable = base.getTable("Inventory");

//Declare fields for lookup of inventory ID #
let inventoryField = inventoryTable.getField('GCCCD');
let inventoryFieldName = inventoryField.name;

// get the records for all inventory items to search user input against
let result = await inventoryTable.selectRecordsAsync();
let inventoryRecords = result.records;

// Prompt the user to pick a location where inventory is located
let currentRoom = await input.recordAsync('Select a Room to Scan', roomTable);
let roomName = currentRoom.name;
let roomID = currentRoom.id;

// prompt user to input an inventory ID number
let currentInventory = await input.textAsync('What is the Barcode for current equipment item?');

// iterate across all the records based on total # records in Inventory database
for (var r = 0; r < inventoryRecords.length; r++)
{
    //If user input  = inventory ID then update location with currentRoom
    if(currentInventory == inventoryRecords[r].getCellValue(inventoryFieldName))
    {
        //save ID number of current record that matches user input
        let inventoryRecordID = inventoryRecords[r].id;

        //Update Location of current inventory item to be current room location
        inventoryTable.updateRecordAsync(inventoryRecordID, {'Location': [{id: roomID}]})
      
        output.markdown("Location for Current Inventory Record **" + currentInventory + "** has been updated to Room: **" + roomName + "**");
    }
}