Mass create Opportunities from a Prospect

Hi all.

I have recently been trying to create more custom functionality in my ERPNext instance.

I am trying to create opportunities using a custom button, by selecting prospects in the Prospect list and clicking one custom button in the “Actions” drop down to create an opportunity with the prospect information pre-populated.

I have added the button thanks to the help from @NCP as seen below.

So once a user has selected a prospect, or many, from the Prospects list, the action button will have a button called “Create Opportunities” which will handle the task as described above.

I have tried looking for the method to create an opportunity from an individual prospect, which is present under the “Create” dropdown on the individual prospect once clicked, but I have not been successful in finding that method.

I am sure I would be able to achieve the above by using that method and looping through each selected Prospect?

Hi @Hiro,

Please apply the ListView client script and set it according to your specific scenario.

frappe.listview_settings["Prospect"] = {
    onload: function(listview) {
        listview.page.add_action_item(__("Create Opportunities"), () => {
            let prospect_ids = listview.get_checked_items(true);
            if (prospect_ids.length > 0) {
                create_opportunities(prospect_ids);
            } else {
                frappe.msgprint("Please select at least one prospect.");
            }
        });
    }
};

function create_opportunities(prospect_ids) {
    frappe.call({
        method: "frappe.client.get_list",
        args: {
            doctype: "Prospect",
            filters: { name: ["in", prospect_ids] },
            fields: ["name", "prospect_owner", "market_segment", "industry", 
            "territory", "website", "annual_revenue", "no_of_employees"]
        },
        callback: function(response) {
            if (response.message) {
                let prospects = response.message;
                prospects.forEach(prospect => {
                    let new_opportunity = frappe.model.get_new_doc("Opportunity");
                    new_opportunity.opportunity_from = "Prospect";
                    new_opportunity.party_name = prospect.name;
                    new_opportunity.opportunity_owner = prospect.prospect_owner;
                    new_opportunity.market_segment = prospect.market_segment;
                    new_opportunity.industry = prospect.industry;
                    new_opportunity.territory = prospect.territory;
                    new_opportunity.website = prospect.website;
                    new_opportunity.annual_revenue = prospect.annual_revenue;
                    new_opportunity.no_of_employees = prospect.no_of_employees;
                    frappe.db.insert(new_opportunity).then(() => {
                        frappe.msgprint(`Opportunity created for prospect: ${prospect.name}`);
                    });
                });
            }
        }
    });
}

If you want to learn, check it out.
It’s a similar concept for bulk updates or creation, so make sure to watch both videos.

Hope this helps!

2 Likes

@NCP, thank you again! Working perfectly. Appreciate your time and effort.