FactoryFour Learn Center Index

Last updated June 25, 2019


Set Datapoint from Task Form

Use Task Forms and Standards to enforce a datapoint entry.


Description:

When you want to leverage Task forms and Standards to enforce entry of a datapoint such as a Tracking Number that can be leveraged by notifications

How to use:

  1. Create a Key Metric to represent the piece of information you want to set as a Datapoint
  2. Create a Task Form and designate one of the fields the above Key Metric
  3. Create a Task Form Custom Rule and add the below code snippet into the custom action
  4. Specify which Datapoint (Name and Code) you want created as well as reference the Metric, Name and Code, you created within the code snippet

    Note: you do not have to create the datapoint in the admin app, this rule will create it on the fly

Snippet:

const axios = require('axios');

const METRIC_CODE = 'tracking_#';
const DATAPOINT_CODE = 'tracking_number';
const DATAPOINT_TITLE = 'Tracking Number';

module.exports = function({ headers, body }, cb) {
    const metrics = body.formData.metrics.filter(m => m.code === METRIC_CODE);
    if (metrics.length === 0 || !headers['x-factoryfour-token']) {
        return cb(null, {});
    }
    const value = metrics[0].value;
    const token = headers['x-factoryfour-token'];

    const data = {
        parent: `f4::order::${body.orders[0].id}`,
        content: {
            value: `${value}`,
            title: DATAPOINT_TITLE,
        },
        format: 'string',
        type: DATAPOINT_CODE,
        permissions: body.parentWorkflows[0].permissions,
    };

    return axios({
        url: 'https://api.factoryfour.com/datapoints/',
        method: 'POST',
        headers: {
            Authorization: `Bearer ${token}`,
        },
        data,
    })
        .then(() => {
            return cb(null, {});
        })
        .catch(err => {
            console.log(err);
            return cb(err);
        });
};

Search the FactoryFour Learn Center


FactoryFour Learn Center