ERPNext Conference 2019* ERPNext.com Blog

Set day name in a field from a selected date

customization
erpnext

#1

Hi,

I have 2 fields in a DocType (CallData) as below:

call_date (field type: datetime)
call_day (field type: data)

When I select the date from the call_date field, I want the call_day field to be auto-populated with the day name for that selected date.

For eg.: If in the call_date field I select, 10-Sep-19 then the call_day field should be auto-populated to Tuesday

I have tried the following custom script but it does not work

frappe.ui.form.on("CallData", "call_date", function(frm) {
frm.set_value("call_day", frappe.utils.get_datetime(frm.doc.call_date).strftime("%a"));
});

Any help will be appreciated


#2

@shashank_shirke, try this

var a = new Date(cur_frm.doc.call_date);
var weekdays=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
cur_frm.set_value("call_day",weekdays[a.getDay()])

#3

Hi,

Thanks for your reply. Your script is working perfectly. I just had to modify it a bit as below:

frappe.ui.form.on("Trade", "call_date", function(frm) {
   var a = new Date(frm.doc.entry_date_time);
   var weekdays=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
   frm.set_value("call_day",weekdays[a.getDay()])
});

#4

Hi @ROHAN_JAIN1,

Further to this can you please help me out with the following:

Let’s say I have a 4th field called “session_name”;

Now depending on the time selected in the field “call_date” can we set the value for “session_name” for the following conditions;

If time in “call_date” is between 8:00 AM to 10:00 AM then set value of “session_name” field as “First Session

If time in “call_date” is between 11:00 AM to 2:00 PM then set value of “session_name” field as “Second Session

If time in “call_date” is between 3:00 PM to 5:00 PM then set value of “session_name” field as “Third Session


#5
var a = new Date(frm.doc.entry_date_time);
var hour = a.getHours();

if( hour >= 8 && hour <= 10 ){
frm.set_value("session_name","First Session")
} else if ( hour >= 11 && hour <= 14 ){
frm.set_value("session_name","Second Session")
} else if ( hour >= 15 && hour <= 17 ){
frm.set_value("session_name","Third Session")
}

#6

Hi @ROHAN_JAIN1,

This doesn’t seem to work. Tried multiple times but still not working. :frowning:


#7

What error you are getting,can you share console.log or any error log’s here?


#8

@ROHAN_JAIN1

There are no errors we are getting in the browser’s console log. Its just that the sessions field seems unaffected.

Also 2 things to note;

  1. The “entry_date_time” and “exit_date_time” fields are set as datetime field types.
  2. For the 1st if condition, we want to set the hour condition as between 08:30am to 10:30am

#9

@shashank_shirke

var a = new Date(cur_frm.doc.start_on);

if( (a.getHours()>= 8 && a.getMinutes() >= 30) && (a.getHours()<= 10 && a.getMinutes() <= 30) ){
	cur_frm.set_value("session","First Session")
} else if ( a.getHours()>= 11 && a.getHours()<= 14 ){
	cur_frm.set_value("session","Second Session")
} else if ( a.getHours()>= 15 && a.getHours()<= 17  ){
	cur_frm.set_value("session","Third Session")
}

#10

@ROHAN_JAIN1

This is working correctly only for getHours() method i.e. for e.g. between 11am to 2pm (below code IS working)

if (a.getHours() >= 11 && a.getHours() <= 14) {
frm.set_value("session","Second Session"); 
}

But its not working for hours and minutes i.e. for e.g. 8:30am to 10:30am (below code IS NOT working)

if( (a.getHours()>= 8 && a.getMinutes() >= 30) && (a.getHours()<= 10 && a.getMinutes() <= 30) ){
frm.set_value("session","First Session")

#11

Screenshot%20from%202019-09-19%2014-33-29

Above code Screenshot.


#12

My bad! I had a typo in the field name.

Its working correctly now :smile:

@ROHAN_JAIN1 thanks a ton man! :bowing_man:


#13

@ROHAN_JAIN1

After testing a bit, there seems a bug if we set different minutes parameters.

For eg.: For 8:30am to 10:45am

if( (a.getHours()>= 8 && a.getMinutes() >= 30) && (a.getHours()<= 10 && a.getMinutes() <= 45) ){
frm.set_value("session","First Session")

This use case seems to create an issue.

The getMinutes() seems to be independent of the getHours() method and so sliding the hours and minutes handles seem to generate mixed results.