Welcome to the new Box Support website. Check out all the details here on what’s changed.

Box Shared Link through Salesforce Apex Class

Answered
New post

Comments

7 comments

  • Jason

    Hi , thanks so much for posting to the forums! I think the docs are slightly off - I'm getting in a request to update them.

     

    Here's what I did to create a shared link:

     

    BoxFolder folder = new BoxFolder(api, 'folder_id_here');
    BoxSharedLink.Permissions permissions = new BoxSharedLink.Permissions();
    BoxSharedLink.Info linkInfo = folder.createSharedLink(BoxSharedLink.Access.OPEN, null, permissions);

    That also assumes you have defined "api" above like what we have here:

    https://github.com/box/box-salesforce-sdk/blob/master/doc/authentication.md

    or here:

    https://github.com/box/box-salesforce-sdk/blob/master/doc/platform.md

     

    Hope that helps! I personally am a bit of a novice at Apex so if you find a way to explain or improve that further, please let me know!

     

    Thanks,

    Jason

    0
    Comment actions Permalink
  • SF-Admin

    I am creating shared link for file via apex code in Salesforce but it's giving me incorrect signature error. Here is my code,

     

    BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
    BoxFile file = new BoxFile(api, fileInfo.Id);
    BoxSharedLink.Permissions permissions = new BoxSharedLink.Permissions();
    BoxSharedLink.Info link = file.createSharedLink(BoxSharedLink.Access.OPEN, null, permissions);

     

    Here is the error that I am getting, 

     

    Method does not exist or incorrect signature: void createSharedLink(BoxSharedLink.Permissions) from the type BoxFile

     

    Please help.

    0
    Comment actions Permalink
  • lcampbell1

    It doesn't work, dude. Notice that they always refer us to that same link, but never give any explanation of how/why it works. Actually, if you have box installed, you already have these classes installed. You can't see the content of them, though, because they're managed. You can see the class names, however.

    0
    Comment actions Permalink
  • ChristosZ

    In case anyone is still agonizing over this, I just managed to create/update an object's shared link and return it.

     

    public without sharing class BoxHelper {
    
        public class BoxObjectJSON {
            public String type {get; set;}
            public String id {get; set;}
            public String name {get; set;}
            public BoxObjectJSON parent {get; set;}
            public SharedLinkJSON shared_link {get; set;}
        }
    
        public class SharedLinkJSON {
            public String url {get; set;}
            public String download_url {get; set;}
            public String access {get; set;}
            public PermissionsJSON permissions {get; set;}
            
            public SharedLinkJSON(String access, boolean can_download) {
                this.access = access;
                this.permissions = new PermissionsJSON(can_download);
            }
        }
        
        public class PermissionsJSON {
            public boolean can_download {get; set;}
            
            public PermissionsJSON(boolean can_download) {
                this.can_download = can_download;
            }
        }
        
        /**
         * Creates a shared link for a Salesforce object's associated Box folder.
         * If the shared link already exists its permissions are updated. Set 'access' to null to delete link.
         * Returns the shared link URL or null if Box folder doesn't exist, invalid inputs, or deleted.
         *
         *  objectId    Salesforce record id of so
         *  access      The Box access level to use (open/company/collaborators), null to delete
         *  canDownload Whether link allows downloads
         */
        public static string procureSharedLink(Id objectId, string access, boolean canDownload) {
            String[] validAccessTypes = new String[]{ 'open', 'company', 'collaborators', null };
            
            if (objectId == null) return null;
            if (!validAccessTypes.contains(access)) return null;
            
            box.Toolkit toolkit = new box.Toolkit();
            
            string folderBoxId = toolkit.getFolderIdByRecordId(objectId);
            if (folderBoxId == null) return null;
            
            string sharedLink = JSON.serialize(new SharedLinkJSON(access, canDownload), true);
            if (access == null) sharedLink = null;
            
            String endpoint = 'https://api.box.com/2.0/folders/' + folderBoxId + '?fields=shared_link';
            String body = '{"shared_link":' + sharedLink + '}';
            
            HttpRequest req = new HttpRequest();
            req.setEndpoint(endpoint);
            req.setMethod('PUT');
            req.setBody(body);
            
            System.debug('Creating shared link...');
            HttpResponse res = toolkit.sendRequest(req);
            if (res.getStatusCode() != 200) {
                System.debug('[Box Shared Link Error] Creating link for object '+objectId+' (folder '+folderBoxId+'). Body: '+res.getBody()+'(status code: '+res.getStatusCode()+')');
                return null;
            }
            
            SharedLinkJSON sLinkJSON = ((BoxObjectJSON)(JSON.deserialize(res.getBody(), BoxObjectJSON.class))).shared_link;
            toolkit.commitChanges();
            
            return (sLinkJSON == null ? null : sLinkJSON.url);
        }
        
    }

     

    0
    Comment actions Permalink
  • AIUIntegration

    Are you able to disable download feature using this method?

    0
    Comment actions Permalink
  • DW19

    Hi Christos, 

     

    Would you mind sharing how you got your test class to work with the sendRequest generic method? I've been struggling to develop a test class than can work with this. 

     

    cheers

     

    Daniel

    0
    Comment actions Permalink
  • PromBot-Box

    Hi There,

    I am connecting to Salesforce through Apex. I use the developer token to access the Box APIs, but this token expires every 60 minutes.

    Can you help me some code example on how to connect, without the need for the developer token?  What is the best practice?

    Box support is suggesting the platformAPI route, but I am not sure of configuring the private/public key.

     

    String userId = 'USER_ID';
    String publicKeyId = '1234';
    String privateKey = '5567';
    String clientId = 'CLIENT_ID';
    String clientSecret = 'CLIENT_SECRET';

    BoxJwtEncryptionPreferences preferences = new BoxJwtEncryptionPreferences();
    preferences.setPublicKeyId(publicKeyId);
    preferences.setPrivateKey(privateKey);
    BoxPlatformApiConnection api = BoxPlatformApiConnection.getAppEnterpriseConnection(userId, clientId, clientSecret, preferences);

    0
    Comment actions Permalink

Please sign in to leave a comment.