How create and insert a new document through custom script

This should work:

frappe.ui.form.on("Wizard", "validate", function (frm, cdt, cdn) {
    var d = locals[cdt][cdn];
    frappe.db.insert({
        "doctype": "Contact",
        "person_profile": d.profile,
        "first_name": d.first_name,
        "last_name": d.last_name,
        "gender": d.gender,
        "email_id": d.email_id
    }).then(function(doc) {
        console.log(doc);
    });
});

or this:

frappe.db.insert({
    doctype: 'Purchase Order',
    supplier: 'Intel',
    items: [
        {
            'item_code': 'XXX141- C - WF',
            'item_name': 'XXX141 - C - WF',
            'schedule_date': '2019 - 03 - 30',
            'description': 'Wafer Test',
            'qty': 12,
            'stock_uom': 'EA',
            'uom': 'EA',
            'conversion_factor': 1,
            'base_rate': 12,
            'base_amount': 12,
        },
    ]
}).then(function(doc) { 
    console.log(`${doc.doctype} ${doc.name} created on ${doc.creation}`);
});

Also, see here:

5 Likes