Item Master Script

61 views
Skip to first unread message

MarkW

unread,
Apr 24, 2013, 4:32:11 AM4/24/13
to erpnext-dev...@googlegroups.com
Hello all,

I am trying to create a script that populates the Item ID, when creating a new item, with the next available number in a sequence. I have a script attached to a button on the item master page that runs correctly and extracts the item group, forms a new ID and updates the ID field.

What I need now is some information on how to get the next sequence number. I can think of two ways of doing this (1) iterate through all of the already created items to find the highest number or (2) use a naming series and have it give me the next number in the series.

I think #2 would be preferable, but I can't find any information on how to go about doing either. Can any of you experts help with this information?

Regards,

MarkW

Anand Doshi

unread,
Apr 24, 2013, 5:33:22 AM4/24/13
to erpnext-dev...@googlegroups.com
Hi Mark,

Create a Custom Field of type Select and label "Naming Series" for Item doctype. Specify possible Naming Series options in the Options field, separated by newlines. Also, mark it Mandatory.

The next part is a trick.
Via the browser's address bar, go to yourdomain/app.html#Form/Property Setter/New Property Setter 1

Save a property setter with the following values, without the double quotes:
DocType or Field = "DocType"
DocType = "Item"
Property = "autoname"
Property Type = "Data"
Set Value = "naming_series:"

Go to Tools (last icon on the top bar) > Clear Cache & Refresh.

Try saving a new Item. It should take the correct series.

Thanks,
Anand.

--
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 post to this group, send email to erpnext-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/erpnext-developer-forum/-/brbo-mKnM94J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

MarkW

unread,
Apr 24, 2013, 6:17:42 AM4/24/13
to erpnext-dev...@googlegroups.com
Hello Anand,

Thanks for your reply. I'm sure that would work, but I would perfer the user be able to overwrite the automatically generated value. So for that reason I don't want this action to happen on save, but in reaction to a button press. I don't think your solution is amenable to this as it automatically sets it on save, not under script control.

If I could extract a list of existing IDs I think this would be the most generic solution, but I'm unsure how to extract this from the database.

Regards,

MarkW

Rushabh Mehta

unread,
Apr 24, 2013, 9:44:30 AM4/24/13
to erpnext-dev...@googlegroups.com
Mark,

Here is what you can do


best,
Rushabh


--
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 post to this group, send email to erpnext-dev...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--



Twitter: @rushabh_mehta

MarkW

unread,
Apr 24, 2013, 11:00:32 AM4/24/13
to erpnext-dev...@googlegroups.com
Thanks Rushabh,

That looks almost what I need. I am running this on a hosted system though, so I need to do this with a client side script. I think your code snippet is for a server side script? I'm not really familiar enough with this to know how to translate that to the client side. Is that possible?

Regards,

MarkW

On Wednesday, April 24, 2013 9:32:11 AM UTC+1, MarkW wrote:

Rushabh Mehta

unread,
Apr 24, 2013, 11:47:48 AM4/24/13
to erpnext-dev...@googlegroups.com
Mark,

Its a client script, you can put it in the trigger function of your custom field to select the item prefix - see the updated snippet


The hack is to use the report builder method to get the last id.

best,
Rushabh


--
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 post to this group, send email to erpnext-dev...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

MarkW

unread,
Apr 24, 2013, 12:17:35 PM4/24/13
to erpnext-dev...@googlegroups.com
Again, many thanks.

I'm still struggling I'm afraid. This is how I have implemented your code : https://gist.github.com/GreyMark7650/5453382

Can you see any issues with this as I just get a blank.

MarkW

On Wednesday, April 24, 2013 9:32:11 AM UTC+1, MarkW wrote:

Anand Doshi

unread,
Apr 24, 2013, 12:50:15 PM4/24/13
to erpnext-dev...@googlegroups.com
Hi Mark,

What are you getting in the callback of wn.call method? Are you getting any value? or are you facing an issue with setting the value of the field?

Thanks
Anand.

--
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 post to this group, send email to erpnext-dev...@googlegroups.com.

Rushabh Mehta

unread,
Apr 25, 2013, 1:05:15 AM4/25/13
to erpnext-dev...@googlegroups.com
Mark,

Can you check what response you are getting on the call by keeping your javascript console window open?

best,
Rushabh


W: https://erpnext.com
T: @rushabh_mehta

On 24-Apr-2013, at 9:47 PM, MarkW <wayfinder...@googlemail.com> wrote:

MarkW

unread,
Apr 25, 2013, 3:58:02 AM4/25/13
to erpnext-dev...@googlegroups.com
Thanks to everyone who responded. Your last questions pointed me in the right direction. The routine was returning the correct value and populating the field, but the display wasn't updating. I don't think the line of code following the callback function was being executed. When I put the "refresh_field" call inside the callback "cur_frm.refresh_field("item_code");" it worked as expected.

Regards,

MarkW

MarkW

unread,
Apr 25, 2013, 4:35:16 AM4/25/13
to erpnext-dev...@googlegroups.com
OK not quite out of the woods yet. I thought I had got it for a while. The code provided works fine now, but I can't work out how to change the pattern for LIKE to a variable so that I can return a result from the appropriate group. Since I couldn't find this out I aslo tried to put the call & callback inside the switch statement , but that also returns syntax errors. Any ideas?


On Wednesday, 24 April 2013 09:32:11 UTC+1, MarkW wrote:

Rushabh Mehta

unread,
Apr 25, 2013, 5:45:21 AM4/25/13
to erpnext-dev...@googlegroups.com
Mark,

"%" is a wild-card. if you want a series with prefix ABC, search for like "ABC%"

if you use cur_frm.set_value - it should also refresh

best,
Rushabh


--
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 post to this group, send email to erpnext-dev...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

MarkW

unread,
Apr 25, 2013, 6:09:48 AM4/25/13
to erpnext-dev...@googlegroups.com
Hi Rushabh,

Yes, I got that, but the prefix needs to depend on which group the Item is in. I have this from the item_group field, but the pattern that LIKE is matching needs to be a variable to accommodate this.

The not updating was my fault, there was another code line later that was reverting the value back.

Regards,

MarkW


On Wednesday, 24 April 2013 09:32:11 UTC+1, MarkW wrote:

Rushabh Mehta

unread,
Apr 25, 2013, 7:19:58 AM4/25/13
to erpnext-dev...@googlegroups.com
Mark,

The sql query is a string -- see my updated gist


best,
Rushabh


W: https://erpnext.com
T: @rushabh_mehta

--
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 post to this group, send email to erpnext-dev...@googlegroups.com.

MarkW

unread,
Apr 25, 2013, 8:15:54 AM4/25/13
to erpnext-dev...@googlegroups.com
OK again thanks to everyone. With the help of this group and a colleague I have come up with this script:


It assumes that the item group has already been selected and that the group name has a prefix of the form"XXX-". The script extracts this prefix and uses it to search for the item with this prefix that has the highest suffix (XXX-YYYYYY). It Increments the suffix by one and populates the item code box with this.


On Wednesday, 24 April 2013 09:32:11 UTC+1, MarkW wrote:

MarkW

unread,
Apr 25, 2013, 8:17:20 AM4/25/13
to erpnext-dev...@googlegroups.com
https://gist.github.com/GreyMark7650/5459251

I think this link works

On Wednesday, 24 April 2013 09:32:11 UTC+1, MarkW wrote:
Reply all
Reply to author
Forward
0 new messages