Client and Server side scripts

Hello guys,

I am finding it quite hard to write custom scripts which work well. I understand the documentation is still in its beta stage. But it would be a great deed if someone would write some more documentation/scripts on client side and server side scripts explaining how to fetch values within the same form, between forms, how to interact with tables,links , assigning values etc. This could help beginners like me to learn quickly and a lot of queries in the forum will be answered automatically.

The above link helps a bit, but its hard to figure out whats happening since its just examples.
Hoping this would be done soon.

Thanks,
Akarsh

2 Likes

Can you post some frequently asked question ?
I will try to give sample code here.

This kb article also helpful https://kb.frappe.io/kb

1 Like

Hey ,
Thank you for showing interest. i am sure this post will help a lot of them like me.

  1. How do we copy value from one field to another field in the same form? (generally)
  2. How do we copy value of one field of form A into another field of form B ?
    3.How to access values of fields within a table of a form ?
    4.What are the different ways of setting values for a field ? what other properties of a field can be modified using code ?

These are some of the questions in my mind right now, which i didn’t find any direct solution. Feel free to use own assumptions for type of field, etc. Just a general example which can be used to replicate in various situations will do.
Also either client or server script will do, whichever you feel is the best practice.
Thank you for your time and efforts :smile:
Regards,
Akarsh

2 Likes

Lets see, we have one requirement.
In Purchase Invoice, We need to add Payment Due Date. Payment Due Date is equal to Posting Date + Credit Days.
Also one more requirement, Supplier Invoice No should be unique in all Purchase Invoice.
Steps to achieve this. 1) Create custom field ‘Credit Days’ and ‘Due Date’ in Purchase Invoice.
To fetch value of Credit Days from supplier into purchase invoice, write this script in Purchase Invoice.

cur_frm.add_fetch(supplier ,'credit_days','credit_days');
  1. To calculate Due Date = Posting Date + Credit Days, write below script in Purchase Invoice.

    frappe.ui.form.on("Purchase Invoice ", " validate ", function(frm) {
    var nos = frm.doc.credit_days* 1;
    var ddate = frappe.datetime.add_days(frm.doc.posting_date,nos);
    cur_frm.set_value(“due_date”, ddate);
    });

Note:
i) doc.field_name will give value of current doc.
ii) cur_frm.set_value(“field_name”, value) used to set value.

  1. To add validation, Supplier Invoice No should be unique in all Purchase Invoice.
    (This will explain how to call server side method using hooks.)

In hooks.py write this code

doc_events = {
    "Purchase Invoice": {
        "validate": "sf_custom_changes.sf_acc.purchase.validate_bill_no"
    }
}

In purchase.py write this code

def validate_bill_no(self, method):
        if self.bill_no:
            # validate bill no is unique
            bill_list = frappe.db.sql("""select name from `tabPurchase Invoice` where bill_no=%s and docstatus =1""",
                self.bill_no)
            if len(bill_list) > 0:
                items = [e[0] for e in bill_list if e[0]!=self.name]
                frappe.throw(_("Supplier Invoice Number must be unique. Current Supplier Invoice Number already exists for {0}").format(comma_and(items)))
        if self.bill_date:
                if getdate(self.bill_date) > getdate(self.posting_date):
                    frappe.throw(_("Supplier Invoice Date cannot be after Purchase Order Date"))

This will help you to write client and server side script. Please ask if you have any doubt in this example.

5 Likes

Hey,
That was useful. It might take some time for me to sink it in. Meanwhile, Cant we modify the value of a field which is ‘read only’ ?
I have a form which has employee link field which a read only field called ‘def_shift’.
When i select the employee, i want the ‘shift_def’ field of that employee to be copied to ‘def_shift’ in this new form. I have used this code. But it doesn’t seem to work.

cur_frm.cscript.emp =function(doc) {
cur_frm.add_fetch("Employee","shift_def","def_shift");
refresh_field("def_shift");
}

emp is the field where i will choose the employee. This is the screenshot of the new form.

Let me know where have i gone wrong.

add_fetch should be outside of any function

cur_frm.add_fetch(employee,'shift_def','def_shift');

this should work.

Hey,
I tried, i am afraid it didnt work :frowning: [i have tried with refresh and without as well]
code :

cur_frm.add_fetch("Employee","shift_def","def_shift");
cur_frm.refresh_field("def_shift");

screenshot:

1 Like

cur_frm.add_fetch(employee,'shift_def",‘def_shift’);
Try this,
Employee should be small.

The kb article https://kb.frappe.io/kb38 has been moved.
Please assist with updated link if it is available.

Thanks,
Gene May

You can check following links.
https://frappe.io/docs/user/en/guides/basics
https://frappe.io/docs/user/en/guides/app-development

Thank you.

Regards,
Gene May