In a custom form in my app, I’m trying to use Javascript to get the preferred billing address for the Customer link field in the form (called ‘customer’).
I’m working on the principle that I should get all the addresses linked to the customer and select the address where is_primary_address=1.
I’ve tried the code below but I clearly haven’t got the linked document filter correct since I get an error: “Field not permitted in query: link_doctype”.
I would welcome any advice to help create the correct filter.
This allows me to get all the addresses for a customer. However, I still can’t filter on “is_primary_address” so my code is just picking the first address, which may not be the one I want. Now that I have an array of addresses, I could loop through and check each of them for the primary address flag, unless I can find a way to add it to the filter.
The problem seems to be that the query is returning only a list of document names rather than objects with the primary address flag included so "order_by: ‘is_primary_address’ " causes an error.
The issue here is that get_list doesn’t fetch data from child tables when polling a parent or data from parent tables when polling a child. (Also, without the parent_doctype field, your code in your second post works for me only when logged in as Administrator.)
You can do what you want to do with two api calls:
If you’d prefer to avoid the second API call, the easiest way is probably to create a new server side method to handle the processing, either in a custom app or a server script.
Thank you very much, Peter. That got me to where I wanted to be. For some reason the “await” keyword threw a syntax error in my code but using asynchronous calls worked fine.
I’m happy to use two calls. In fact, I also want to get the preferred shipping address so I can get the list of addresses just once and then use that to get the preferred shipping and billing addresses.
Thanks, Peter.
Interestingly, I had tried omitting the “fields” argument but this led to get_list failing with a KeyError on “parent”. Not on “name”, though. Odd.
I’m happy with this solution. As you say, it would be nice if get_list included child table data but now I know how to do it, it isn’t a big problem.