Link field to update another field's value

I have a table that contains a link field that when pressed, directs user to outside website. What I want to accomplish is when this link is pressed it also updates a field’s value (field_407) to “YES”. Field 407 is located on the same table (object_8) as the link field.

I know this is fairly easy to do with action links using Knack’s built in “update record” option, and I’ll looking to do the exact same thing but with a link field.

Any and all help would be appreciated…

Here is a script that OpenGPT has suggested that partially works:

$(document).on(‘knack-view-render.view_235’, function(event, view) {

$(document).on('click', '.SFRButton', function(e) {
  
    var $tableRow = $(this).closest('tr');
    
    // Extract value of 'field_49' from the current row. 
    var fieldValue49 = $tableRow.find('.field_49').text().trim();

    console.log("Captured field 49 value:", fieldValue49);

    var objectID = 'object_8';

    var headers = {
        "X-Knack-Application-Id": "XXXXXXXXXXXXX", // Replace with your App ID
        "X-Knack-REST-API-Key": "XXXXXXXXXXXXXX"   // Replace with your API Key
    };


    // First, search for the record in 'object_8' that matches 'field_49' value
    $.ajax({
        url: 'https://api.knack.com/v1/objects/' + objectID + '/records',
        method: 'GET',
        headers: headers,
        data: {
            'filters': [{
                'field':'field_49',
                'operator':'is',
                'value':fieldValue49
            }]
        },
        success: function(response) {
            console.log("API response:", response);
            if (response.records && response.records.length > 0) {
                var recordID = response.records[0].id;
		


                // Now, update 'field_407' of the found record
                $.ajax({
                    url: 'https://api.knack.com/v1/objects/' + objectID + '/records/' + recordID,
                    method: 'PUT',
                    headers: headers,
                    data: {
                        'field_407': "YES"
                    },
                    success: function(updateResponse) {
                        console.log("Field updated successfully!");
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        console.error("Error updating field:", textStatus, errorThrown);
                    }
                });
            } else {
                console.error("No matching record found for field_49 value:", fieldValue49);
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.error("Error searching for record:", textStatus, errorThrown);
        }
    });
});

});

As you can see, it’s capturing field 49 and trying to match in the table with field 49 data captured and when is found, updates field 407 to “YES”. According to the console log, it’s capturing field 49, but not updating field 407 to “YES”. Just a fyi, field 49, the link field (SFRButton) and field 407 are in the same table and row.

Any help with this is appreciated…

Hey @Jon1. I assume you aren’t using the Action trigger because the records don’t all have the same URL?
Your ChatGPT code contains an object-based request which will expose your API key and may also have an incorrect format for the data parameter.

Here is some more optimised code below:

Requirements:

  • A URL field (field_64)
  • An ‘is read’ short text field (field_65)
  • A structure that looks similar to the below, with a login-protected page containing a grid view (view_xx), and a nested page (scene_yy) that has an edit form (view_zz).
    (Note that the edit form doesn’t necessarily have to be nested on this grid, so long that it’s present elsewhere in the app with the same login permissions enabled)
    image
$(document).on('knack-view-render.view_xx', function(event, view, record) {
    
    const urlField = "field_64";
    const editFormScene = "scene_yy";
    const editFormView = "view_zz";
    const myData = {field_65: "Yes"};

    /* Listen for the URL field being clicked on the grid view, get the record ID, and update the 'is clicked' field */
    $(`#${view.key} .${urlField} a`).click(function() {
      
      const recordID = $(this).closest('tr').attr('id');

      $.ajax({
        url: `https://api.knack.com/v1/pages/${editFormScene}/views/${editFormView}/records/${recordID}`,
        type: 'PUT',
        headers: {
          'X-Knack-Application-Id': Knack.application_id,
          'Authorization': Knack.getUserToken(),
          'Content-Type': 'application/json'
        },
        data: JSON.stringify(myData),
        success: function(response) {
          // Handle a successful response here
        }
      });

    });
});

Here’s a breakdown of the code:

$(`#${view.key} .${urlField} a`).click(function() {
Listens for the link being clicked in the grid view.

const recordID = $(this).closest('tr').attr('id');
Gets the record ID of that same table row.

$.ajax({...
Edits that record via a view-based PUT, with the credentials of the logged in user, rather than referencing the API key (more secure).

1 Like

Hey Stephen, I appreciate the working solution… JON