Sunday, November 9, 2014

Writing a post method !! of course for Egnyte Integration

Method to get the files under one folder
public string emailFile(){
                
                if(wrapPdfLinksList!=null && wrapPdfLinksList.size()>0 ){
                        list<string> selectedFiles = new list<string>();
                        for(wrapPdfLinks w :wrapPdfLinksList){
                                if(w.check)
                                selectedFiles.add(w.pdfLink);
                        }
                        for(string thefolder :selectedFiles){
                                
                                // Create a new http object to send the request object 
                                string accessibility = 'anyone';
                                boolean send_email = false;
                                string path =thefolder;
                                boolean notify = false;
                                string recipients;
                                //End of creating the variables
                                
                                /*//to get quote num
                                temp1 = thefolder.split('/');
                                if(temp1.size()>5){
                                    system.debug('***** temp1-- '+temp1);
                                        if(temp1[4] != null)
                                            quoteNo = temp1[4];
                                }   
                                //End to get Quote Number*/
                                
                                //to get the receipent of the Quote
                                list<Quotes__c> receipientList = new list<Quotes__c>([SELECT Email__c from Quotes__c WHERE Name =: quoteNo Limit 1]);   
                                system.debug('UUUUUUUUUU'+receipientList);
                                
                                if(receipientList != null){ 
                                    for(Quotes__c a :receipientList){
                                        recipients = a.Email__c;
                                        system.debug('DDDDDDDDDD'+recipients);
                                    }
                                }
                                //end to get the receipent of the Quote
                                
                                
                                HttpRequest req = new HttpRequest();
                                req.setEndpoint('https://constructionspecialties.egnyte.com/pubapi/v1/links');
                                req.setMethod('POST');
                                 
                                list<eToken__c> sToken = new list<eToken__c>([Select Token__c From eToken__c where name =: UserInfo.getUserEmail()]); 
                                system.debug('***sToken**'+sToken);
                                 
                                req.setHeader('Authorization', 'Bearer '+sToken[0].Token__c);
                                req.setHeader('Content-Type','application/json');
                                                                
                                string reqBody = '{"accessibility":"'+accessibility+'","send_email":"'+send_email+'","type":"file","path":"'+path+'","notify":"'+notify+'","link_to_current":"true","recipients":["'+recipients+'"]}';
                                req.setBody(reqBody);
                                
                                Http http = new Http();
                                HTTPResponse res = http.send(req);
                                system.debug('reqBody'+reqBody);
                                System.debug(res);
                                system.debug('***** response---'+res.getStatusCode());
                                system.debug('***** to string---'+res.getbody());
                                //fileURL = res.getbody();
                                string theResponse = res.getbody();
                                
                                string afterUrl = theResponse.subStringAfter('"url":"');
                                 system.debug('***** afterUrl---'+afterUrl);
                                string theUrl = afterUrl.subStringBefore('","recipients":');
                                system.debug('***** theUrl---'+theUrl);
                                fileURL = theUrl;
                        }
                
                } 
         
         String url = '/_ui/core/email/author/EmailAuthor?p7=The%20files%20'+fileURL; 
         pageReference pr = new PageReference(url);
   pr.setRedirect(true);
      return url;
         }

Getting started with Egnyte API

Now the first part is getting the token !
so I wrote a short code on the custom button to get the code and then redirect it to a page which calls a page and its controller which saves it in custom settings.


Here is the diagram explaining the same !!

You can read about it here <Token Page>
Custom Button
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 

var qResult = sforce.connection.query("Select Name, Token__c from eToken__c ");
records = qResult.getArray("records");
if(records == ''){
var idofQuote ='{!Quotes__c.Id}';
window.location.href='https://<ur domain>.egnyte.com/puboauth/token?client_id=<ur api KEY>&redirect_uri=<complete url of ur vf page>?id='+idofQuote+'&mobile=0';
}

The Page
    
Redirecting............  
  
       
      
          
      
  
  
  
  


The part of the controller
public string authToken {get;set;}
public pagereference basicAuthCallout(){
        id quoId = ApexPages.currentPage().getParameters().get('Id');
        
        string token2 ; 
        
        
            // Get the URL for the current request.
            url currentRequestURL = URL.getCurrentRequestUrl();
            system.debug('>>>>>>>>>>authToken>>>>>>>>>>>>>'+authToken);
     if(authToken!= null){  
             String extract = authToken ;
            list newstring2 = new list();
            list newstring;
            if(extract !=null){
                newstring = new list(extract.split('#access_token='));
                
            system.debug('*******newstring******'+newstring);}
            
            for(String a : newstring){
                if(a.contains('&token_type')){
                    newstring2 = a.split('&token_type');
                }
            }
            if(newstring2 !=null && newstring2.size() > 0){
                string theToken = newstring2[0];
                system.debug('theToken'+theToken);
                token2 = theToken;
            }
            
    }
            eToken__c customToken = new eToken__c();
            customToken.name = UserInfo.getUserEmail();
            customToken.Token__c = token2;
            if(([Select Name, Token__c from eToken__c]).isEmpty()){
            	upsert customToken ;
            }	
            PageReference pageRef = new PageReference('/'+ quoId); 
            return pageRef;
            }
  

All about the egnyte Integration

Well I had a client requirement in which they wanted the Egnyte API to be integrated with salesforce.The first one was to make the egnyte page appear on the Quote.This was achived by this guide.The egnyte salesforce integration guide Installion guide



And the result was this !!


Things to remember


  1. Egnyte after integration puts everything of the folder under shared/salesforce.com/<name of the object>/<Name of the record>
  2. If u need to test Egnyte for different users then you need to use that users's SF username and password as well as Egnyte username and password(sorry dude! logging in as different user dsnt work for Egnyte integration)
  3. You need to add the salesforce userid to Egnyte to access it through egnyte !


Now I will be explaining the actual code in different posts so stay tuned.!!!!