Add button at doctype

Hi,

i wonder how i can add a button like stop button in invoice in my new module?

Thanks

1 Like

Check https://github.com/frappe/erpnext/blob/develop/erpnext/selling/doctype/sales_order/sales_order.js#L45

1 Like

Hi ,

thanks its works , but i confuse how to remove the button after the user click it…
this is my code so far :
cur_frm.cscript.custom_refresh = function(doc) {
if(doc.docstatus==1) {
cur_frm.add_custom_button(__(‘Sell / Disable’), cur_frm.cscript[‘Sell’],“icon-exclamation”, “btn-default”)
}
}

cur_frm.cscript[‘Sell’] = function() {
var doc = cur_frm.doc;

var check = confirm(__("Are you sure you want to Disable ") + doc.name);

if (check) {
    return $c('runserverobj', {
        'method':'stop_asset',
        'docs': doc
        }, function(r,rt) {
        cur_frm.refresh();
    });
}

}
the sell asset button i want to remove it after user click it , but im confused how to do it…please help…

thanks

You should only show the button based on some condition. You can add another condition with if(doc.docstatus==1) based on the value you set on click of the button.

Hi,

Ya, im already do that, but what i mean is, when the button is clicked i changed the docstatus to be 0, but the button is still there, i need to refresh manually to make it gone , i want it gone when i clicked the button

Thanks

I did not understand why you want to change the docstatus to zero? You can’t do that (1 to 0) after submitting a document, system will give a transition error.

Can you share the server side function stop_asset?

sure , here
from future import unicode_literals
import frappe
from frappe.model.document import Document

class Asset(Document):
def validate(self):
self.book_value=self.asset_value
val=frappe.get_doc(“Depreciation”, self.depreciation)
self.deprecation_value=val.valueself.asset_value/1200
self.counter=val.periode
12
self.last_deprecated=self.posting_date

def stop_asset(self):
    frappe.db.sql("""update tabAsset set counter=0 , book_value=0,docstatus=2 where name="{}" """.format(self.name))

sorry i changed it to 2, becase after stop the asset, the asset will no longer active and the document actually is not amend able and not cancel able

Thanks

You have to assign the value of docstatus to self as well.

def stop_asset(self):
    frappe.db.sql("""update tabAsset set counter=0 , book_value=0,docstatus=2 where name="{}" """.format(self.name))
    self.docstatus = 2