Hi,
I am trying to update the customer credit limit bypass option at the end of each day. I have created a server-side script, but it is not working as expected. Thank you in advance for your help
def uncheck_the_credit_limit():
try:
frappe.log_error(title=“Scheduler Triggered”, message=“Script execution started.”)
query = """SELECT name FROM `tabCustomer Credit Limit` WHERE parenttype = 'Customer' AND bypass_credit_limit_check = 1"""
results = frappe.db.sql(query, as_dict=True)
if not results:
frappe.log_error(title="Query Result", message="No records found where credit limit bypass is enabled.")
return
# Loop through each record
for row in results:
customer_credit_limit = frappe.get_doc("Customer Credit Limit", row['name'])
customer_credit_limit.reload()
parent = customer_credit_limit.parent
parenttype = customer_credit_limit.parenttype
# Update the fields
customer_credit_limit.modified = frappe.utils.now()
customer_credit_limit.modified_by = "Administrator"
customer_credit_limit.bypass_credit_limit_check = 0
customer_credit_limit.parent = parent
customer_credit_limit.parenttype = parenttype
# Save the changes (update the record)
customer_credit_limit.save(ignore_version=True)
# Commit changes to the database
frappe.db.commit()
# Log successful update
frappe.log_error(title="Customer Credit Limit Update", message="Credit limit bypass disabled for relevant customers.")
except Exception as e:
# Log any error encountered during execution
frappe.log_error(title="General Error", message=f"Error during script execution: {str(e)}")
Execute the function
uncheck_the_credit_limit()