Question on Custom script

47 views
Skip to first unread message

Nguyen Do Le Bao

unread,
May 25, 2014, 11:10:24 AM5/25/14
to erpnext-dev...@googlegroups.com
Hi, 

So i am using the custom script to add some client functionality to Sales Invoice. 
And there are some behaviour i really don't understand. Here is the code:

cur_frm.cscript.custom_refresh  =  function(doc, dt, dn) {
  // Loop through Item
  var sales_invoice = locals[dt][dn];
  var total_com = 0;
  var sales_person = wn.model.get("Sales Invoice");
  cur_frm.set_value('sales_commission_value', -2); // This code works, it set the 'sales_commission_value' field to -2
  wn.call({
    method: "accounts.doctype.sales_invoice.sales_invoice.get_sale_commission",
    args: {},
    callback: function(r) {
      console.log("Received value ", r); // Received  r = {message:'1'}
      cur_frm.set_value('sales_commission_value', r.message); // This doesn't work, however, it does not set value of the field to 1 
    }
  });
}
 
As I printed out the two 'cur_frm' seems to be the same var, but one's value can be set (outside of wn.call) and one cannot ? 
This happens to New Sale Invoice, initiated from an existing Delivery Note. 
Does anyone have the explanation for this ? Thank you!

Rushabh Mehta

unread,
May 25, 2014, 11:21:29 PM5/25/14
to erpnext-dev...@googlegroups.com
When you loop, the calls get fired immediately (they are asynchronous, i.e. get fired on different threads), so the callbacks are all seeing different versions of cur_frm object and overwriting it.

If you have variable commission per item, you will have to write a server side functions to return all the commissions in one call, then set them all together.


--
Note:
 
If you are posting an issue,
1. 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.
2. Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
3. 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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/c2d6b886-6830-42dd-a88f-e97f89b002cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nguyen Do Le Bao

unread,
May 26, 2014, 12:05:44 AM5/26/14
to erpnext-dev...@googlegroups.com
I am really sorry the comment // Loop through item confused you. It should have been removed.
As you can see in the code, there are no loop involve. I am just simply use wn.call to call a function on Sales Invoice and set the value of cur_frm in the callback method. 

But i will definitely take note what you said abt. looping the items, I need that as well. 
Thank you!
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.

Rushabh Mehta

unread,
May 26, 2014, 12:48:04 AM5/26/14
to erpnext-dev...@googlegroups.com
Can you post your latest script to pastebin / gist.github.com?

Let us please follow this simple rule while posting scripts. Otherwise it becomes very un-readable.



@rushabh_mehta

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/bcfab877-f97a-4b7b-b068-60062ede8c34%40googlegroups.com.

Nguyen Do Le Bao

unread,
May 27, 2014, 6:08:21 AM5/27/14
to erpnext-dev...@googlegroups.com
Sure, here you go: 

This is not calculated only when it's a new Sale Invoice made by Delivery Note\Actions\Make Invoice. Thank you!
And sorry for the trouble!

Rushabh Mehta

unread,
May 27, 2014, 6:11:55 AM5/27/14
to erpnext-dev...@googlegroups.com
What does this line do?

wn.model.get("Sales Invoice");

If this is an ajax call, then it will overwrite your frm. Otherwise can't see a problem.


@rushabh_mehta

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/96cd0498-c557-4662-8e5a-cdb33d7b62ea%40googlegroups.com.

Nguyen Do Le Bao

unread,
May 27, 2014, 6:36:40 AM5/27/14
to erpnext-dev...@googlegroups.com
Okay for testing purpose I removed that line as well, it is still not working.

The main wn.call to get_sale_commission is also ajax right ? Will the cur_frm variable inside the callback refer to the current form ?

I just wonder why it works when the Sale Invoice saved, only not working when make a new one...

Rushabh Mehta

unread,
May 27, 2014, 6:40:10 AM5/27/14
to erpnext-dev...@googlegroups.com

On 27-May-2014, at 4:06 pm, Nguyen Do Le Bao <natha...@gmail.com> wrote:

> Okay for testing purpose I removed that line as well, it is still not working.
>
> The main wn.call to get_sale_commission is also ajax right ? Will the cur_frm variable inside the callback refer to the current form ?

cur_frm is the same.

>
> I just wonder why it works when the Sale Invoice saved, only not working when make a new one...

Thanks for this information. It would help if you give all the details upfront. I don't think that should makes a difference though - there could be something else that is interfering.

>
> --
> Note:
>
> If you are posting an issue,
> 1. 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.
> 2. Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
> 3. 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/859e9219-1b14-4010-b378-1ab595a9cdd3%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages