Automating Date Fields with Scripts

Automating Date Fields with Scripts

Overview

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).


This article 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

Here’s a quick look at some common issues you might run into.


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

Here’s a quick look at some of the best practices for Date Management.


  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.




    • 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 ...
    • Editing Fields After Submission and Changing Data Types

      Overview Efficient data entry and clear document identification are critical for productivity in iVendNext. This guide covers two powerful features: Setting default values to automate repetitive inputs. Creating multi-field document titles for better ...
    • 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

      Overview Custom fields in iVendNext let you tailor forms to fit your specific data needs—like extra customer info, project tags, or inventory tweaks. They help your system match your workflow, not the other way around. This guide walks you through ...
    • Advanced Reporting - Custom Reports and Scripts

      Overview This article gives you an overview of advanced reporting in iVendNext, highlighting tools like custom templates, SQL queries, and Python scripts for deeper, tailored insights. When to Use Advanced Reporting Use these tools when you need: ✔ ...