function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
James MadisonJames Madison 

file upload with box integration

Hello,
I'm new (like beginner) to Salesforce and we are trying to integrate BOX, I need to create and object which must have a file upload field and save that into BOX, please someone help me the procedure? I googled but no use (since I'm a new I have no clue where to find)
Appreciate your help..
James MadisonJames Madison
Hey Onesh, thank you very much for your reply.
I tried box integration with the app and successfully upload files but we need to have functionality to upload files from Notes & Attachments to box.
By the way I'm newely learning salesforce (I haven't started Apex and Visualforce), do we need Apex and Visualforce knowledge for this functionality?

Thanks again Onesh..
Onesh ReddyOnesh Reddy
To achieve functionality to upload files from Notes & Attachments to box.
You may have to use a simple Apex code.
I can help you with that !! 
James MadisonJames Madison
Oh thats really helpful Onesh.... Thank you very much.
Could you provide details if possible with examples?

And also I have qyestion, is box asks to login for first time when the user logins to salesforce? 
Here is the thing I did,
1) I downloaded box app from appexchange and did setup (with two box accounts, one is service and another one is admin)
2) created a Account, and I see box upload in the bottom of account page and uploaded file
3) created a user (for another seperated salesforce account) but when created an account with this user account its asking box login, can we skip this?

Once again thank you verymuch Onesh...
Onesh ReddyOnesh Reddy
For the second user you have created Login to Box is mandatory to Save Account into Box. It asks the Login details for user only once.
Its just a security protection that every user in your salesforce org cannot login into or Save data into Box unless they have Credentials.
Hope this answers your Question.

The below procedure uploads the file from Salesforce to Box, when a file is attached in Salesforce.
1) Create a folder in your Box Account and copy the FolderId.(The folder where you would like to upload files from Salesforce)
     Copy the Highlighted folderid 
2) Goto your Salesforce Developer Console a Click on FIle-- New--Trigger, give the name and Select Attachment as Object.
3) Empty the Default content in Trigger.
4) Copy and Paste the below code in your Trigger.
//Copy and Paste the Code..AttachmentUsingToolkit is the name of Trigger. Make sure to override the Triggername.
trigger AttachmentUsingToolkit on Attachment (after insert, after update) {
    
    //Paste the copied BoxFOlderid in the below line.
    String BoxFolderid='11365458650'; 
    box.Toolkit boxToolkit = new box.Toolkit();
    Attachment Myattachment=[select name,body,parentid from attachment where id=:trigger.new[0].id];
    boxtoolkit.createFileFromAttachment(Myattachment,null, BoxFolderid,null);
}

5) Save the Trigger.. CTRL+S
6) Next goto an Account and Upload a file from attachment.
7) It should be visible in the Box folder.
Bingoo

Let me know if it helps you.

Best Regards,
Onesh.K
 
James MadisonJames Madison
This is perfect I guess, let me try from my end and see... :) thanks a lot Onesh ..
James MadisonJames Madison
hi Onesh,

I tried with all your test but I can't find the uploaded file in my box (new folder I creaded).
AFTER created trigger do we have to add this trigger to any specific Object (in our case Account)? I guess Notes and Attachments are all common, what ever the object we use to attachment are all goes to box, is that correct?

Here are the steps how I attached file.
1) Click Account -> New --> save
2) Open that account --> click on Attach File under Notes & Attachments and uploaded
3) checked the folded in box, but no file loaded

here is the code for trigger i Used
 
trigger SFBoxAttachment on Attachment (after insert, after update) {
    String BoxFolderid='11363284100'; // this is folder Id I copied from box URL
    System.debug('Hello box ');
    box.Toolkit boxToolkit = new box.Toolkit();
    Attachment Myattachment=[select name,body,parentid from attachment where id=:trigger.new[0].id];
    boxtoolkit.createFileFromAttachment(Myattachment,null, BoxFolderid,null);
    //boxToolkit.commitChanges();
}

Note: I don't even see "'Hello box '" debug on logs
Please advice.

 
James MadisonJames Madison
Im still struggling to get this soluttion

Quick question, before making Apex/trigger classes we must have to install BOX from AppExchange, is it right?
James MadisonJames Madison
I think I'm missing something in between, I set System.debug between lines and I see the error "AUTH_WRAPPER_PREPARE_FAIL_MESSAGE=Unable to use default credentials to make a callout to box (maybe this is in a trigger or constructor):"
Am I missing anything?
box.Toolkit boxToolkit = new box.Toolkit();
System.debug(boxToolkit);
14:09:57:025 USER_DEBUG [5]|DEBUG|Toolkit:[AUTH_WRAPPER_PREPARE_FAIL_MESSAGE=Unable to use default credentials to make a callout to box (maybe this is in a trigger or constructor): , authWrapper=BoxAuthWrapper:[adminSettings=box__BoxUser_Settings__c:{}, client_id=0lep3h31wef8um4p6ncc47e5n0awpssv, client_secret=D4XxxP83Ukh2pj6sUyGU2sgsPjMaTONm, redirect_uri=null, resultMessage=, userSettings=box__BoxUser_Settings__c:{}], didAuthChange=false, existingFolderId=null, mostRecentError=, newFolderMetas=(), newFrups=(), newRootFolders=()]
Onesh ReddyOnesh Reddy
Yes James we must install Box from AppExchenge.
The Trigger failed to Callout the method to Box, So i have replaced the code in a Helperclass and its working.

Trigger :
//Copy and Paste the Code..AttachmentUsingToolkit is the name of Trigger. Make sure to override your Triggername from mine.
trigger AttachmentUsingToolkit on Attachment (after insert, after update) {
   //Call the method from HelperClass 
    BoxAttachmentHelperclass.createattachment(trigger.new[0].id);
    system.debug('In Trigger');
}

Create an ApexClass and copy the below code :
public class BoxAttachmentHelperclass {
    @future (callout=true) //Callout is made to Box
    public static void createAttachment(id attachmentid){
        system.debug('In HelperClass');
        String BoxFolderid='11365458650'; //Paste the box folderid
        box.Toolkit boxToolkit = new box.Toolkit();
        Attachment Myattachment=[select name,body,parentid from attachment where id=:attachmentid];
        boxtoolkit.createFileFromAttachment(Myattachment,null, BoxFolderid,null);
    }
}

Let me know if it helps you.

Best Regards,
Onesh.K
 
Onesh ReddyOnesh Reddy
Below is the Updated HelperClass
 
public class BoxAttachmentHelperclass {
    @future (callout=true)
    public static void createAttachment(id attachmentid){
        system.debug('In HelperClass');
        String BoxFolderid='11365458650'; 
        box.Toolkit boxToolkit = new box.Toolkit();
        Attachment Myattachment=[select name,body,parentid from attachment where id=:attachmentid];
        boxtoolkit.createFileFromAttachment(Myattachment,null, BoxFolderid,null);
        boxToolkit.commitChanges();
    }
}

 
James MadisonJames Madison
Thanks Onesh,
After we uploaded the file, is that give the link from box to download again?
James MadisonJames Madison
It worked .. Its really great Onesh ... wonderful .. Please ignore my just before question.. I see links as well :)
James MadisonJames Madison
So there are multiple users uploads files and all these files stored in that same folder, correct? and they are all related to corresponding user.. i.e. one user uploads a file and another user can't see, rt?
James MadisonJames Madison
How do we make sure that uploaded file not stored in Salesforce? I can see them in box but I want to make sure that attachments are not in SF.
James MadisonJames Madison
Hi Onesh,

I guess our solution is not upto expection because we are still storing files in Salesforce File Storage, do we have any solution which we can avoid storing in SF and store directly to BOX and give view link? either creating VF page for upload ....
Peter Rocks 8Peter Rocks 8
Thanks for simple overview Onesh which worked first time. This has been useful for me as a SFDC beginner to understand how to construct Triggers and Helper Classes with Box for Salesforce Managed package & Toolkit :)
Brian MartinezBrian Martinez
Curious if it's possible to create a different folder structure for uploads..Let's say that the attachment is from the Opportunity Object, and you want it to create a folder for Account, Account Name, Opportunities, Files attached. Is that possible? Right now while playing with BOX it creates the object of the record, so if opportunity, it creates Opportunities, Opportunity Name, Files related to opportunity. This makes it hard to find records because Opportunity names are not unique. Having it with the account being the main folder then the opportunity is much easier to search for because you can search for a specific account then located records from there. 
Harshvardhan YadavHarshvardhan Yadav
 box.Toolkit boxToolkit = new box.Toolkit();
 box.Toolkit is an inalid type,  developer consol is giving error.
 
Anurag DandamudiAnurag Dandamudi
Hi, the current sdk only supports upload of upto 4.3MB, how to upload file of upto 25MB? can we use blob or something with the api callout?