I have a DB that that has a embedded object called Login Information that contains 11 fields and 1 field outside the embedded object called Account.
its all working well and emails the details of the embedded object however if 2 or more embedded are present under the account name it fails with this error.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lgwang.Class java.lang.Object.getClass()' on a null object reference
I have tried to get ChatGPT fix it but it always get it wrong. Does anyone know how to fix it please.
This is my script below that works with a single embedded object
const line = nn => "\n" + "-".repeat(nn) + "\n";
const line1 = nn => "\n" + "=".repeat(nn) + "\n";
// Login information constructor
function LoginDevice(ob) {
this["Name"] = ob["Name"];
this["Login Username"] = ob["Login Username"];
this["Login Password"] = ob["Login Password"];
this["Login Pin"] = ob["Login Pin"];
this["Login Passkey"] = ob["Login Passkey"];
this["Date (re) Set"] = ob["Date (re) Set"];
this["Associated Email"] = ob["Associated Email"];
this["Category"] = ob["Category"];
this["Website"] = ob["Website"];
this["Phone Number"] = ob["Phone Number"];
this["Additional Info"] = ob["Additional Info"];
}
// Format login information into text
const loginToText = items => {
if (!items || !items.length) return "";
let result = [];
let fieldNames = [
"Name",
"Login Username",
"Login Password",
"Login Pin",
"Login Passkey",
"Date (re) Set",
"Associated Email",
"Category",
"Website",
"Phone Number",
"Additional Info"
];
let logins = items.map(a => new LoginDevice(a));
for (let item of logins) {
let entry = [];
for (let name of fieldNames) {
let value = item[name];
if (value !== undefined && value !== null && value !== "") {
// Format date using moment if valid
if (name === "Date (re) Set" && moment(value).isValid()) {
value = moment(value).format("DD MMM YYYY");
}
entry.push(name + ": " + value);
}
}
if (entry.length > 0) result.push(entry.join("\n"));
}
return result.length > 0
? "🔑 Login Information" + line1(28) + "\n" + result.join(line(28)) + line(28)
: "";
};
// Build and send email
let e = entry();
let txt = loginToText(e.field("Login information") || []);
txt += "\n";
// Use only the 'Email' field for recipient
let toField = e.field("Email") || "";
// Send email
AndroidMessages.email(
toField,
(e.field("Account") || "User") + " Login Credentials",
txt
);