Automating Date Fields with Scripts

Automating Date Fields with Scripts

Introduction

Manually calculating and entering dates in iVendNext documents is error-prone and time-consuming. With custom scripts, you can automate date fields to:
Auto-set deadlines (e.g., Production Due = Delivery Date - 2 days).
Enforce business rules (e.g., prevent invalid date ranges).
Sync dates across linked documents (e.g., PO → Invoice due dates).


Notes
This guide walks through practical scripting examples to streamline your date management.




When to Automate Date Fields

Automation is ideal for:


  • Project timelines (Task Start/End Dates).

  • Order processing (Ship By → Delivery Date).

  • Financial workflows (Invoice Due = Issue Date + 30 days).




Method 1: Basic Date Calculation

Example Scenario

Set a Production Due Date to always be 2 days before the Delivery Date.


Step-by-Step Script

  1. Ensure Fields Exist:


  • Add delivery_date (Date) and production_due_date (Date) to your DocType.


  1. Add a Client Script:


// Triggered when Delivery Date changes  

cur_frm.cscript.delivery_date = function(doc) {  

  if (doc.delivery_date) {  

    // Calculate 2 days before  

    var dueDate = ivendnext.datetime.add_days(doc.delivery_date, -2);  

    // Update Production Due Date  

    cur_frm.set_value("production_due_date", dueDate);  

  }  

};  


  1. Test:


  • Enter a Delivery Date → Production Due Date auto-updates.




Method 2: Conditional Date Logic

Example Scenario

Set Payment Due Date to:


  • 30 days after invoice date for "Net 30" terms.

  • Immediate for "Cash on Delivery".


Script

cur_frm.cscript.payment_terms = function(doc) {  

  if (doc.payment_terms === "Net 30") {  

    var dueDate = ivendnext.datetime.add_days(doc.posting_date, 30);  

    cur_frm.set_value("due_date", dueDate);  

  } else if (doc.payment_terms === "COD") {  

    cur_frm.set_value("due_date", doc.posting_date); // Same day  

  }  

};  


Key Notes:


  • Uses ivendnext.datetime.add_days() for safe date math.

  • Always validate fields (if (doc.fieldname)).




Method 3: Syncing Dates Across Documents

Example Scenario

When creating a Delivery Note from a Sales Order, auto-set:


  • delivery_date = Sales Order’s ship_by_date.


Solution

  1. Ensure field names match in both DocTypes (ship_by_date).


  • Or use add_fetch for non-matching names:


// In Delivery Note script  

cur_frm.add_fetch("sales_order", "ship_by_date", "delivery_date");  


  1. Test:


  • Create Delivery Note from Sales Order → delivery_date copies automatically.





Advanced Techniques

1. Blocking Invalid Dates

Prevent users from selecting weekends for deadlines:


cur_frm.cscript.delivery_date = function(doc) {  

  var day = ivendnext.datetime.get_day(doc.delivery_date);  

  if (day === 0 || day === 6) { // Sunday (0) or Saturday (6)  

    ivendnext.msgprint("Delivery dates cannot be on weekends!");  

    cur_frm.set_value("delivery_date", ""); // Clear field  

  }  

};  

2. Dynamic Date Ranges

Auto-adjust end_date if start_date changes (e.g., fixed 7-day duration):


cur_frm.cscript.start_date = function(doc) {  

  if (doc.start_date) {  

    cur_frm.set_value("end_date", ivendnext.datetime.add_days(doc.start_date, 7));  

  }  

};  




Troubleshooting

Issue

Solution

Script not running

Check if the script is enabled in Customize Form.

Incorrect dates

Verify timezone settings in ivendnext.datetime.

Permissions

Ensure users have edit rights to date fields.




Best Practices

  1. Document Dependencies: Note which fields trigger date changes.

  2. User Feedback: Use ivendnext.msgprint() to explain auto-updates (e.g., "Due date set to 30 days after invoice").

  3. Test Edge Cases: Validate with blank dates, holidays, and timezone changes.




Conclusion

Automating date fields in iVendNext reduces manual work and ensures consistency. Whether you’re calculating deadlines, enforcing rules, or syncing documents, scripts provide flexible solutions.




Key Takeaways


✅ Use ivendnext.datetime.add_days() for safe date math.
✅ Match field names or use add_fetch to sync dates across docs.
✅ Add validation logic to block invalid entries.




    • Related Articles

    • Automating Recurring Transactions

      Introduction In today’s fast-paced business environment, automating repetitive tasks is essential for improving efficiency and reducing manual errors. Recurring transactions, such as monthly subscriptions, regular billings, or periodic payments, are ...
    • Working with Client Scripts for Enhanced UI

      Overview Client Scripts in iVendNext allow users to customize and extend the functionality of forms dynamically. These JavaScript-based snippets enable real-time modifications—such as hiding buttons, validating fields, or triggering actions—without ...
    • Creating and Managing Custom Fields

      Introduction Custom fields allow businesses to extend standard forms in iVendNext to capture unique data requirements. Whether you need to track additional customer details, add project-specific fields, or customize inventory attributes, creating ...
    • Advanced Reporting - Custom Reports and Scripts

      Introduction While iVendNext provides pre-built operational reports, businesses often need tailored analytics to address unique challenges. This guide explores advanced reporting tools—including custom Report Builder templates, SQL Query Reports, and ...
    • Automating Workflows with Energy Point Rules

      Introduction iVendNext's Energy Point System goes beyond simple gamification - it serves as a powerful workflow automation tool that rewards users for completing critical business processes. By strategically configuring Energy Point Rules, ...