Deluge function help

Deluge function help

I am rather new to deluge and trying to write a function.  

I have a form in Creator called BoM that I use as a related list in my Deals module. Within the form is the field Deal_Name (integration with CRM Deals module) and subforms containing products from my Products module. The subforms are Sheet Goods, Lumber, Edgebanding, Hardware, and Finishing. The subforms have Quantity (number), Product_Name (integration with Products module in CRM), SKU (single line), Vendor (single line), Unit_Price (currency). I have a custom module in CRM called Material Orders. The Materials Order module has Name (single line, mapped to Deal_Name), Quantity (number), Product_Name (single line), SKU (single line), Vendor (single line), Unit_Price (currency). I have a button on the details page of Deals called PO Project. When I push the button I want the products from the BoM form to be created as new entries in the Material Orders module.

Like I said, I'm quite new to deluge and scripting in general, but have been trying to learn. I used ChatGPT to try to create a function and after some troubleshooting I think I might be close, but have hit a wall. Here's what I've got so far:

try {
    // Fetch the BoM record linked to the Deal using its unique ID
    bomRecord = zoho.creator.getRecordById("contact4247", "bom", "BoM", "input.dealId", "creatorconnection");

    // Iterate over each subform and create entries in Material Orders
    subforms = ["Sheet_Goods", "Lumber", "Edgebanding", "Hardware", "Finishing"];
    for each  subformName in subforms {
        subformData = bomRecord.get(subformName);
        for each  product in subformData {
            materialOrder = map();
            materialOrder.put("Quantity", product.get("Quantity"));
            materialOrder.put("Product_Name", product.get("Product_Name"));
            materialOrder.put("SKU", product.get("SKU"));
            materialOrder.put("Vendor", product.get("Vendor"));
            materialOrder.put("Unit_Price", product.get("Unit_Price"));
            
            createResp = zoho.crm.createRecord("CustomModule2", materialOrder);
            if (createResp.get("status") == "error") {
                // Logging the error instead of alert
                info "Error creating Material Order in CRM: " + createResp.get("message");
            }
        }
    }

    // Add a return statement at the end
    return "Success";
} catch(exception) {
    // Logging the error
    info "An error occurred: " + exception.toString();
    return "An error occurred: " + exception.toString();
}

It seems like ChatGPT might be somewhat close, but I believe that since deluge is not as popular as other scripting languages it gets mixed up. Can someone help me out here?