Update field based on previous field

I was creating student information management application using frappe.
I wanted to update a field called fees that is depended on year field.

for this i wrote client script but its not working, any suggestions ?

screenshots are attached below.

When do you want this to work? If you want a field to be automatically updated with the fee once you select the year, then the code is wrong. It’s wrong because you’re launching the code on loading the page instead of the year field’s value change.

For your code to work as you have written it. you would need to select the year. save it, and refresh the page.

If that was your intended user experience, then you just need to solve a little issue with your syntax:

Basically, all fields need to have **frm.doc.**field_name. You wrote field_name directly. So it should be form.doc.fees instead of just fees. Similarly, it should be frm.doc.year instead of just year.

I want this to work when year field is filled.
it should display fees as specified in script for that specific year.
As below screenshot attached ,
In this form second year is filled in year field so i need fees to be automatically display for that second year specified fees as in code.

Ok, so your new code is wrong again. Now you are working on Refresh.

frappe.ui.form.on('DocType Name', {
    on_submit: function (frm) {
       //write code to run when user submits form.
    refresh: function(frm, cdt, cdn) {
         //this is where you wrote your code. on the form refresh. this only runs every time you open or refresh the form.
//this is where you should write. this is outside the refresh but inside the frappe.ui.form.on bracket. this will garantee that whenever the year value changes, this code should run.
	year: function(frm, cdt, cdn) {
//write your code in here ...
1 Like

Also, another thing, I just noticed. You wrote “form.doc”, make sure its “frm.doc”
If you see in the refresh function, you are importing (frm). Keep an eye out for those kinds of syntax errors, as it will not know what to do.

@Saba_Fatima - In the first screenshot where you have mentioned the fields, the first 3 custom fields: USN, Year and Department are linked to Student. This does not seem to be correct. You should be linking them to their respective doctypes.

If you can link the year to Year doctype, here is my suggestion:

When you are using values in the code you are hard coding it. You loose on scalability and are required to change the code every time there is a change in the fee structure.

My suggestion would be to add a custom field Fees in the Year doctype and use fetch from in the docfield of Fees in the Feespayment doctype. You will have the flexibility without writing a single line of code.

sorry for replying late but thanks for help.
Your suggestions worked for me.
Thank you!

Sorry for replying late,
Thank you for help