Client Script on Salary Slip

I have a client script that runs perfectly well if i create and save a salary slip directly from salary slip. However, when salary slip is created from payroll entry this script is not run. Any help with it plz?

frappe.ui.form.on(‘Salary Slip’, ‘after_save’, function(frm, cdt, cdn)
{
frappe.call({
method:“frappe.client.get_value”,
async:false,
args:{
doctype:‘Lunch Mess’,
filters:{
employee:cur_frm.doc.employee
},
fieldname:‘opt_in’
},callback:function(r){
console.log(r.message);
if(r.message !== undefined){
cur_frm.set_value(‘opt_in’,r.message.opt_in);
cur_frm.refresh_field(‘opt_in’);
cur_frm.refresh_field(‘earning_deduction’);
cur_frm.refresh_field(‘opt_in’);
cur_frm.save();
}
}
});
});

Client scripts are triggered only when you interact with the doc through the UI as it runs on the client side (browser). As you’re creating the Salary Slip from the Payroll Entry, the client script won’t work.

You should try achieving this with a server script instead.

Thanks. Would the same scrip / code work in server script?

1 Like

Just try it
it should work but you will not know, until you try

Tried; it doesn’t work.
I just need to fetch data from one doctype to salary slip for each employee.

No.

Client script is written in Javascript, whereas Server Script runs on Python.

Reference:
https://docs.erpnext.com/docs/v13/user/manual/en/customize-erpnext/server-script

1 Like

@usmanalikhan - If I am able to understand it correct, you have a custom doctype ‘Lunch Mess’ and it stores opt_in for employees. You would like to set the opt_in in salary slip from this custom doctype matching the employee.

You can create a server script for Salary Slip Doctype on Before Validate with the code below:
doc.opt_in = frappe.db.get_value(‘Lunch Mess’, {‘employee’ : doc.employee}, ‘opt_in’)

I am unaware of your use case, here are my comments.
You do not need a custom doctype, a custom field in Employee doctype would also have sufficed. Alternatively, you can also add it to the salary structure assignment doctype. The benefit of this approach is that you can use the lunch mess as a condition in the salary component.

3 Likes

Thank you @atulagrawal . This code worked perfectly!

Just to add to the use case: the reason I created a custom doctype was that this allows individual employees access to choose whether to opt-in or opt-out of lunch mess for a particular month. We dont want employees to be able to edit employee data and adding it as a salary component would also not allow the flexibility for employees to change their option for the month, if they so wish.

Thanks a lot again for your help!