Client Script, Server Script, System Console - examples and walthroughs for beginners

Here we are explaining clients scrips and servers scripts for developers who start programming Frappe or ERPNext.

 · 3 min read

How Long Does it Take to Be a Computer Programmer|Disney Codeillusion


We have a an explanation video on ERPGulf Youtube channel about this document. The concepts and explanation of following codes are explained in that video. You can watch the video here ( Client Script, Server Script, System Console - examples and walthroughs for beginners - YouTube )


Log and alert methods used in script


Server script example  ( runs on both servers script and system console )

frappe.msgprint(frappe.get_url())

frappe.throw("throw")    --- stops the execution.


client alert( "Clint script " )

client - console.log("this log is On refresh ")

frappe.msgprint(__("Message printed on Refresh"));

frappe.throw(__("Message thrown on refress"));


system log

log(frappe.get_url())

frappe.msgprint(frappe.get_url())



Activate server script execution on a site

bench --site site1.erpgulf.com set-config server_script_enabled true


Server script types

doctype event

Schedule event

permission que

API



Server script execution events

Before Insert

Before Save

After Save

Before Submit

After Submit

Before Cancel

After Cancel

Before Delete

After Delete

Before Save (Submitted Document)

After Save (Submitted Document)


Client script -

adding a button


frappe.ui.form.on("Issue", "refresh", function(frm) {

   frm.add_custom_button(__("Do Something"), function() {    

       alert("Hello from client script")

   });

});



--------


change color  using client script

frappe.ui.form.on('Issue', {


 refresh: function(frm) {


   frm.fields_dict['sb_details'].wrapper.css('background-color', 'lightblue');


   frm.fields_dict['section_break_19'].wrapper.css('background-color', 'gold');


 }


});

---------

Use our twitter feed for more examples ( https://twitter.com/erpgulf/status/1649710391117639680 )

-------------

making changes on one field change


frappe.ui.form.on('Issue', {

"subject": function(frm) {

frm.set_value('tax_id', frm.doc.subject);

}

});


-------------

Calculate with both fields


frappe.ui.form.on('Issue', {

   "col_1": function(frm) {

   frm.set_value('col_3', frm.doc.col_1  + frm.doc.col_2);

   }

});


frappe.ui.form.on('Issue', {

   "col_2": function(frm) {

   frm.set_value('col_3', frm.doc.col_1  + frm.doc.col_2);

   }

});


--------------

add a conditional color changing for the above


if (frm.doc.col_3 > 100 ) {

   cur_frm.get_field('col_3').$input.css('background-color', 'gold')

   } else {

   cur_frm.get_field('col_3').$input.css('background-color', 'lightblue')

   }

   

--------------------




fetching customer ID


frappe.ui.form.on('Issue', {

"customer": function(frm) {

cur_frm.add_fetch('customer','tax_id','tax_id')

}

});



--------


validate a date


frappe.ui.form.on("Issue", "validate", function(frm) {

       if (frm.doc.closing_date < get_today()) {

           frappe.msgprint(__("You can not select past date in From Date"));

           frappe.validated = false;

       }

   });



----------



Create a new db record.


frappe.ui.form.on("Issue", "before_save", function(frm) {

   frappe.db.insert({

   'doctype': 'Item',

'item_code': 'Test3',

'item_name': 'Test3',

'item_group': 'Consumable',

   })

});


Options

validate

refresh

onload

on_submit

on_cancel

on_update

before_submit

after_save

after_insert

after_delete

before-deleate



SERVER SCRIPT

-------------


frappe.msgprint("running server script")

emp = frappe.db.get_list('Employee')

frappe.msgprint(json.dumps(emp))



---


update data

frappe.db.set_value('Task', 'TASK-2023-00006', 'subject', 'New Subject')


---

insert data


doc = frappe.new_doc('Task')

doc.title = 'New Task 2'

doc.subject='New subject 2'

doc.insert()


-------------



SYSTEM CONSOLE


frappe.msgprint(json.dumps(frappe.db.sql(f"""SELECT  actual_qty FROM `tabBin`  WHERE item_code='Pen' ORDER BY modified DESC LIMIT 1;""")))


SSQL  - SELECT  actual_qty FROM `tabBin`  WHERE item_code='Pen'


-----------

API



if frappe.form_dict.message == "ping":

frappe.response['message'] = "pong"

else:

frappe.response['message'] = "ok"


access it here - sum02.erpgulf.com:1828/api/method/get_stock?message=ping


SERVER and CLIENT TOGETHER - SCRIPTS

--------------


API - get_stock

item_code_var=frappe.form_dict.item_code



stock=frappe.db.sql(f"""SELECT  actual_qty FROM `tabBin`  WHERE item_code='{item_code_var}' ORDER BY modified DESC LIMIT 1;""")


frappe.response["message"]=stock


client script for above


frappe.ui.form.on("Issue", "refresh", function(frm) {

   frm.add_custom_button(__("Do Something"), function() {

       // When this button is clicked, do this


       var subject = frm.doc.subject;

       var event_type = frm.doc.event_type;

       alert(frm.doc.stock_item)

       // do something with these values, like an ajax request

       // or call a server side frappe function using frappe.call

       frappe.call({

           method: "get_stock",

           args: {

               item_code:frm.doc.stock_item

           },

           callback: function(r) {

           console.log(r);

           alert(r["message"][0][0])

           }

       });


   });

});


-----------------------

Thanks.

For any clarification, correction, or feedback please send email to support@ERPGulf.com





Team ERPGulf

The team behind ERPGulf blogs here, expresses their thoughts, shares the experience, often show the frustrations. Contact us on support@ERPGulf.com

1 comment
onie June 14, 2023

Can you please show how to let an input filed to not accept characters other than numbers. While typing, numbers will be accepted, but letters must not be displayed as you type

Add Comment