Hi everyone, I have a query about session defaults. I want the system to pop up the session defaults form every time the user logs in. How can I do this?
Reference :
1 Like
@Meet please check
$(document).on('app_ready', function() {
// Function to get a cookie by name
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
// Function to set a cookie
function setCookie(name, value, days) {
let expires = "";
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
// Function to get a session storage item
function getSessionItem(name) {
return sessionStorage.getItem(name);
}
// Function to set a session storage item
function setSessionItem(name, value) {
sessionStorage.setItem(name, value);
}
// Get the current user
let currentUser = frappe.session.user;
// Check if the dialog has already been shown for the current user in this session
if (!getSessionItem('dialog_shown_' + currentUser)) {
let fields = [];
frappe.call({
method: "frappe.core.doctype.session_default_settings.session_default_settings.get_session_default_values",
callback: function (data) {
fields = JSON.parse(data.message);
let perms = frappe.perm.get_perm("Session Default Settings");
// Add settings button only if user is a System Manager or has permission on 'Session Default Settings'
if (frappe.user_roles.includes("System Manager") || perms[0].read == 1) {
fields.push({
fieldname: "settings",
fieldtype: "Button",
label: __("Settings"),
click: () => {
frappe.set_route(
"Form",
"Session Default Settings",
"Session Default Settings"
);
},
});
}
// Create a dialog
let d = new frappe.ui.Dialog({
title: __("Session Defaults"),
fields: fields,
primary_action_label: __("Save"),
primary_action(values) {
// If default is not set for a particular field in prompt
fields.forEach(function (d) {
if (!values[d.fieldname]) {
values[d.fieldname] = "";
}
});
frappe.call({
method: "frappe.core.doctype.session_default_settings.session_default_settings.set_session_default_values",
args: {
default_values: values,
},
callback: function (data) {
if (data.message == "success") {
frappe.show_alert({
message: __("Session Defaults Saved"),
indicator: "green",
});
frappe.ui.toolbar.clear_cache();
} else {
frappe.show_alert({
message: __("An error occurred while setting Session Defaults"),
indicator: "red",
});
}
d.hide();
},
});
}
});
// Show the dialog
d.show();
// Set the flag in session storage to prevent showing the dialog again for this user in the same session
setSessionItem('dialog_shown_' + currentUser, 'true');
},
});
}
});
// Clear the session storage item on logout
$(document).on('frappe.ui.toolbar.clear_cache', function() {
let currentUser = frappe.session.user;
sessionStorage.removeItem('dialog_shown_' + currentUser);
});
If your code is not working then also use this reference :
1 Like
clear cache and check it