What I have done,
Whenever a user enters the lead, I create a default event for that day,
So it has two piece of code
Client-Script:
frappe.ui.form.on("Lead", {
after_save: function (frm) {
// Triggered after the Lead form is saved
// Get creation and modification timestamps
const creation_time = frm.doc.creation
? frm.doc.creation.replace("T", " ").replace("Z", "")
: null;
const modified_time = frm.doc.modified
? frm.doc.modified.replace("T", " ").replace("Z", "")
: null;
// Check if the lead is new (not yet modified)
if (creation_time && modified_time && creation_time === modified_time) {
console.log("New Lead saved. Initiating event creation process.");
const lead_name = frm.doc.name;
const sales_person = frm.doc.sales_person;
const lead_owner = frm.doc.lead_owner;
// Call the backend API to create an event for the new lead
frappe.call({
method: "phillip_custom.online_lead_to_lead.create_event_for_lead",
args: {
lead_name: lead_name,
sales_person: sales_person,
lead_owner: lead_owner,
is_import:false,
},
callback: function (response) {
if (response.message) {
frappe.msgprint(
"Today's Follow-up created successfully for the lead."
);
// window.location.reload();
} else {
frappe.msgprint(
"Failed to create event for the lead. Please try again.",
"Error"
);
}
},
});
} else {
console.log("Existing Lead saved. Skipping event creation process.");
}
},
});
Server side:
def create_event_for_lead(lead_name, sales_person,lead_owner,is_import):
try:
today = getdate()
event = frappe.new_doc("Event")
event.subject = f"Follow-up Meeting for Lead {lead_name}"
event.starts_on = today
event.sales_person = sales_person # Set the salesperson as the owner of the event
event.owner = lead_owner # Set the salesperson as the owner of the event
if(is_import):
frappe.set_user(lead_owner)
event.created_by = lead_owner
event.modified_by =lead_owner
event.insert(ignore_permissions=True)
# Associate the lead with the event
event_participants = frappe.new_doc("Event Participants")
event_participants.created_by = lead_owner
event_participants.modified_by = lead_owner
event_participants.reference_doctype = "Lead"
event_participants.reference_docname = lead_name
event_participants.parent = event.name
event_participants.parentfield = "event_participants"
event_participants.parenttype = "Event"
event_participants.insert(ignore_permissions=True)
return event
Also tried commenting this code
if(is_import):
frappe.set_user(lead_owner
But whenever user enters the lead and save it lead the lead gets saved but he faces below issue.and also gets loggout