Writing code scripts for access provisioning
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.
For example:
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. Useinput.iga_context.subject_user(usually itsemail) 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 fromrequest_fields.item_fields. - Handle both operations. Check
operationand 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,
executeHttpRequestreturns astatus_codesuch as 404 with abodythat starts withError 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:
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
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.
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.
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
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.
