Steps to move a Box folder in Salesforce

If your Salesforce org has been integrated with Box, then there might arise a situation where you may need to move a Box folder from one location to another. In this article we will learn how to move a Box folder properly inside Salesforce. 

Assumptions

  • Salesforce org is already acquired. 
  • Box account is already acquired. 
  • Box account is already integrated with Salesforce org. 

Steps

#1 Create a connected app in Box

  • Login to box account. 
  • Click on Dev console button.           
  • This will open the Box developers console in a new tab. 
  • Navigate to My apps section from the left side navigation drawer. 
  • Click on “Create New App” 

  • Select custom app 

  • In the prompt select “Server authentication(with JWT)” under authentication section. 

  • Give an app name such as “Box-Cust-App” 
  • Click on “Create App” 
  • Now open the newly created app 
  • Navigate to “Configuration” tab. 
  • Scroll down to the “Application scope” section. 
  • Enable the “write all files and folders stored in Box” checkbox. 

  • Scroll down to “Add and manage public keys” section 
  • Click on “Generate a public/private keypair” button.
    (Note: Two factor authentication needs to be enabled for your Box account in order to generate a public/private keypair.) 

  • The browser will prompt you to download a config.json file. Download the file and keep it aside for now. (We’ll be using the file in one of the next steps) 
  • Navigate to “Authorization” tab and click on “Review and Submit”. 
  • Now return to the box account page and click on “Admin Console”. 
  • Open “Apps” from the left-hand side navigation menu.

  • Navigate to “Custom Apps” tab 

  • In this screen you will see your custom app populated in the list which you submitted for review earlier. 
  • Click on “Status” column value of the custom app and select “Authorize”. 

#2 Leveraging BOX Move API

In this step we will be using a python script to leverage the Move API of Box in order to actually a move a folder from one location to another.

  • Create a python project in any of your favorite IDEs.
  • Using pip install the following packages – boxsdk, jwt.
  • Copy and paste the “config.json” file which downloaded in the previous step inside the root folder of the python project.
  • Write the following code:

from boxsdk import OAuth2, Client
import json
import requests
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import load_pem_private_key
import time
import secrets
import jwt

config = json.load(open(‘config.json’))

appAuth = config[“boxAppSettings”][“appAuth”]
privateKey = appAuth[“privateKey”]
passphrase = appAuth[“passphrase”]

# https://cryptography.io/en/latest/
key = load_pem_private_key(
data=privateKey.encode(‘utf8’),
password=passphrase.encode(‘utf8’),
backend=default_backend(),
)

authentication_url = ‘https://api.box.com/oauth2/token’

claims = {
‘iss’: config[‘boxAppSettings’][‘clientID’],
‘sub’: config[‘enterpriseID’],
‘box_sub_type’: ‘enterprise’,
‘aud’: authentication_url,
‘jti’: secrets.token_hex(64),
‘exp’: round(time.time()) + 45
}

keyId = config[‘boxAppSettings’][‘appAuth’][‘publicKeyID’]

assertion = jwt.encode(
claims,
key,
algorithm=’RS512′,
headers={
‘kid’: keyId
}
)

params = {
‘grant_type’: ‘urn:ietf:params:oauth:grant-type:jwt-bearer’,
‘assertion’: assertion,
‘client_id’: config[‘boxAppSettings’][‘clientID’],
‘client_secret’: config[‘boxAppSettings’][‘clientSecret’]
}
response = requests.post(authentication_url, params)

oauth = OAuth2(
client_id= config[‘boxAppSettings’][‘clientID’],
client_secret= config[‘boxAppSettings’][‘clientSecret’],
access_token= response.json()[‘access_token’]
)

client = Client(oauth)

updated_folder = client.folder(folder_id=’130048807913′).update_info({
“parent”: {
“id”: “129688895492”
}
})

print(‘Folder updated!’)

 

The above code uses the config.json file in order to generate the access token which will be used while performing API operations:

  • The important thing to modify in the above code is to modify the id of the folder (we are terming it as CHILD FOLDER) which needs to be moved from a one location to another (we are terming it as PARENT FOLDER).
  • Scroll down to the section and update the ids accordingly:
    updated_folder = client.folder(folder_id='<CHILD_FOLDER_ID_HERE>’).update_info({
    “parent”: {
    “id”: “<PARENT_FOLDER_ID_HERE>”
    }
    })

Note: You can find the id of a folder by the following methods:

  1. You can get the id of a folder directly from the box link which is of the below format:
    https://app.box.com/folder/130048807913
  2. You can get the folder ids from Salesforce workbench portal by querying the below tables:box__Folder_Meta__c – Contains records for child folders.box__FolderId__c – Contains records for parent folders.

 

#3 Sync data in Salesforce

  • Open box settings page in Salesforce.
  • Click on “Sync Now” button.

#4 Update entries in Salesforce tables

The final step is to update box__FRUP__c  table which contains records mapping child folder id with parent folder name.

  • Open APEX execution console window and execute the below code:box__FRUP__c[] frup = new List<box__FRUP__c>();
    try{
    frup = [
    SELECT
    box__Folder_ID__c,
    box__Object_Name__c,
    box__Record_ID__c
    from
    box__FRUP__c
    where
    box__Record_ID__c = ‘<SOURCE_CHILD_FOLDER_ID>’
    ];
    List<box__FRUP__c> listToUpdate = new List<box__FRUP__c>();
    for(box__FRUP__c item : frup) {
    box__Object_Name__c = ‘<DESTINATION_PARENT_FOLDER_ID>’;
    item.box__Record_ID__c = ‘<DESTINATION_CHILD_FOLDER_ID>’;listToUpdate.add(item);
    }
    update listToUpdate;
    } catch(DmlException e) {
    System.debug(‘An unexpected error has occurred: ‘ + e.getMessage());
    }