Mar 25, 2024 08:21 AM
I am new to scripting in Airtable and am receiving "Received malformed JSON input." every time I try to validate my JSON. Below is my script, and the error message Airtable keeps returning- can someone help me determine what is wrong with my formatting? For context, I am trying to use a web hook to connect Salesforce and Airtable, where Salesforce creates a record in my Airtable base.
// @TS-nocheck
var Base64 = {
// private property
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for decoding
decode: function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 decoding
_utf8_decode: function (utftext) {
var string = "";
var i = 0;
var c = 0;
var c2 = 0;
var c3 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
let sfFields = JSON.parse(Base64.decode(input.config().update));
console.log(sfFields);
const NUM_OF_PASSIVE_PARTICIPANTS = "fldIo8ZOWFEvX7joL"
const ACCOUNT_ID = 'fldNP4o5Fm5BAcOu1';
const AGENDA_PROPOSAL_LINK = 'fldNsfagu9jlBHXnN';
const ASSESSMENTS = 'fld2cgwKP06grankN';
const AUDIENCE_LEVEL = 'fldikAOITHFyDNxHL';
const CLIENT_RELATIONS_DIRECTORS = 'fldQdxidRivMyws8k';
const fldConnection = {
[NUM_OF_PASSIVE_PARTICIPANTS]: 'Num_Passive_Participants_Observers__c', //# of Passive Participants/Observers
[ACCOUNT_ID]: 'AccountId', //AccountId
[AGENDA_PROPOSAL_LINK]: 'Agenda_Proposal_Link__c', //Agenda/Proposal Link
[ASSESSMENTS]: 'Assessments__c', //Assessments
[AUDIENCE_LEVEL]: 'Audience_Level__c', //Audience Level
[CLIENT_RELATIONS_DIRECTORS]: 'Client_Relations_Directors__c', //Client Relations Directors
var fldType = {
[NUM_OF_PASSIVE_PARTICIPANTS]: 'number',
[ACCOUNT_ID]: 'text',
[AGENDA_PROPOSAL_LINK]: 'text',
[ASSESSMENTS]: 'multiSel',
[AUDIENCE_LEVEL]: 'multiSel',
[CLIENT_RELATIONS_DIRECTORS]: 'multiSel',
};
let progRecFields = {};
for (const fld in fldConnection) {
let val = sfFields[fldConnection[fld]]
if (val !== undefined) {
switch(fldType[fld]){
case "singleSel":
progRecFields[fld] = formatSingleSel(val);
break;
case "multiSel":
progRecFields[fld] = formatMultiSel(val);
break;
case "date":
progRecFields[fld] = formatDate(val);
break;
case "number":
progRecFields[fld] = formatNumber(val);
break;
case "percent":
progRecFields[fld] = formatPercent(val);
break;
case "text":
progRecFields[fld] = val;
break;
default:
error("Unknown Field Type");
}
}
}
let tblPrograms = base.getTable("tbl6bgXM1l9yfZgtJ"); //Programs
let progRecExists = false;
let curRecId = sfFields[fldConnection[SALESFORCE_ID]];
let atProgram = await tblPrograms.selectRecordsAsync({ fields: [SALESFORCE_ID] });
let atRecId;
atProgram.records.forEach(rec => {
if (rec.getCellValueAsString(SALESFORCE_ID) === curRecId) {
progRecExists = true;
atRecId = rec.id;
return;
}
})
if (!progRecExists) {
createProgram();
} else {
updateProgram();
}
async function createProgram() {
await tblPrograms.createRecordAsync(progRecFields);
}
async function updateProgram() {
await tblPrograms.updateRecordAsync(atRecId, progRecFields);
}
function formatSingleSel(val) {
return { name: val };
}
function formatMultiSel(val) {
let valArr = val.split(";");
valArr.forEach( (ele, index) => {
valArr[index] = { name: ele };
})
return valArr;
}
function formatDate(val){
return new Date(val);
}
function formatNumber(val){
return parseInt(val);
}
function formatPercent(val){
return parseInt(val)/100;
}