I think this is going to be obvious to someone, but I have been struggling with this for a bit. I have a classic app. in this i have a table of sites. Each site has 3 levels of manager associated. Previously i used this to control the access to the that site. However as i need to be able to give access to the site to other people i added an additional field for who can access. This works well, however the issue i have is that when i update the manager i would like to update the access by automatically (even if it just adds the new manager and leaves the old one). As the first 3 fields are linked to the specific level of user role and the access i by is linked to the accounts I cant find a way to do this automatically.
Has anyone found a way to do this? I feel like it may be where flows come in, but i haven’t spent much time with flows (have 0 in the app currently).
Hi @Chris8, do you mean something like the below, where the Access field should merge the Manager L1-3 fields?:
Manager L1
Manager L2
Manager L3
Access
Alex Chen
Brooke Smith
George King
Alex Chen, Brooke Smith, George King
Your Knack Flow/Make/Zapier scenario might look something like this:
Listen for ‘edit site’ form being submitted
Merge ID values from Manager L1, Manager L2, Manager L3 into an array: ["123abc","456def","789ghi"]
Update Access field record with new array value
Alternatively, you could use JavaScript and the Knack API Helper library (see guide here) to do something similar on a form-submit handler:
$(document).on('knack-form-submit.any', function(event, view, record) {
if (view.key === 'view_aa') { // Edit site form
const accessAccounts = [
record?.field_aa?.[0]?.id, // Manager L1
record?.field_bb?.[0]?.id, // Manager L2
record?.field_cc_raw?.[0]?.id // Manager L3
].filter(Boolean)
const body = { field_dd: accessAccounts }; // Access
KnackAPI.makeRequest('put', {
scene: 'scene_bb', // A hidden page with same role permissions as Edit site
view: 'view_bb', // An edit site form view within this hidden page (should contain field_dd)
recordId: record.id,
body
});
}
});
Thanks for this. When I try to do it, it seems to give me issues as each of the 3 fields is connected to a user role (l1, l2, l3), where as the ‘access by’ is connected to the ‘accounts’ table. Additionally the ‘access by’ field may contain other users, so I cant simply overwrite the data, i need to try and include the current data and simply add the new data. I tried to do it with JavaScript but kept hitting issues. I gave up after a bit, as it occurred that it may be better to try flows, but maybe i need to have another look at using javascript. This is where I got to:
$(document).on('knack-record-update.view_234', function(event, view, record) {
const getIds = (fieldData) => {
if (!fieldData) return [];
return Array.isArray(fieldData) ? fieldData.map(item => item.id) : [fieldData.id];
};
// Gather IDs from the record just updated
const existingIds = getIds(record.field_732);
const idsFrom439 = getIds(record.field_439);
const idsFrom165 = getIds(record.field_165);
const idsFrom164 = getIds(record.field_164);
// Merge and Remove Duplicates
const combined = [...existingIds, ...idsFrom439, ...idsFrom165, ...idsFrom164];
const uniqueIds = [...new Set(combined)].filter(id => id);
// Prepare the data for the PUT request
const updateData = {
field_732: uniqueIds
};
// Execute the call
$.ajax({
url: `https://api.knack.com/v1/objects/object_1/records/${record.id}`,
type: 'PUT',
headers: {
'Authorization': Knack.getUserToken(),
'X-Knack-Application-Id': Knack.application_id,
'Content-Type': 'application/json'
},
data: JSON.stringify(updateData),
success: function(response) {
console.log('Master field updated successfully:', response);
},
error: function(error) {
console.error('Error updating master field:', error);
}
});
});
However I think I need to amend to using a form to update as opposed to updating the object as I think there is a permission error when getting the user token.
Your code makes sense given there may be other users in that field, so no problems there
You cannot use a user token for an object-based request, so you are best to use view-based instead, as you mentioned
Pointing to different user role records rather than all to the accounts table is going to be problematic, it’s not too late to restructure that if you can - Callum Boase has an extensive video on this here: Why I never add fields to child user roles in Knack
With the above point, you are going to run into similar trouble if you go down the Flows route