Clear Cache only on clicking new for a doctype

I want to clear cache not on opening existing docs but only when clicking on New.
How to achieve ?

Hi,

What is the use case for this?

You can bind the New button’s click event to this function:

frappe.ui.toolbar.clear_cache()
1 Like

yeah thats what i’m trying to do but not able to clear cache on new’s click.
how to bind the new’s click event ?

$('.primary-action:contains("New")').click(function() {
    frappe.ui.toolbar.clear_cache();
})

You can add a console.log before call to frappe.ui.toolbar.clear_cache to verify that this works well.

If you don’t want the page to reload:

$('.primary-action:contains("New")').click(function() {
   frappe.assets.clear_local_storage();
frappe.call({
	method: 'frappe.sessions.clear',
	callback: function(r) {
		if(!r.exc) {
			frappe.show_alert({message:r.message, indicator:'green'});
			// location.reload(true);
		}
	}
})
})

EDIT:
You’ll need to add this code to your custom app’s public/js directory and ensure app_include_js hook includes the file.

but i don’t have access to backend

I’d be able to help you better if you answer this.

I am rendering a HTML field for showing templates based on selection. Once filling all fields and saving, I again click on NEW but the previous rendered template is showing by default (which should be displayed only if I choose category).
One more thing I want is I want my rendered field to be saved permanently but because of cache issue, the latest rendered is shown on rest of docs.

You can reset the field during the onload event if category is not selected.

EDIT:
If you want the data to be saved permanently, ensure you’re using the fields created using Customize Form.

frappe.ui.form.on("Schedule","onload",function(frm){
if(!frm.doc.category)
{
frappe.ui.toolbar.clear_cache();
console.log(12)
}
});

Its clearing cache everytime because by default that field is empty and on every page loading, it again is clearing cache so its a loop. How to make it clear ache just for one time.

and yeah, m using html field, so having trouble pushing the rendered html data as permanent

Use the following code code instead of frappe.ui.toolbar.clear_cache();

frappe.assets.clear_local_storage();
frappe.call({
	method: 'frappe.sessions.clear',
	callback: function(r) {
		if(!r.exc) {
			frappe.show_alert({message:r.message, indicator:'green'});
			// location.reload(true);
		}
	}
})

A better way might be:

frm.set_value('your_html_field', '');
1 Like
var headhtml=email_preview;
$("[data-fieldname='preview']").html(headhtml);

i’m using this to show the html field.
email_preview contains the html code (from standard reply ). I want to save the complete showing html along with data on a permanent basis. Just came to know that on clearing cache, that html is not getting displayed. ANy alternate ??

HTML field data isn’t saved to the DB.
You can try using Text/Text Editor fieldtype instead.

Alternatively, you’ll want to create a hidden field to save the relevant data and render it in the HTML Field.

Ok thanks