Customer is not saving while creating a customer through dialog

When the user clicks the “Create” button, a quick entry form appears where they can enter the customer name. After entering the customer name and clicking “Create,” the customer is created in the Customer doctype, but it remains unsaved. Why is this happening? Please correct this issue.
Below is the Screenshot

Then click Create a new Doc is Created in Customer but it still Unsaved

Below is the code
Help

refresh: function(frm) {
// Check if the document is in “Draft” status or is a new document
if (frm.doc.docstatus === 0 || frm.is_new()) {
{ frm.set_df_property(‘customer’, ‘only_select’, true); }

        // Show the button only for "Draft" status and new documents
        
        // addCreateCustomerButton(frm);

frm.fields_dict['customer'].$wrapper.find('.btn-create-customer').remove();

// Add the button
frm.fields_dict['customer'].$wrapper.append('<button class="btn btn-xs btn-primary btn-create-customer">Create Customer</button>');
frm.fields_dict['customer'].$wrapper.find('.btn-create-customer').css({
    'background-color': '#ffffff',
    'color': '#000000',
    'width': '100%',  // Adjust the width as needed
    'border': '1px solid #808080'
}).on('click', function() {
    frappe.call({
        method: 'frappe.client.get_value',
        args: {
            doctype: 'Company',
            filters: {'name':  frm.doc.company},
            fieldname: ['default_currency',]
        },
        callback: function(response) {
            if (!response.exc && response.message) {
                defaultCurrency = response.message.default_currency;
            }
        }
    });


    frappe.prompt([
        {'label': 'Customer Name', 'fieldname': 'customer_name', 'fieldtype': 'Data', 'reqd': true},
        {
            'label': 'Customer Type1',
            'fieldname': 'customer_type1',
            'fieldtype': 'HTML',
            'options': `
                <div style="display: flex;">
                    <label style="margin-right: 20px !important;">
                        <input type="radio" name="customer_type1" value="Company" checked> Company
                    </label>
                    <label>
                        <input type="radio" name="customer_type1" value="Individual"> Individual
                    </label>
                </div>
            `
        },

        {'label': 'Company', 'fieldname': 'company', 'fieldtype': 'Link', 'options': 'Company', 'default': frm.doc.company,'hidden': true},
        {'label': 'Plan', 'fieldname': 'plan', 'fieldtype': 'Data',  'default': frm.doc.custom_plan,'hidden':true},

        {'label': 'Customer Group', 'fieldname': 'customer_group', 'fieldtype': 'Link', 'options': 'Customer Group' },
        {'label':'indusrty ','fieldname':'industry','fieldtype':'Data','default':frm.doc.custom_industry,'hidden':true},
        {'label': 'Country', 'fieldname': 'country', 'fieldtype': 'Link','options':'Country','default':frm.doc.custom_country,'mandatory_depends_on': 'eval:doc.industry==="IT Service" && doc.plan=="Professional" || doc.plan =="Premium" && doc.industry=="IT Service"','depends_on': 'eval:doc.industry==="IT Service" && doc.plan=="Professional" || doc.plan=="Premium" && doc.industry=="IT Service"'},

        {'label': 'Tax Category', 'fieldname': 'tax', 'fieldtype': 'Link','options':'Tax Category','depends_on':'eval:doc.country=="India"','mandatory_depends_on':'eval:doc.country=="India"','description':'For Transactions Outside your State, Choose "Outstate" .For Transactions within your State, Choose "Instate".'},

        {'label': 'Mobile Number', 'fieldname': 'mobile_number', 'fieldtype': 'Phone', 'mandatory_depends_on': 'eval:doc.industry==="IT Service" && doc.plan=="Professional" || doc.plan =="Premium" && doc.industry=="IT Service"'},

    ],
    function(values){

        
        var newCustomer = frappe.model.get_new_doc('Customer');
        newCustomer.customer_name = values.customer_name;
        newCustomer.customer_group = values.customer_group;
        newCustomer.custom_mobile_number = values.mobile_number;



        // Set the custom_customer_type based on the selected value

    console.log("newCustomer before save call:", newCustomer);

        frappe.call({
            method: 'frappe.client.save',
            args: {
                doc: newCustomer
            },
            callback: function(response) {
                                console.log("Response from save call:", response);
                if (!response.exc) {
                    // frappe.msgprint(__('Customer created successfully!'));
                frappe.msgprint('<div style="text-align:center;"><i class="fa fa-check-circle" style="color: green; font-size: 60px;"></i><br>' +
'<span style="font-size: 20px;">' + __('Customer created successfully!') + '</span>' +
'</div>');

        frappe.call({
            method:"frappe.client.get",
            args:{
                doctype:"Customer",
                name: response.message.name
                
            },
            callback :function(responseion){
              var customerData = responseion.message
                console.log( "Inner ",customerData)
                
                frappe.call({
                    method:"frappe.client.save",
                    args:{
                        doc: customerData
                    },
            callback: function(saveResponse) {
                if (saveResponse.message) {
                    console.log("Customer saved successfully!");
                }
            }

                })
                
            }
        })

                    frm.set_value('customer', response.message.name);

                    
                } else {
                    frappe.msgprint(__('Error creating customer: ') + response.exc);
                }
            }
        });
    }, 'New Customer', 'Create');

});




        
        
        

    }
},

@PRaful_9898 try this frappe.call({
method: ‘frappe.client.save’,
args: {
doc: newCustomer
},
callback: function(response) {
if (!response.exc) {
frappe.msgprint((‘Customer created successfully!’));
frm.set_value(‘customer’, response.message.name);
frm.save(); // Save the form after setting the customer value
} else {
frappe.msgprint(
('Error creating customer: ') + response.exc);
}
}
});

Don’t create the complexity. apply the frappe.db.insert.
please check it.

frappe.ui.form.on('Sales Invoice', {
    refresh: function(frm) {
        // Check if the document is in “Draft” status or is a new document
        if (frm.doc.docstatus === 0 || frm.is_new()) {
            frm.set_df_property('customer', 'only_select', true);

            // Remove existing button if any
            frm.fields_dict['customer'].$wrapper.find('.btn-create-customer').remove();

            // Add the button
            frm.fields_dict['customer'].$wrapper.append('<button class="btn btn-xs btn-primary btn-create-customer">Create Customer</button>');
            frm.fields_dict['customer'].$wrapper.find('.btn-create-customer').css({
                'background-color': '#ffffff',
                'color': '#000000',
                'width': '100%',  // Adjust the width as needed
                'border': '1px solid #808080'
            }).on('click', function() {
                frappe.prompt([
                    {'label': 'Customer Name', 'fieldname': 'customer_name', 'fieldtype': 'Data', 'reqd': true},
                    {
                        'label': 'Customer Type1',
                        'fieldname': 'customer_type1',
                        'fieldtype': 'HTML',
                        'options': `
                            <div style="display: flex;">
                                <label style="margin-right: 20px !important;">
                                    <input type="radio" name="customer_type1" value="Company" checked> Company
                                </label>
                                <label>
                                    <input type="radio" name="customer_type1" value="Individual"> Individual
                                </label>
                            </div>
                        `
                    },
                    {'label': 'Company', 'fieldname': 'company', 'fieldtype': 'Link', 'options': 'Company', 'default': frm.doc.company,'hidden': true},
                    {'label': 'Plan', 'fieldname': 'plan', 'fieldtype': 'Data',  'default': frm.doc.custom_plan,'hidden':true},
                    {'label': 'Customer Group', 'fieldname': 'customer_group', 'fieldtype': 'Link', 'options': 'Customer Group'},
                    {'label':'indusrty ','fieldname':'industry','fieldtype':'Data','default':frm.doc.custom_industry,'hidden':true},
                    {'label': 'Country', 'fieldname': 'country', 'fieldtype': 'Link','options':'Country','default':frm.doc.custom_country,'mandatory_depends_on': 'eval:doc.industry==="IT Service" && doc.plan=="Professional" || doc.plan =="Premium" && doc.industry=="IT Service"','depends_on': 'eval:doc.industry==="IT Service" && doc.plan=="Professional" || doc.plan=="Premium" && doc.industry=="IT Service"'},
                    {'label': 'Tax Category', 'fieldname': 'tax', 'fieldtype': 'Link','options':'Tax Category','depends_on':'eval:doc.country=="India"','mandatory_depends_on':'eval:doc.country=="India"','description':'For Transactions Outside your State, Choose "Outstate" .For Transactions within your State, Choose "Instate".'},
                    {'label': 'Mobile Number', 'fieldname': 'mobile_number', 'fieldtype': 'Phone', 'mandatory_depends_on': 'eval:doc.industry==="IT Service" && doc.plan=="Professional" || doc.plan =="Premium" && doc.industry=="IT Service"'},
                ],
                function(values) {
                    let new_cust = frappe.model.get_new_doc("Customer");
                    new_cust.customer_name = values.customer_name;
                    new_cust.customer_group = values.customer_group;
                    new_cust.mobile_number = values.mobile_number;
                    frappe.db.insert(new_cust).then(() => {
                        frappe.msgprint(`Customer created successfully: ${values.customer_name}`);
                    });
                });
            });
        }
    }
});
1 Like

Thank you, NCP, for the simple code. My code is complicated, but it’s working. It’s not a code issue; my another code is working in backround . I commented out all my background codes in Customer Doctype and found the issue.
Also Where can I find references for easy Frappe codes like this?