IdentitySync Custom Scripts

Skip to end of metadata
Go to start of metadata

Overview

IdentitySync offers you the option of writing your own custom scripts to use in a dataflow. A custom JavaScript step receives a user record as an input, manipulates it, and produces the record as an output. The ability to execute self-written scripts gives you extensive flexibility to maximize IdentitySync to suit your needs.

Including a Custom Script in Your Dataflow

Custom scripts are included in a dataflow by adding a record.evaluate step to your data flow. For more information on creating a data flow and customizing it, see IdentitySync Implementation Flow.

 Click for a full dataflow example that includes a records.evaluate custom step
{
 "name": "append subscription to subscriptions array", // This flow appends a new subscription to an existing data.subscriptions array.
 "description": "account > transform > write",
 "steps": [
  {
   "id": "account",
   "type": "datasource.read.gigya.account",
   "params": {
    "select": "UID,data.subscriptions",
    "from": "accounts",
    "where": "data.subscriptions is not null",
    "deltaField": "lastUpdatedTimestamp",
    "maxConcurrency": 1,
    "excludeEmptyFields": false
   },
   "next": [
    "transform"
   ]
  },
  {
   "id": "transform",
   "type": "record.evaluate",
   "params": {
    "script": "ZnVuY3Rpb24gcHJvY2VzcyhyZWNvcmQsIGN0eCwgbG9nZ2VyKSB7DQoNCiAgICBpZiAocmVjb3JkICE9PSBudWxsKSB7DQogICAgICAgIGlmIChyZWNvcmQuZGF0YSAhPT0gbnVsbCkgew0KICAgICAgICAgICAgdmFyIHN1YnNjcmlwdGlvbiA9IHt9Ow0KICAgICAgICAgICAgc3Vic2NyaXB0aW9uLmFjY2VwdGFuY2VEYXRlID0gIjIwMTctMDQtMjhUMDI6MDQ6MzEuMzEzWiI7DQogICAgICAgICAgICBzdWJzY3JpcHRpb24ubGlzdElEID0gImxpc3QwNCI7DQogICAgICAgICAgICBzdWJzY3JpcHRpb24uc3Vic2NyaXB0aW9uU291cmNlQXBwbGljYXRpb24gPSAibmV3QXBwNCI7DQogICAgICAgICAgICBzdWJzY3JpcHRpb24uYWN0aXZlID0gdHJ1ZTsNCg0KICAgICAgICAgICAgdmFyIGxvZyA9IHt9Ow0KICAgICAgICAgICAgbG9nLnNvdXJjZUFwcGxpY2F0aW9uID0gInNvdXJjZUFwcDQiOw0KICAgICAgICAgICAgbG9nLmFjdGlvbkRhdGUgPSAiMjAxMC0xMS0yOVQwMjowNDozMS4zMTNaIjsNCiAgICAgICAgICAgIGxvZy5hY3Rpb24gPSAiYWNjZXB0YW5jZSI7DQogICAgICAgICAgICBzdWJzY3JpcHRpb24ubG9nID0gbG9nOw0KDQogICAgICAgICAgICByZWNvcmQuZGF0YS5zdWJzY3JpcHRpb25zLmFkZChzdWJzY3JpcHRpb24pOw0KICAgICAgICB9DQogICAgICAgIHJldHVybiByZWNvcmQ7DQogICAgfQ0KfQ=="
   },
   "next": [
    "write"
   ]
  },
  {
   "id": "write",
   "type": "datasource.write.gigya.account",
   "params": {
    "maxConnections": 10
   }
  }
 ]
}

 

Interface

Custom scripts must use the following interface:

public interface Step {
    void process(Object item, Context context, Logger logger, Consumer next);
}

Parameters

  • item: the record to process.
  • context: the Context object supplying execution contextual information.
  • (Optional) logger: the Logger object. Log entries are written by default to the remote JobStatus traces attribute.
  • (Optional) next: the next step, for further processing the record. Use this parameter only when the script produces a large number of records. In this case, the records need to be fed one by one to the next step in the dataflow and the next parameter is used. 


Related Interfaces

public interface Logger {

	void debug(String sender, String message, String data); // In debug mode, use 'data' to add additional record data to the logger.

	void info(String sender, String message);
 
	void error(String sender, String message);
}
public interface Context {

    Object getSessionParameter(String name);

    void setSessionParameter(String name, Object parameter);

    File getWorkingDirectory();
}

Examples

Following are examples for custom scripts that can be used as dataflow steps.


Input

These examples assume that the following is received as input, included in the data.subscriptions field: 

Input
{
	"data": {
		"subscriptions": [{
				"listID": "CA_SUBSCRIPTION_01",
				"acceptanceDate": "2010-11-25T20:42:15.280Z",
				"subscriptionSourceApplication": "123",
				"active": true,
				"log": [{
						"sourceApplication": "123",
						"actionDate": "2010-11-25T20:42:15.280Z",
						"action": "acceptance"
					}
				]
			}
		]
	}
}


Append to Array

The following example demonstrates appending a new JSON object (new subscription) to the existing subscription data. 

The code: 

Custom Step
function process(record, ctx, logger) {

    if (record !== null) {
        if (record.data !== null) {
            var subscription = {};
            subscription.acceptanceDate = "2017-04-28T02:04:31.313Z";
            subscription.listID = "list04";
            subscription.subscriptionSourceApplication = "newApp4";
            subscription.active = true;

            var log = {};
            log.sourceApplication = "sourceApp4";
            log.actionDate = "2010-11-29T02:04:31.313Z";
            log.action = "acceptance";
            subscription.log = log;

            record.data.subscriptions.add(subscription);
        }
        return record;
    }
}

 

The eventual output would be, for example: 

Output
[{
		"data": {
			"subscriptions": [{
					"acceptanceDate": "2010-11-25T20:42:15.280Z",
					"listID": "CA_SUBSCRIPTION_01",
					"subscriptionSourceApplication": "123",
					"log": [{
							"action": "acceptance",
							"actionDate": "2010-11-25T20:42:15.280Z",
							"sourceApplication": "123"
						}
					],
					"active": true
				}, {
					"acceptanceDate": "2017-04-28T02:04:31.313Z",
					"listID": "list02",
					"subscriptionSourceApplication": "newApp",
					"active": "true",
					"log": {
						"sourceApplication": "sourceApp",
						"actionDate": "2010-11-29T02:04:31.313Z",
						"action": "acceptance"
					}
				}
			]
		}
	}
]


Remove from Array

The following example demonstrates removing a JSON object (existing subscription) from the subscription data. 

Custom Step
function process(record, ctx, logger) {

    if (record !== null) {
        if (record.data !== null && record.data.subscriptions !== null) {
            var subscriptions = record.data.subscriptions;
            for (var index = subscriptions.length; index--;) {
                var subscription = subscriptions[index];
                if (subscription.acceptanceDate !== null && subscription.acceptanceDate === "2010-11-25T20:42:15.280Z") {
                    subscriptions.remove(index);
                }
            }
            record.data.subscriptions = subscriptions;
        }
        return record;
    }
}

 

The output would look like this: 

 

Output
[{
		"data": {
			"subscriptions": []
		}
	}
] 



Set Value

The following example demonstrates setting a value to one of the fields within the JSON object (subscription status). 

Custom Step
function process(record, ctx, logger) {

    if (record !== null) {
        if (record.data !== null && record.data.subscriptions !== null) {
            var subscriptions = record.data.subscriptions;
            for (var subscriptionIndex in subscriptions) {
                var subscription = subscriptions[subscriptionIndex];
                if (subscription.acceptanceDate !== null && subscription.acceptanceDate === "2010-11-25T20:42:15.280Z") {
                    subscription.active = false;
                }
            }
        }
        return record;
    }
}

 

In the output, you can see that the value of active has changed to "false": 

Output
 [{
		"data": {
			"subscriptions": [{
					"acceptanceDate": "2010-11-25T20:42:15.280Z",
					"listID": "CA_SUBSCRIPTION_01",
					"subscriptionSourceApplication": "123",
					"log": [{
							"action": "acceptance",
							"actionDate": "2010-11-25T20:42:15.280Z",
							"sourceApplication": "123"
						}
					],
					"active": false
				}
			]
		}
	}
]

 

Writing Logs

Custom script logs are built of the script name and the custom message. 

logger.info("myScript", "My custom message");
  • No labels