Multiple Choice Field - Check Items

I am loading a Multiple Choice field based on a pnpjs query. I figured out how to add Options to the list, but I am trying to figure out how to check certain ones based on if they exist in array: Here is my code:

let selItems;
    if (fd.field("Serial_x0020_Number").value.length > 0) {
        selItems = fd.field("Serial_x0020_Number").value.split("\n");
    }
    console.log(selItems);
    sp.web.lists.getById("MyListID").items.filter("Product/Id eq " + ProductId + " and Status eq 'Active'").orderBy("ID", true).get().then(
        function(Products) {
            if (Products.length == 0) {
                fd.field("InventoryItems").options.push("No Items available in Inventory");
            } else {
                Products.forEach(p => {
                    /*Here is where I want to see if the current Product is in selItems, if it is, I want to add it and mark it selected*/
                    let current_datetime = new Date(p.Created);
                    let formatted_date = current_datetime.getMonth() + "-" + (current_datetime.getDate() + 1) + "-" + current_datetime.getFullYear();
                    fd.field("InventoryItems").options.push(p.Title + " : " + p.ID + " : " + p.Model + " : " + formatted_date);
                });
            }
        });

I figured it out.

Products.forEach(p => {
let current_datetime = new Date(p.Created);
let formatted_date = current_datetime.getMonth() + "-" + (current_datetime.getDate() + 1) + "-" + current_datetime.getFullYear();
fd.field("InventoryItems").options.push(p.Title + " : " + p.ID + " : " + p.Model + " : " + formatted_date);
if (selItems.indexOf(p.Title) != -1) {
fd.field("InventoryItems").value.push(p.Title + " : " + p.ID + " : " + p.Model + " : " + formatted_date);
}
});

1 Like