Nov 01, 2024 04:19 AM
Hi all,
Having a really rough time trying to update a (User) record through Script.
I am trying to update multiple fields, names and types below:
I have checked my personal reference, and the scripting docs, and I can't for the life of me figure out what's going on...
I have double-checked that the multi-select field options are present before trying to update the record, but my issue highlighted in the issue below:
Example Log of Values:
Example Multi-Select
// uniqueLandlordProperties
(2) [Object, Object]
0: Object
name: "0001271"
1: Object
name: "0001262"
Example Linked Field*
// "uniqueLandlordLeads"
(3) [Object, Object, Object]
0: Object
name: "rec3WbJ2Xb3rTpj2q"
1: Object
name: "rec9VnDniSERZkyIo"
2: Object
name: "recjbDvZZtGh3YCih"
*please note that (stupidly), the record Name/primary field is the record ID.
This is how I'm updating:
// Update
const dataToUpdate = {}
if (uniqueLandlordProperties.length > 0) dataToUpdate["Landlord of Properties"] = uniqueLandlordProperties;
if (uniqueLandlordMarketingConsents.length > 0) dataToUpdate["Marketing Consent"] = uniqueLandlordMarketingConsents;
if (uniqueLandlordSources.length > 0) dataToUpdate["Source"] = uniqueLandlordSources;
if (uniqueLandlordUTMSources.length > 0) dataToUpdate["UTM Source"] = uniqueLandlordUTMSources;
if (uniqueLandlordUTMMediums.length > 0) dataToUpdate["UTM Medium"] = uniqueLandlordUTMMediums;
if (uniqueLandlordUTMCampaigns.length > 0) dataToUpdate["UTM Campaign"] = uniqueLandlordUTMCampaigns;
if (uniqueLandlordLeads.length > 0) dataToUpdate["Leads"] = uniqueLandlordLeads;
await usersTable.updateRecordAsync(landlordId, dataToUpdate);
All fields are failing to update, and I really don't understand why.
What's going on?!?
Full code below (sorry it's messy, I'm trying to debug...
// console.log("Turned Off")
let inputConfig = input.config()
let foundLandlordLeadIds = inputConfig.foundLandlordLeadIds
let landlordId = inputConfig.landlordId
let role = inputConfig.userRole[0]
console.log("foundLandlordLeadIds", foundLandlordLeadIds)
console.log("landlordId", landlordId)
console.log("role", role)
if(role === "Landlord") {
let webhook = `https://hook.eu2.make.com/xfnmooxk91jxshgt9vcqb8bqjjvexutc?landlordId=${landlordId}&foundMarketingLeads=${foundLandlordLeadIds}`
fetch(webhook)
let leads
let usersTable = base.getTable("Users")
let landlord = await usersTable.selectRecordAsync(landlordId, {
fields: [
"Email",
"Landlord of Properties",
"Marketing Consent",
"Source",
"UTM Source",
"UTM Medium",
"UTM Campaign",
"Leads"
]
})
let landlordProperties = landlord.getCellValue("Landlord of Properties") ? landlord.getCellValue("Landlord of Properties") : []
let landlordMarketingConsent = landlord.getCellValue("Marketing Consent") ? landlord.getCellValue("Marketing Consent") : []
let landlordSource = landlord.getCellValue("Source") ? landlord.getCellValue("Source") : []
let landlordUTMSource = landlord.getCellValue("UTM Source") ? landlord.getCellValue("UTM Source") : []
let landlordUTMMedium = landlord.getCellValue("UTM Medium") ? landlord.getCellValue("UTM Medium") : []
let landlordUTMCampaign = landlord.getCellValue("UTM Campaign") ? landlord.getCellValue("UTM Campaign") : []
let landlordLeads = landlord.getCellValue("Leads") ? landlord.getCellValue("Leads") : []
console.log("landlordLeads", landlordLeads)
if(foundLandlordLeadIds && foundLandlordLeadIds.length > 0) {
let foundLeadObjArray = []
for( let lead of foundLandlordLeadIds) {
let obj = {
id: lead,
name: lead,
}
foundLeadObjArray.push(obj)
}
let leadsTable = base.getTable("Leads")
let leadsRecords = await leadsTable.selectRecordsAsync({
fields: [
"Airtable Record ID",
"Source",
"Marketing Consent",
"All Properties",
"UTM Source",
"UTM Medium",
"UTM Campaign"
],
recordIds: foundLandlordLeadIds
})
let leads = leadsRecords.records
console.log("leads", leads)
let landlordLeadProperties = []
let landlordLeadMarketingConsents = []
let landlordLeadSources = []
let landlordLeadUTMSources = []
let landlordLeadUTMMediums = []
let landlordLeadUTMCampaigns = []
for(let lead of leads) {
let leadsProperties = lead.getCellValue("All Properties")
let leadsMarketingConsents = lead.getCellValue("Marketing Consent")
let leadsMarketingSources = lead.getCellValue("Source")
let leadsUTMSources = lead.getCellValue("UTM Source")
let leadsUTMMediums = lead.getCellValue("UTM Medium")
let leadsUTMCampaigns = lead.getCellValue("UTM Campaign")
leadsProperties && leadsProperties.length > 0 ? leadsProperties.forEach(val => landlordLeadProperties.push(val)) : null;
leadsMarketingConsents && leadsMarketingConsents.length > 0 ? leadsMarketingConsents.forEach(val => landlordLeadMarketingConsents.push(val)) : null;
leadsMarketingSources && leadsMarketingSources.length > 0 ? leadsMarketingSources.forEach(val => landlordLeadSources.push(val)) : null;
leadsUTMSources && leadsUTMSources.length > 0 ? leadsUTMSources.forEach(val => landlordLeadUTMSources.push(val)) : null;
leadsUTMMediums && leadsUTMMediums.length > 0 ? leadsUTMMediums.forEach(val => landlordLeadUTMMediums.push(val)) : null;
leadsUTMCampaigns && leadsUTMCampaigns.length > 0 ? leadsUTMCampaigns.forEach(val => landlordLeadUTMCampaigns.push(val)) : null;
}
console.log("landlordLeadProperties", landlordLeadProperties)
console.log("landlordLeadMarketingConsents", landlordLeadMarketingConsents)
console.log("landlordLeadSources", landlordLeadSources)
console.log("landlordLeadUTMSources", landlordLeadUTMSources)
console.log("landlordLeadUTMMediums", landlordLeadUTMMediums)
console.log("landlordLeadUTMCampaigns", landlordLeadUTMCampaigns)
console.log("landlordProperties", landlordProperties)
console.log("landlordMarketingConsent", landlordMarketingConsent)
console.log("landlordSource", landlordSource)
console.log("landlordUTMSource", landlordUTMSource)
console.log("landlordUTMMedium", landlordUTMMedium)
console.log("landlordUTMCampaign", landlordUTMCampaign)
let uniqueLandlordProperties = deduplicateAndStrip([landlordLeadProperties, landlordProperties])
let uniqueLandlordMarketingConsents = deduplicateAndStrip([landlordLeadMarketingConsents, landlordMarketingConsent])
let uniqueLandlordSources = deduplicateAndStrip([landlordLeadSources, landlordSource])
let uniqueLandlordUTMSources = deduplicateAndStrip([landlordLeadUTMSources, landlordUTMSource])
let uniqueLandlordUTMMediums = deduplicateAndStrip([landlordLeadUTMMediums, landlordUTMMedium])
let uniqueLandlordUTMCampaigns = deduplicateAndStrip([landlordLeadUTMCampaigns, landlordUTMCampaign])
let uniqueLandlordLeads = deduplicateAndStrip([foundLeadObjArray, landlordLeads])
console.log("uniqueLandlordProperties", uniqueLandlordProperties)
console.log("uniqueLandlordMarketingConsents", uniqueLandlordMarketingConsents)
console.log("uniqueLandlordSources", uniqueLandlordSources)
console.log("uniqueLandlordUTMSources", uniqueLandlordUTMSources)
console.log("uniqueLandlordUTMMediums", uniqueLandlordUTMMediums)
console.log("uniqueLandlordUTMCampaigns", uniqueLandlordUTMCampaigns)
console.log("uniqueLandlordLeads", uniqueLandlordLeads)
let fields = []
let propertiesField = usersTable.getField("Landlord of Properties")
let marketingConsentField = usersTable.getField("Landlord of Properties")
let sourceField = usersTable.getField("Source")
let utmSourceField = usersTable.getField("UTM Source")
let utmMediumField = usersTable.getField("UTM Medium")
let utmCampaignField = usersTable.getField("UTM Campaign")
let leadsField = usersTable.getField("UTM Campaign")
console.log("propertiesField", propertiesField)
console.log("marketingConsentField", marketingConsentField)
console.log("sourceField", sourceField)
console.log("utmSourceField", utmSourceField)
console.log("utmMediumField", utmMediumField)
console.log("utmCampaignField", utmCampaignField)
console.log("leadsField", leadsField)
// await usersTable.updateRecordAsync(landlordId, {
// "Landlord of Properties": uniqueLandlordProperties,
// "Marketing Consent": uniqueLandlordMarketingConsents,
// "Source": uniqueLandlordSources,
// "UTM Source": uniqueLandlordUTMSources,
// "UTM Medium": uniqueLandlordUTMMediums,
// "UTM Campaign": uniqueLandlordUTMCampaigns,
// "Leads": uniqueLandlordLeads
// })
const dataToUpdate = {}
if (uniqueLandlordProperties.length > 0) dataToUpdate["Landlord of Properties"] = uniqueLandlordProperties;
if (uniqueLandlordMarketingConsents.length > 0) dataToUpdate["Marketing Consent"] = uniqueLandlordMarketingConsents;
if (uniqueLandlordSources.length > 0) dataToUpdate["Source"] = uniqueLandlordSources;
if (uniqueLandlordUTMSources.length > 0) dataToUpdate["UTM Source"] = uniqueLandlordUTMSources;
if (uniqueLandlordUTMMediums.length > 0) dataToUpdate["UTM Medium"] = uniqueLandlordUTMMediums;
if (uniqueLandlordUTMCampaigns.length > 0) dataToUpdate["UTM Campaign"] = uniqueLandlordUTMCampaigns;
if (uniqueLandlordLeads.length > 0) dataToUpdate["Leads"] = uniqueLandlordLeads;
await usersTable.updateRecordAsync(landlordId, dataToUpdate);
}
}
// // Return Array of Full Objects
// // function deduplicateAndStrip(arrays) {
// // const combinedArray = arrays.flat();
// // const deduplicatedMap = combinedArray.reduce((map, obj) => {
// // if (!map.has(obj.name)) {
// // map.set(obj.name, obj);
// // }
// // return map;
// // }, new Map());
// // return Array.from(deduplicatedMap.values());
// // }
// Return Array of Objects with Name Value
function deduplicateAndStrip(arrays) {
const combinedArray = arrays.flat();
const deduplicatedMap = combinedArray.reduce((map, obj) => {
if (!map.has(obj.name)) {
map.set(obj.name, { name: obj.name }); // Store only the name property
}
return map;
}, new Map());
return Array.from(deduplicatedMap.values());
}
// // Return Array of Strings
// // function deduplicateAndStrip(arrays) {
// // const combinedArray = arrays.flat();
// // const deduplicatedMap = combinedArray.reduce((map, obj) => {
// // if (!map.has(obj.name)) {
// // map.set(obj.name, obj.name);
// // }
// // return map;
// // }, new Map());
// // return Array.from(deduplicatedMap.values());
// // }