FactoryFour Learn Center Index

Last updated March 24, 2019


Set Due Date from Requested Date in the form

Set Due Date from Requested Date in the form with a lead time.


Description:

When you want to set Due Date automatically from the Order Form you need to do it through the Requested Date. Since the Order Form can be extended to an external party, but only the internal organization has control over Due Date, you must specify a ‘Requested Date’ through the form and use a code snippet to set a ‘Due Date’ off of the ‘Requested Date’

How to Use:

  1. Create a form field with a Date
  2. Designate that Date as the ‘Requested Date’ using the ‘+ Add Extractions’ button in the Form Builder
  3. Create a Custom Order Status Rule where the Scope is triggered when an Order moves from ‘Incomplete’ to ‘Pending’ state, a.k.a when the order is submitted
  4. For the rule action, use the below snippet
  5. Make sure you have loaded axios

  6. In the below part of the snippet, replace N with the number of days you want the ‘Due Date’ to be out from ‘Requested’

    due: new Date(+(new Date(neededDate)) + (N) * 86400000).toISOString()

    Note: If you want Due Date= Requested Date, put 0, if you want a week lead time, put 7…

  7. Make sure you save the code in the editor, and turn the rule on

Snippet:

const axios = require('axios');
    module.exports = function(context, cb) {

        const neededDate = context.body.order.dates.needed;

        if (neededDate) {
            const orderUpdate = {
                dates: {
                due: new Date(+(new Date(neededDate)) + (N) * 86400000).toISOString(),
                }
            };

            const token = context.headers['x-factoryfour-token'];
            const url = `https://api.factoryfour.com/orders/${context.body.order.id}`;
            axios({
                method: 'PUT',
                url,
                headers: {
                    Authorization: `Bearer ${token}`,
                },
                data: orderUpdate,
            })
            .then(response => console.log(JSON.stringify({response})))
            .catch(err => console.log(JSON.stringify({err})));
        }

        cb(null, 'ok');
};

Search the FactoryFour Learn Center


FactoryFour Learn Center