Introduction: Managing Custom Fields in a Custom App
Hello friends.
Today I have a new series and in this one I would like us to look at customizing doctypes that have been built by ERPNext. Now, customizing or creating your own doctypes is not difficult now to do. Hopefully if you have been following my YouTube channel. But there is one thing that I have not covered yet and this one is still becoming a problem to most of the people that follow me on my channel online. So I want to go ahead and dig into this today. I want to explain to you how you can create a custom field on a doctype that may be like Customer. We're going to be working with customer doctype that has been done by ERPNext and/or Frappe framework. And then we are going to be also exploring how we can manage the code that we have written or the field that we have created using our custom application.
So I am going to be showing you how that code is going to be accessible or available to your custom application that you can then push into GitHub and you can install into your application, just in the event that you wanted to install it in another application, or just for the sake of safety when you need to upgrade the version of your ERPNext from one version, let's say for instance 13 to 14 when it is officially released. So, let's dive in and get started. Now my video may disappear here because I want us to have enough room to do this.
Creating a Custom Field in the Customer DocType
So let's open the doctype here. And this is the customer doctype. This doctype is created by ERPNext. Now let's say for instance we needed to add a field anywhere here. Now what we do, there is another field I want just to open a new tab here so that we don't lose the customer form. And here you can look for something like a custom field. You can see here we have custom field list and you can add your own custom field. Now the first thing that we need to do here is to select the document where we want to add our custom field. Now our document here is going to be customer. Now note that if you needed to add your custom field in maybe let's say Sales Invoice, you just type here sales invoice and you select it. If it was student, of course I have not installed the student module. So that that's how you do this. So we need customer in our case. So select customer.
The label, this is the label that you want to see on your field. For instance the label of this first field here is Full Name. The label of the company here is Type. Here we have PAN and these are what are called labels. Let's say for instance I want to call this something like, what do we call this? Something like, let me see what we can add into our customer here. We can add something like, let's just call it any name. So let me just say Test Field. This is just for the purpose of testing.
And then the next thing here is very very important is where you need it to be added. Now you need to select this. If you do not select anything here, your field is going to be added to the top of the document here. That is not how you want it. You want your form to be organized in a way that you have control over. So we want our form to come just after full name. So we can just come here and say insert after and select full name here. If you wanted it to be after any other field here we can just go ahead and select that field from here. So that is full name and then here of course is the other form or input of custom field details here that you need to select like the data type, the field type data or whatever you want to insert here, whether they select options, fetch from, and all these others. I know you're familiar with them. Thank you up to there. So we can go ahead and save this.
So when we save, just after saving here, this field is now available in our customer doctype here. So if we go ahead and reload we expect that here we are going to see our our test field. And yes, you can see here we have a test field and this is what we created.
The Version Control Challenge: Untracked Changes
Now that's not enough. It is here, but then how about when you need to export this field and actually put it on something like GitHub? And then we can manage that code when we need to install our custom application into another instance of our server. Let's say for instance we have production and development or we have multiple servers for some reason. Or maybe your system has a challenge and it crashed and you needed to install this. You want your custom fields to be managed in such a way that by installing your custom app it will come with all these fields that you have created.
Now remember the fields that are here may be in the tens, in hundreds or even in thousands if you have a big application. So you do not want to go and start recreating them again just in the event of something happening to your instance. So how do we do that? For that I'm going to show you my terminal. So here's my terminal, let me just go ahead and clear this. This is the instance that we are running on. I can get out of that. And the thing I want to show you is here, when I do bench version, let me just make this a little bit bigger for the sake of your eyes. You can see that here we have an app called opersoft and in version 0.0.1. Now this is the app that we want to manage our application. Now note that we have already created a custom field here. So we are expecting that when we do a git version, not git version but git status here. If by the way you don't know what this is, I showed you how to to manage the custom code with GitHub and I think yes here is the code. You can go ahead and check this if you want to learn more about how to manage custom code in ERPNext or Frappe. So if I check git status here, notice that, all right we need to be in there. So let me go to apps and then I go to opersoft, remember that is where we want to check. And then git status is telling us that there is nothing to commit here. Now note, it is telling us there is nothing to commit and we have already created a test field. That means our test field is not being tracked by our application.
Configuring Fixtures in hooks.py
So how do we help our application to know that we want to track this? Now there is one more thing that you need to do and that thing is if you open your apps, let me also make this bigger for the sake of your eyes also. If you enlarge up apps and then you go to opersoft and opersoft again, there is going to be a hooks file here. Now this file is very very important. I once, in my tutorials went through this. If you have any questions please drop them in the chat section and I'm going to address them one by one. Or maybe other members in the community are going to assist you. But if you have anything just drop it in the chat section.
Now here we need to tell our application that we want to track our our custom fields. And how we do that, we add another attribute here that we are going to call fixtures. fixtures and this fixtures is going to be a list. And this list is going to contain a number of things but in this case, this first case, the only thing we need to add here is the name Custom Field. And this Custom Field is what is going to tell our application that we want to start managing our application like this.
Now even before we save this hooks.py file, there's one more thing I want to show you. How you tell it that I want to get the custom fields is just by doing bench export-fixtures. Now note bench export-fixtures. Now note I have run this bench export-fixtures before we saved our hooks file. You can see I've not saved the hooks file. This is the change I have made. It is not saved yet. And I have already run bench export-fixtures. Now how about when we check the status now? This still tells us that there is nothing here.
Exporting Fixtures and Verifying with Git
So how do we get it to work? Just save this now, like that. Let me just allow it to save. And now this thing is saved. Now let's go back here and again run bench export-fixtures. When I do that you can see here this is telling us that the custom field opersoft and filters is none. And now when we go ahead and look, do bench status, we can see here that there are two things that have been done here or two files that have been affected. The first one is the hooks file of course, because we have added some code which is this code here, of course that is true. And the second one is our directory and this directory is in opersoft and fixtures.
Let's go there and see what has happened there. So if you come here, opersoft, this directory has been created. So anything, anything that you're going to be adding now as a custom field is going to be added here. So you can see there's a file here called custom_field.json. If you check it closely you can see here it's telling the doctype, telling us that the doctype is Custom Field. And when you scroll down just a little bit, there are so many things here. And I want to show you where our field is. Just come down, come down, come down and you're going to find it somewhere here. Note that all these things have been added from the custom fields that are in that form. So let's check our console field and see where it is. We can search it. The custom field was called test and you can see here our test field. It's telling us that it's a custom field, doctype is Customer and the field name is test_field and it's a data type and all this. So label is Test Field, this is what we added, and insert_after: customer_name. So our code is now being tracked. So any other custom field that you're going to add is going to be added, this object is going to be added into this file here.
Conclusion and Next Steps
And now when this one is on GitHub, of course you can go ahead now and add it to your repository and commit it and now you can start managing it like that. Once this is done, now it has been tracked by your custom app and now you can go ahead and install it anywhere you want to install it.
So that is what I wanted us to look at today and I am hopeful that that has been very helpful. Thank you so much for watching. Go ahead and subscribe to my YouTube channel so that you do not miss the next video that I'm going to be building. That video by the way is going to be on print format. I will be teaching you how you can fetch data from any doctype to your print format into another doctype. Thank you so much and I hope to see you in that video.