How To
ForgeRock Identity Platform
Does not apply to Identity Cloud

How do I write to a file using JavaScript on a custom endpoint in IDM (All versions)?

Last updated Jan 12, 2023

The purpose of this article is to provide examples of writing to a local file using JavaScript® on a custom endpoint in IDM. This article assumes you have already created your custom endpoint.

1 reader recommends this article


IDM uses Mozilla® Rhino, which is an embedded Java® implementation of a JavaScript engine. This implementation allows you to execute JavaScript used by endpoints, inline code etc in IDM.

Writing to a file using JavaScript examples

Here are two examples of how you can write to a file using JavaScript by calling Java from Rhino:

// Verify that the file exists and if not, create it var fileObj = new"/path/to/myfile/file.txt"); if (!fileObj.exists()) { fileObj.createNewFile(); } // Create a filewriter object, then append a line to the file and close the file var fileWriter = new"/path/to/myfile/file.txt", true); // true = append var logData = "Test\n"; fileWriter.write(logData); fileWriter.close();

Or a more efficient version:

var content = new java.lang.String("some string" + java.lang.System.lineSeparator()); java.nio.file.Files.write( java.nio.file.Paths.get("logs/mylog.log"), content.getBytes(), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.APPEND );

See Also

How do I load JavaScript functions into IDM (All versions)?

How do I add logging to JavaScript files in IDM (All versions)?

FAQ: Scripts in IDM

Create Custom Endpoints to Launch Scripts

Create a Custom Endpoint

Connector reference

Rhino documentation

Related Issue Tracker IDs


Copyright and Trademarks Copyright © 2023 ForgeRock, all rights reserved.