In our system, we have multiple companies and some users have access to multiple companies. So on Purchase Order screen, we would like to do some client side validation to make sure that the cost center and account that they have selected for each item is for the select company and not other. I have written below script. But I have two issues:
1. I have used r.message.company_name to set a local variable called mycompany. I know this is not a right way to do it. So I need help with setting it in a right way.
2. I do see message box for validation when i tried to save a PO with wrong information, but system still continue with saving the doc. It seems to ignore validated=false statement. I think it is because of For loop but I am not sure how to break out of it. I can't use break statement under if clause.
3. Is there a better way to check for this kind of validation?
Please note we are still using older version of ERPNext. We haven't migrated to Responsive branch yet.
cur_frm.cscript.custom_validate = function(doc) {
// Get all line-items for the selected PO
var el = getchildren('Purchase Order Item',doc.name,'po_details');
// Go through each line item
var mycompany ="";
for(var i in el){
// Check for the company of Cost Center
wn.call({
method:"webnotes.client.get_value",
args: {
doctype:"Cost Center",
filters: {name:el[i].cost_center},
fieldname:["company_name"]
},
callback: function(r) {
mycompany=r.message.company_name;
if (doc.company != mycompany) {
msgprint('<b>' + '\'' +el[i].cost_center +'\'' +' is not a valid ' +'department code for '+ doc.company +'.');
validated = false;
}
}
});
// Check for the company of Expense Code (Account Code)
wn.call({
method:"webnotes.client.get_value",
args: {
doctype:"Account",
filters: {name:el[i].expense_head},
fieldname:["company"]
},
callback: function(r) {
mycompany=r.message.company;
if (doc.company != mycompany) {
msgprint('<b>' + '\'' +el[i].expense_head +'\'' +' is not a valid ' +'expense code '+ doc.company +'.');
validated = false;
}
}
});
}
}
Thanks in advance for your help.
Kind regards,
MP
–
Note:
If you are posting an issue,
- We should be able to replicate it at our end. So please give us as much information as you can. Please see it from the point of view of the person receiving the communication.
- Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
- For sending images, use http://imgur.com or other similar services. Do not send images as attachments. Links are good. Same goes for any file you are going to send.
End of Note
—
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.