Skip to navigation

Writing code scripts for access provisioning

View as Markdown

When an entitlement uses Run code script, the AccessOps Manager coworker runs your script to grant access when a request is approved. If the de-provisioning action is the same script, it also runs it to remove access when the access is revoked or expires. This article explains what your script receives, how to write it, and how to test it.

To set the method on an entitlement, see Provisioning methods for entitlements.

What the script receives

For these runs, Atomicwork passes your script one extra input: input.iga_context. Atomicwork builds it from the access ticket that is being fulfilled, so neither the coworker nor the requester can change it.

FieldWhat it holds
operationPROVISION to grant access. DEPROVISION to remove it, after a revoke or when the access expires.
request_idThe ticket being fulfilled, for example SER-1306: the access request on a grant, the revocation ticket on a revoke.
access_request_idThe original access request. On a grant it’s the same as request_id. On a revoke it’s the request that granted the access, or null if that request is no longer available.
grant_idThe grant being changed. null on a grant, because the grant doesn’t exist yet. Set on a revoke.
entitlement_id, app_idThe entitlement and its application in Access management.
subject_userThe person who gets or loses the access: id, email and name.
requesterThe ticket’s requester: id, email and name. On a grant this is normally the person getting the access, also when someone else asked for them. On a revoke it’s whoever started the revocation, for example the admin who revoked the access. Unlike subject_user, its email can be empty.
request_fieldsThe request’s form values, with your data masking policies applied. custom_fields holds the ticket’s custom fields. On a grant, item_fields holds the answers on the requested item, such as the entitlement’s Collect information questions. Empty ({}) when there are none.

For example:

{
"operation": "PROVISION",
"request_id": "SER-1306",
"access_request_id": "SER-1306",
"grant_id": null,
"entitlement_id": 164751,
"app_id": 42584,
"subject_user": { "id": 133854, "email": "alex@example.com", "name": "Alex Kim" },
"requester": { "id": 133854, "email": "alex@example.com", "name": "Alex Kim" },
"request_fields": {}
}

iga_context doesn’t include the user’s manager, department or other profile details. If your script needs them, look the user up by subject_user.id.

Write the script

Follow these rules so the script does the right thing for every request:

  • Take the user from subject_user. Use input.iga_context.subject_user (usually its email) as the account to change. Don’t add your own input for who the user is. For anything else the requester needs to tell you, add a Collect information question to the entitlement and read the answer from request_fields.item_fields.
  • Handle both operations. Check operation and grant or remove the access. When De-provisioning action is same as the provisioning action is selected, the same script runs for both.
  • Make a second run safe. A failed run isn’t retried on its own, but a person or a later request can run the script again. Treat “already has access” and “already removed” as success.
  • Check the result. Before the script ends, read the app back to confirm the access was added or removed. If the app can’t be reached, executeHttpRequest returns a status_code such as 404 with a body that starts with Error occurred while calling URL. Treat that as a failure, not as “not found”.
  • Throw an error when something fails. An error tells Atomicwork that the change didn’t complete, so nothing is recorded and a person checks the app. It doesn’t prove that nothing changed, so say in the error what was done. Ending normally means success.
  • Keep secrets out of the code. Use saved credentials. See Use saved credentials in code.
  • Plan for the coworker’s permissions. The script runs as the AccessOps Manager coworker, not as you. A test from the Code library uses your permissions, so it can pass while the real run fails. For example, reading or changing custom objects needs custom-object management permission, which the coworker doesn’t have by default. Prefer the app’s own API with a saved credential, or give the coworker the permission it needs.

Here is a script that grants and removes a role in an app’s API:

// Grants or revokes the Editors role in Example App for the person in an access request.
async function main() {
// Step 1: Fixed settings.
const API_BASE = "https://api.example.com/v1";
const ROLE_ID = "editors";
const CREDENTIAL = "example-app"; // a saved credential that allows api.example.com
// Step 2: Read the access context that Atomicwork adds.
let ctx = input.iga_context;
if (typeof ctx === "string") ctx = JSON.parse(ctx);
if (!ctx || !ctx.subject_user || !ctx.subject_user.email) {
throw new Error("No iga_context. Run this script from an access request, or pass a sample to test it.");
}
const email = ctx.subject_user.email;
const memberPath = "/roles/" + ROLE_ID + "/members/" + encodeURIComponent(email);
// Step 3: Call the app with the saved credential. Returns the app's HTTP status.
async function call(method, path) {
const res = await MCP.Atomicwork.executeHttpRequest({
url: API_BASE + path,
method: method,
headers: [{ key: "Authorization", value: aw.secretHeader(CREDENTIAL, { prefix: "Bearer " }) }],
});
if (!res || !res.success || !res.data || typeof res.data !== "object") {
throw new Error("Atomicwork could not send the request: " + JSON.stringify((res && (res.error || res.data)) || res));
}
const body = typeof res.data.body === "string" ? res.data.body : JSON.stringify(res.data.body || "");
if (body.indexOf("Error occurred while calling URL") === 0) {
throw new Error("Example App did not answer: " + body); // a network failure, not a 404
}
return Number(res.data.status_code);
}
// Step 4: Grant, then check.
if (ctx.operation === "PROVISION") {
const added = await call("PUT", memberPath);
if (added !== 200 && added !== 201 && added !== 204) {
throw new Error("Example App did not add " + email + ": HTTP " + added);
}
if ((await call("GET", memberPath)) !== 200) {
throw new Error("Example App accepted the add, but " + email + " is not in " + ROLE_ID);
}
return { result: "GRANTED", user: email, role: ROLE_ID, ticket: ctx.request_id };
}
// Step 5: Revoke, then check. A 404 from Example App means the access was already removed.
if (ctx.operation === "DEPROVISION") {
const removed = await call("DELETE", memberPath);
if (removed !== 200 && removed !== 204 && removed !== 404) {
throw new Error("Example App did not remove " + email + ": HTTP " + removed);
}
if ((await call("GET", memberPath)) !== 404) {
throw new Error("Example App accepted the removal, but " + email + " is still in " + ROLE_ID);
}
return { result: "REVOKED", user: email, role: ROLE_ID, ticket: ctx.request_id };
}
throw new Error("Unknown operation: " + ctx.operation);
}
// --- DO NOT MODIFY: EXECUTION ---
return await main();
// --- END EXECUTION ---

You can also ask AI Assist to write the script, for example: “Write a code script for the Editor entitlement of Example App. It should add the user to the Editors role through the API and remove them on revoke.” AI Assist knows what iga_context holds and writes the script to use it.

Test the script

1

Open the test panel

Go to AI Workforce > AI Registry > Code library, open the script and open the test panel. When the script reads input.iga_context, the Test data panel shows an iga_context field.

2

Paste a grant sample

Copy the example from What the script receives, change subject_user to a test account, and paste it into the iga_context field. You can also ask AI Assist for a sample with your test account.

3

Run the test

Run the test and check the output. The test is a real run: the script changes the app. The panel sends the sample as text, so the script must turn a string into an object first, as the example script does.

4

Test the revoke

In the sample, change operation to DEPROVISION, set request_id to another ticket such as SER-1307, and set grant_id to a number, for example 1. Keep access_request_id and subject_user as they are. Run the test again and check that the access is removed.

After the script works, publish it, set it on the entitlement, and raise a real access request for a test user to check the whole flow. Don’t skip this: this end-to-end test is the only one that runs with the coworker’s permissions. Then revoke the access as an admin and check that the revoke run gets the same subject_user.

What happens after the script runs

ResultGrant (PROVISION)Revoke (DEPROVISION)
The script ends normallyThe grant is recorded right away. If recording it fails, it’s recorded when the ticket is resolved.The access is removed in the app. The grant shows as revoked (or expired, after an expiry) when the revocation ticket is resolved.
The script throws an errorNo grant is recorded. A person checks the app: if the access is there, resolve the ticket and the grant is recorded; if it isn’t, cancel the ticket.The grant stays active. A person checks the app: if the access is gone, resolve the ticket; if it isn’t, cancel the ticket.

Good to know

  • Only access requests fill in iga_context. When you run the script from the test panel, a workflow or a coworker, the value comes from whoever runs it. Use access scripts only for access provisioning.
  • The script must be published and available to coworkers before you can pick it for an entitlement.