SharinPix DocumentationMain Documentation SharinPix - Access and SecuritySharinPix automatic token generation (Developer-oriented)

SharinPix automatic token generation (Developer-oriented)

Overview

In this article, you will learn how to use a trigger to automatically generate a token on an object.

Setting-Up

Assumption:

For this demo, we will use the WorkOrder object.

Firstly, we need to create a field on the WorkOrder object that will hold the generated token.

For the new field:

  1. Data type: Text Area (Long)
  2. Field Label: SharinPix Token

Creation of the Trigger

In this section, we will create a Trigger on the WorkOrder object. 

Name the Trigger SharinPixWorkOrderTrigger and use the code snippet below to create it:

trigger SharinPixWorkOrderTrigger on WorkOrder (after insert, before update) {
    sharinpix.Client clientInstance = sharinpix.Client.getInstance();
    String token;
    List<WorkOrder> updatedWOrders = new List<WorkOrder>();
    for (WorkOrder wOrder : Trigger.new) {
        if (String.isBlank(wOrder.SharinPix_Token__c)) {
            token = sharinpix.Client.getInstance().token(
                new Map<String, Object> {
                    'album_id' => wOrder.Id,
                    'exp' => 0,
                    'path' => '/pagelayout/' + wOrder.Id,
                    'native_app_options' => new Map<String, String> {},
                    '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,
                                'image_annotate' => true
                            }
                        },
                        'Display' => new Map<String, Object> {
                            'tags'=> true
                        }
                    }
                }
            );
            if (Trigger.isInsert) {
                updatedWOrders.add(new WorkOrder(
                    Id = wOrder.Id,
                    SharinPix_Token__c = token
                ));
            } else {
                wOrder.SharinPix_Token__c = token;
            }
            
        }
    }
    if (Trigger.isInsert) { update updatedWOrders; }
}

The Trigger SharinPixWorkOrderTrigger:

  1. Generate a token
  2. Stores the value of the token generated in the field SharinPix Token

Find the SharinPix Token

A SharinPix Token is now generated whenever a new WorkOrder is created. This token can be found inside the SharinPix Token field of the newly-created object.

0 Comments

Add your comment

E-Mail me when someone replies to this comment