Validation on Creating Opportunity from Lead


Im trying to validate the mobile number field from the lead form in order to create an opportunity. I’ve made a custom script which checks for the mobile number, but I’m getting stuck on the else statement.
Could someone please help me out?

cur_frm.cscript.create_opportunity = function(doc) {
if(cur_frm.doc.mobile_no.length < 10 ){
msgprint("Please Enter a Valid Mobile Number"); 
validated = true;}

Thanks a lot!

P.S- No else statement doesn’t work either.

You can use:

cur_frm.cscript.original_create_opportunity = cur_frm.cscript.create_opportunity;

cur_frm.cscript.create_opportunity = function() {
	if(cur_frm.doc.mobile_no.length < 10 ){
		frappe.throw("Please Enter a Valid Mobile Number"); 
	} else {

Hi @anand,

Thanks so much!

I didn’t realize I was re-defining the function. Thought I was just passing a validation function.

Just out of curiosity, is there anyway to validate a field before running the function instead of re-defining the function?

Thanks again!

@Tanuj say you want to validate mobile_no

Then, this function is triggered when mobile_no is changed.

frappe.ui.form.on("Lead", "mobile_no", function(frm) { // do something });


Is there no way to validate only for “create opportunity”?

I basically want to allow leads to be made without the mobile no. But not go further without it.

Your first answer works perfectly but I just wanted to know if it could happen without redefining the function.

Thanks again!

that is why you always use frappe.ui.form.on("..")

CC @anand

1 Like

Thanks @rmehta


Won’t frappe.ui.form.on will get triggered in parallel for “create_opportunity” and thus won’t stop a user from creating an opportunity even if the mobile no is invalid?

@anand Saw the 2nd example. cscript should always be deprecated. Though in this example you need to explicitly override.