Automatic token generation on a WorkOrder using Trigger (Developer-oriented)

In this demo, we will demonstrate how to generate a token automatically on any newly-created WorkOrder objects.

To implement this, we will:

  1. Create a custom field on the WorkOrder object to hold the generated token.

  2. Create a Trigger to generate a SharinPix token value upon creation of a WorkOrder record.

triangle-exclamation

Creation of the custom field

Create a custom field on the object WorkOrder to retrieve the SharinPix token using the following information:

  • Field Label: SharinPix Token

  • Field Name: SharinPix_Token

  • Date Type: Long Text Area

  • length: 32, 768

Creation of the WorkOrder Trigger

Implement a WorkOrder Trigger to generate a SharinPix Token Value when a WorkOrder record is inserted. This token will match the SharinPix Album to its corresponding record. Implement the WorkOrder Trigger using the code snippet below:

trigger SharinPixWorkOrderTrigger on WorkOrder (after insert, before update) {
    sharinpix.Client clientInstance = sharinpix.Client.getInstance();
    String token, appUrl;
    List<WorkOrder> updatedWOrders = new List<WorkOrder>();
    for (WorkOrder wOrder : Trigger.new) {
        if (String.isBlank(wOrder.SharinPix_Token__c) || String.isBlank(wOrder.SharinPix_App_URL__c)) {
            token = sharinpix.Client.getInstance().token(
                new Map<String, Object> {
                    'album_id' => wOrder.Id,
                    'exp' => 0,
                    'path' => '/pagelayout/' + wOrder.Id,
                    'abilities' => new Map<String, Object> {
                        wOrder.Id => new Map<String, Object> {
                            'Access' => new Map<String, Boolean> {
                                'see' => true,
                                'image_list' => true,
                                'image_upload' => true,
                                'image_delete' => true
                            }
                        },
                        'Display' => new Map<String, Object> {
                            'tags'=> true
                        }
                    }
                }
            );
            appUrl = 'sharinpix://upload?token=' + token;
            if (Trigger.isInsert) {
                updatedWOrders.add(new WorkOrder(
                    Id = wOrder.Id,
                    SharinPix_Token__c = token,
                    SharinPix_App_URL__c = appUrl
                ));
            } else {
                wOrder.SharinPix_Token__c = token;
                wOrder.SharinPix_App_URL__c = appUrl;
            }
            
        }
    }
    if (Trigger.isInsert) { update updatedWOrders; }
}

The source code for the Apex Class of the above Apex Trigger can be found below:

circle-info

You can find this Trigger source in Github here: SharinPix Mobile App Launcher (FSLarrow-up-right)

circle-exclamation

To test the previous implementations, create a new WorkOrder record. The generated token will be found in the SharinPix Token field of the newly-created record as shown below:

Last updated

Was this helpful?