How to declare and access variable in public scope

i want to store dialogue box details and use it to insert to other doctype on saving current doctype

 use_portal_for_invoice:function(frm){
	if(frm.doc.use_portal_for_invoice){
		var d = new frappe.ui.Dialog({
			title: __("Online Portal Credentials"),
			fields: [
				{"fieldname":"url", "fieldtype":"Data", "label":__("URL"),
					reqd: 1},
				{"fieldname":"user_name", "fieldtype":"Data", "label":__("User Name"),
					reqd: 1},
				{"fieldname":"password", "fieldtype":"Password", "label":__("Password"),
					reqd: 1},
				{"fieldname":"client", "fieldtype":"Link","options":"Customer", "label":__("Client"),
					"default":frm.doc.client,reqd: 1,hidden:1},
			]
		});
		d.set_primary_action(__("Save"),function(){
			var values = d.get_values();
//this below frappe call should perform on saving of doctype and not here
			frappe.call({
				method: "//path of the function",
				args: values,
				callback: function(r) {
					d.hide();	
				}
			});
		})
		d.show()
	}
},
before_save:function(frm){
//access variable values
frappe.call({
	method: "//path of the function",
	args: values,
	callback: function(r) {
	}
});
}

basically i wan to access the variable 'values in before_save event

I’m not sure if I understand you correctly. I think you want to only apply the values from your dialog in before_save for that you should define a global variable on the upper scope, top of your file and when running the primary_action of your dialog assign the values to said global variable. Then you would have access to it in the before_save callback.

@Javier_Lopez Yes this is what I asked. Can you give me an example to define global variable

Here is what I tried
frappe.ui.form.on(“Sales Order”, {
//Defining global variable
var values = 0;
before_save: function(frm){
// Function to perform before save
}
});

I can’t define as this. Tell me the right way

Well that’s JavaScript basics, you need to study some! But Here’s how I’ll do it., not a JS expert either.

  1. Using the global window object or a upper scope/global variable.
// Declare the variable in the top scope
// So you have access to it in the inner scopes
let myDialogValues;

let myDialog = new frappe.ui.Dialog({
    title: 'Enter details',
    fields: [
        {
            label: 'First Name',
            fieldname: 'first_name',
            fieldtype: 'Data'
        },
        {
            label: 'Last Name',
            fieldname: 'last_name',
            fieldtype: 'Data'
        },
        {
            label: 'Age',
            fieldname: 'age',
            fieldtype: 'Int'
        }
    ],
    primary_action_label: 'Submit',
    primary_action(values) {
        // Here you get the values from your dialog, don't execute the
        // action, just assign it to a global variable
        myDialogValues = values
        // You can also add it to the window global object
        window.myDialogValues = values
        console.log(values);
        d.hide();
    }
});

frappe.ui.form.on('Whatever', {
     use_portal_for_invoice:function(frm){
    	if(frm.doc.use_portal_for_invoice){
            myDialog.show()
        }
    },
    before_save:function(frm){
    frappe.call({
    	method: "//path of the function",
        // Now here you can reference your saved values
        // Both should work, but not exactly sure, test it.
        // You should also process first to make sure values
        // exist and not just call the method with no parameters.
    	args: myDialogValues,
    	args: window.myDialogValues,
    	callback: function(r) {
    	}
    });
    }
    

    refresh(frm) {
        // if reference_type and reference_name are set,
        // add a custom button to go to the reference form
        if (frm.doc.reference_type && frm.doc.reference_name) {
            frm.add_custom_button(__(frm.doc.reference_name), () => {
                frappe.set_route("Form", frm.doc.reference_type, frm.doc.reference_name);
            });
        }
    }
})
1 Like

@Javier_Lopez Thank you so much.