Skip to main content

HelloID

Add, edit, or remove a product action

For more information, see Product actions.

Note

In this example, we use the Google Chrome product shown in the Add a product article, and the Weather report form shown in the Add a dynamic form article.

  1. Go to Self Service > Products and click Edit for the relevant product.

    For this example, we'll edit our Google Chrome product.

  2. Go to the Actions - New tab.

  3. In the States pane, select the state of the Product request lifecycle during which this action will run.

    For this example, we'll select the Approved state.

    SelfService_Products_ActionsNew_Approved.jpg
  4. Enter a Name for the action.

    For this example, we'll call it Product task: Google Chrome: Approved.

    SelfService_Products_ActionNew_Approved_Chrome.jpg
  5. Click Add product action.

    The code editor is shown.

    ProductAction_CodeEditor.jpg
  6. There are two ways to start writing the script:

  7. In this example we are starting with the default script.

    ProductAction_SetDefaultScript.jpg
    ProductAction_DefaultScript.jpg

    Tip

    To start over at any time, delete all text from the script field. The Set Default Script button reappears.

  8. Customize the script according to what should happen during the selected stage of the product lifecycle for this product. For example, make API calls into external systems based on the data available in the product, the request, etc.

    Keep the following in mind when writing code for a product action:

    For this example, we will query a weather API. We'll use the following script:

    #######################################################################
    # Template: HelloID SA PowerShell product action
    # Name: Google Chrome: Approved
    # Date: 8.7.2023
    #######################################################################
    
    #region init
    
    $VerbosePreference = "SilentlyContinue"
    $InformationPreference = "Continue"
    $WarningPreference = "Continue"
    
    # global variables (Automation --> Variable library):
    $globalVarExample = $companyName
    
    # product variables:
    $requesterUsernameExample = $requester.userName
    $requesterUserGUIDExample = $requester.userGUID
    $productNameExample = $product.name
    
    # variables configured in form:
    $formVarExample = $form.tempUnitDropdown.tempUnitValue
    
    #endregion init
    
    #region functions
    
    Function Get-Current-NYC-Weather {
        $locationEndpoint = "https://api.open-meteo.com/v1/forecast?latitude=40.71&longitude=-74.01&hourly=temperature_2m&temperature_unit=$formVarExample&windspeed_unit=mph&precipitation_unit=inch"
        $response = Invoke-RestMethod -uri $locationEndpoint
        Write-Information "Response: $response"
        return @{weather = "Current time: $($response.hourly.time[0]); current temperature: $($response.hourly.temperature_2m[0]) degrees $formVarExample"}
    }
    
    #endregion functions
    
    #region task
    try {
        Write-Verbose "Looking up NYC weather for [$requesterUsernameExample ($requesterUserGUIDExample)] at $globalVarExample, in product $productNameExample"
    
        $result = Get-Current-NYC-Weather
        Write-Information "Result: $($result.weather)"
    
        $Log = @{
            Action = "Undefined"
            System = "OpenMeteo"
            Message = "Successfully executed action for [$requesterUsernameExample ($requesterUserGUIDExample)]"
            IsError = $false
            TargetDisplayName = $requesterUsernameExample
            TargetIdentifier = $([string]$requesterUserGUIDExample)
        }
    
        Write-Information -Tags "Audit" -MessageData $log
    
        $email = @{
            from = "[email protected]"
            to = "[email protected]"
            subject = "Your weather report"
            body = "<strong>$($result.weather)</strong>"
            confidential= $false
        }
    
        Write-Information -Tags "Email" -MessageData $email
    } catch {
        $ex = $PSItem
        Write-Warning "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
    
        Write-Error "Error executing action for [$requesterUsernameExample ($requesterUserGUIDExample)]. Error Message: $($ex.Exception.Message)"
    
        $Log = @{
            Action = "Undefined"
            System = "OpenMeteo"
            Message = "Error executing action for [$requesterUsernameExample ($requesterUserGUIDExample)]. Error Message: $($ex.Exception.Message)"
            IsError = $true
            TargetDisplayName = $requesterUsernameExample
            TargetIdentifier = $([string]$requesterUserGUIDExample)
        }
        Write-Information -Tags "Audit" -MessageData $log
    }
    #endregion task
    2023-09-25_13-43-18.jpg

    Warning

    Product actions that run frequently and log a lot of lines can quickly fill up your system's database. Limit the amount of logging to avoid running out of database space and hitting the Performance limits.

  9. If your script relies on form data, enter JSON in the Input tab to simulate form input.

    For this example, we'll enter the following JSON to simulate input from our Weather report form:

    {
       "tempUnitDropdown": {
          "tempUnitLabel": "Fahrenheit",
          "tempUnitValue": "fahrenheit"
       }
    }
    2023-09-25_13-44-14.jpg
  10. To test your script, click Execute PowerShell. View the results on the Received logs and Received audit logs tabs.

    For this example, the NYC weather has been fetched and logged.

    2023-09-25_13-45-18.jpg
    2023-09-25_13-46-39.jpg
  11. Preview any email sends on the Notifications tab.

    2023-09-25_13-47-49.jpg
  12. Click Update.

  13. Click Save.

The action(s) will run during the appropriate lifecycle stage(s) during users' Product requests for this product.

Note

it is strongly recommended to Add a product action on the product's Actions - new tab, using a code snippet from the newer Code snippet library.

  1. Go to Self Service > Products and click Edit for the relevant product.

  2. Go to the Actions tab.

  3. Click Add product action.

  4. In the Task catalog dialog box, search for the pre-defined task that you want to create.

    For this example, we'll add the Add AD user to AD group action.

    2022-06-13_15-43-13.png
  5. Optional: Change the task's default Name.

    For this example, we'll use Add AD user to AD group.

  6. Set Execute On State to the stage of the Product request lifecycle when this script should be executed.

    For this example, we'll set it to Approved.

  7. Enter the required input values for the task.

    2022-12-05_15-15-40.jpg

    Tip

    Type {{ in a text field to activate autocomplete.

    You have access to $manager, $product, $request, $requester, and $resourceOwnerGroup. You also have access to System variables (under {{portal.}}) and Custom variables (under {{variable.}}).

    2023-08-11_12-33-51.jpg
  8. Click Update Action.

  9. Optional: Add additional actions for other stages of the Product request lifecycle.

    For example, here we've added the Remove AD user from AD group action to the product's Returned stage.

    2022-12-05_15-13-29.jpg
  10. Click Save.

The action(s) will run during the appropriate lifecycle stage(s) during users' Product requests for this product.

  1. Go to Self Service > Products and click Edit for the relevant product.

  2. Go to the Actions - New tab.

  3. Click the relevant State.

  4. For the relevant task, click Edit product action.

    2023-08-09_14-39-00.jpg
  5. Continue by following the instructions in Add a product action.

Edit a product action added from the Task catalog
  1. Go to Self Service > Products and click Edit for the relevant product.

  2. Go to the Actions tab.

  3. For the relevant product action, click Edit.

    2023-08-09_14-45-31.png
  4. Continue by following the instructions in Add a product action from the Task catalog.

  1. Go to Self Service > Products and click Edit for the relevant product.

  2. Go to the Actions - New tab.

  3. Click the relevant State.

  4. For the relevant action, click Delete product task.

  5. To confirm, click Delete.

  6. Click Save.

Remove a product action added from the Task catalog
  1. Go to Self Service > Products and click Edit for the relevant product.

  2. Go to the Actions tab.

  3. For the relevant product action, click Delete.

  4. To confirm, click Delete Action.

  5. Click Save.