Client Script to Custom Method: Passing in Form Data?

I want to create a custom button via client script that will create a new document (Communication).

What I don’t know is how to pass in the arguments (the fields values for the Communication) from the javascript to the custom method/server script.

My Client Script is

frappe.ui.form.on("Lead", "refresh", function(frm) {
    frm.add_custom_button(__("Log Call"), function() {
        // When this button is clicked, do this

       
let d = new frappe.ui.Dialog({
    title: 'Enter details',
    fields: [
        {
            label: 'Subject',
            fieldname: 'subject',
            reqd: 1,
            fieldtype: 'Data'
        },
        { fieldname:"section_break_1",
          fieldtype: 'Section Break'
        },
                       {
            fieldname: 'column_break_1',
            fieldtype: 'Column Break'
        },
        {
            label: 'Direction',
            fieldname: 'direction',
            fieldtype: 'Select',
            reqd: 1,
            options: ['','Inbound','Outbound'],
        },
                               {
            fieldname: 'column_break_2',
            fieldtype: 'Column Break'
        },

        {
            fieldname:'section_break_2',
            fieldtype: 'Section Break'
        },
                {
            label: 'Message',
            fieldname: 'message',
            fieldtype: 'Long Text'
        },
    ],
    size: 'large', // small, large, extra-large 
    primary_action_label: 'Submit',
    primary_action(values) {
        frappe.call({
            method: "communication_from_call",
            args: {subject:frm.doc},
        })
        console.log(values);
        d.hide();
    }
});

d.show();
            
    });
});

My Server Script (API) is

doc = frappe.new_doc('Communication')
doc.subject = 'Test Subject'
doc.communication_type = 'Communication'
doc.status = 'Open'
doc.sent_or_received = 'Sent'
doc.insert()```

Hello,
on server side you can get the args saved in frappe.form_dict.{arg_name}.
Jirka

How do you pass in data from the Client Script form fields?

For example, my Client Script has the following fields.

   fields: [
        {
            label: 'Subject',
            fieldname: 'subject',
            reqd: 1,
            fieldtype: 'Data'
        },
        { fieldname:"section_break_1",
          fieldtype: 'Section Break'
        },
                       {
            fieldname: 'column_break_1',
            fieldtype: 'Column Break'
        },
        {
            label: 'Direction',
            fieldname: 'direction',
            fieldtype: 'Select',
            reqd: 1,
            options: ['','Inbound','Outbound'],
        },
                               {
            fieldname: 'column_break_2',
            fieldtype: 'Column Break'
        },

        {
            fieldname:'section_break_2',
            fieldtype: 'Section Break'
        },
                {
            label: 'Message',
            fieldname: 'message',
            fieldtype: 'Long Text'
        },
    ],

How do I pass in the subject field using frappe.call({
method: “communication_from_call”,
args: {subject:d.???},
})

Hi, you can pass all “values” in arg in frappe.call function in client script:

    frappe.call({
        method: "communication_from_call",
        args: {values},
    })

On server side in frappe.form_dict you can find this dict:

{‘values’: ‘{“subject”:“subject”,“direction”:“Inbound”,“message”:“message”}’, ‘cmd’: ‘communication_from_call’}

=>

data = json.loads(frappe.form_dict.get('values'))
subject = data.get('subject')

Jirka