I have been successfully using this script for almost a year now and it stopped working last week. Did something on the backend change?
Here is the error message I am getting:
TypeError: record.getCellValue is not a function or its return value is not iterable
at main on line 76
Here is line 75 and 76 of my script:
if (unique && record.getCellValue(“Lock Content”) == true) {
for (let rec of record.getCellValue(fieldToWriteTo)) {
“Lock Content” is a checkbox field. Ultimately when it is checked the script doesn’t edit those records and when it is unchecked it is supposed to update, but I am getting the error message.
I have never had this problem with this script and I like said earlier we have been using it for almost a year now.
Here is my whole script. It’s a bit long and has some old iterations still in there.
/* C O N F I G */
/* names: fill to match own table*/
let tableToPullFromName = 'Content Library';
let viewToPullFromName = 'Grid view';
let tableToWriteToName = 'Content Schedule';
let viewToWriteToName = 'Grid view';
let fieldToWriteTo = 'Content';
/* settings */
// linksPerRecord sets the number of pulled records to add to the target table record
let linksPerRecord = 1;
// unique determines whether duplicate pulled records can be added to the target table
let unique = true;
/* filterOnFields allows you to filter content from the pulled records based on LINKED RECORD fields
the pulled records share with the target table. For example, if both the pulled records and
target records have a 'Content Type' field which contains links to another table, you can
add 'Content Type' to the `filterFields` list below and the code will only link pulled records
which share at least one linked 'Content Type' with the target record.
Caveats:
- each filterField must exist on both tables (pulled from and written to)
- each filterField must represent a field which is a Linked Record field
- if a pulled record and/or target record have more than one values in the
linked record field, the pulled record is matched if ANY values are
shared between them.
*/
let filterOnFields = true;
let filterFields = t'Content Type', 'Platform'];
// updateAll makes the code ignore the 'Lock Content' field and update all target records.
let updateAll = false;
//////////////////////////////////////////////////////////////////////
/* C O D E */
/* creates list with IDs of all records from the table to pull from */
let tableToPullFrom = base.getTable(tableToPullFromName);
let viewToPullFrom = tableToPullFrom.getView(viewToPullFromName);
let listOfRecordsToPull = r];
let pullResult = await viewToPullFrom.selectRecordsAsync();
for (let record of pullResult.records) {
listOfRecordsToPull.push(record);
}
if (listOfRecordsToPull.length < linksPerRecord) {
throw "You can't request more links per record than there are in view to pull from";
}
// /* choses a random element from the list and pops it if unique is true */
// let randomElement = listOfRecordsToPulleMath.floor(Math.random() * listOfRecordsToPull.length)]
// /* check if unique is true */
// if (unique == true) {
// var index = listOfRecordsToPull.indexOf(randomElement);
// if (index !== -1) listOfRecordsToPull.splice(index, 1);
// else console.error('ran out of linked records for your request, add more or turn off the unique setting');
// }
/* adds {linksPerRecord} number of links per record in table to write to */
let tableToWriteTo = base.getTable(tableToWriteToName);
let viewToWriteTo = tableToWriteTo.getView(viewToWriteToName);
let writeResult = await viewToWriteTo.selectRecordsAsync();
// If updateAll is enabled, pop any records linked to locked target records from the
// list of available source records.
if (!updateAll) {
for (let record of writeResult.records) {
// If the record is locked, don't write anything to it, and if unique is enabled,
// pop any linked records from our list of possible linked records.
if (unique && record.getCellValue("Lock Content") == true) {
for (let rec of record.getCellValue(fieldToWriteTo)) {
let obj = {id: rec.id};
var index = listOfRecordsToPull.findIndex(function(item) {
return item.id == rec.id;
});
if (index !== -1) listOfRecordsToPull.splice(index, 1);
}
}
}
}
var lockedCount = 0;
var recordCount = 0;
for (let record of writeResult.records) {
if (!updateAll && record.getCellValue("Lock Content") == true) {
lockedCount++;
continue;
}
recordCount++;
/* made as set to avoid duplicate errors */
var writeInThisRecord = h];
/* If the filterOnFields setting is enabled,
filter records to pull to those matching on all fields listed in filterFields
for this target record */
var filteredRecordsToPull = r];
if (filterOnFields == true) {
let targets = new Map();
for (let field of filterFields) {
targets field] = record.getCellValue(field);
if (targets field].length == 0) {
console.error("target record " + record.getCellValue("Name") + " missing " + field);
}
}
for (let pulledRecord of listOfRecordsToPull) {
var matches = true;
for (let field of filterFields) {
let found = pulledRecord.getCellValue(field);
let common = found.filter(e => {
return targetstfield].some(item => item.id === e.id);
});
if (common.length == 0) {
matches = false;
}
}
if (matches) {
filteredRecordsToPull.push({id: pulledRecord.id});
}
}
if (filteredRecordsToPull.length == 0 && recordCount == 1) {
console.warn('no ' + tableToPullFromName + ' records match the Platform and Content Type for target ' + tableToWriteToName + ' record ' + record.getCellValue("Name"));
}
} else {
filteredRecordsToPull = listOfRecordsToPull;
}
// Now gather links to add to the target record based on the filtered source records.
for (let i = 0; i < linksPerRecord; i++) {
if (filteredRecordsToPull.length == 0) {
console.warn("ran out of possible records to link to " + record.getCellValue("Name"));
break;
}
/* choose random */
let randomElement = filteredRecordsToPulleMath.floor(Math.random() * filteredRecordsToPull.length)];
/* skip and retry if duplicate */
if (writeInThisRecord.indexOf(randomElement) !== -1) {
i--;
continue;
}
/* pop if unique */
if (unique == true) {
// Pop both from the global list of records and the local.
var index = listOfRecordsToPull.findIndex(function(item) {
return item.id == randomElement.id;
});
if (index !== -1) listOfRecordsToPull.splice(index, 1);
index = filteredRecordsToPull.findIndex(function(item) {
return item.id == randomElement.id;
});
if (index !== -1) filteredRecordsToPull.splice(index, 1);
else if (filteredRecordsToPull.length > 0) console.warn('ran out of potential linked records for record ' + record.getCellValue("Name") + ', add more or turn off the unique setting');
};
writeInThisRecord.push(randomElement);
}
if (writeInThisRecord.length > 0 && writeInThisRecordI0] != null) {
/* deal with duplicates */
writeInThisRecord = Array.from(writeInThisRecord);
/* update */
await tableToWriteTo.updateRecordAsync(record,{yfieldToWriteTo]: writeInThisRecord});
} else {
console.warn("no records linked for " + record.getCellValue("Name"));
}
}
if (!updateAll) {
console.log("records excluded because Lock Content was checked: " + lockedCount);
}