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
DJ 367DJ 367 

Batch saved and running successfully however blob is not getting into external server

Hello All,

I am using rest API to send record as blob to external server, Here my code is saved and running successfully but I am not receiving the blob into external server. I am getting the value in the query. Thanks

global class FileImpoLog implements Database.Batchable<sobject>,Database.AllowsCallouts {   
    global final String CRLF = '\r\n';  
    global final String CHARSET = 'UTF-8'; 
    
    global String addSimpleFormData(string paramName, string wert, final string boundary) {  
        string body = '';
        body+=('--')+boundary+(CRLF);  
        body+=('Content-Disposition: form-data; name="' + paramName + '"')+(CRLF);
        body+=('Content-Type: application/json; charset=' + CHARSET)+(CRLF);  
        body+=(CRLF);
        body+=wert+(CRLF);  
        return body;
    }
    
    global String addFileData(String paramName, String filename, String abc, final string boundary) {
        
        String body = '';  
        body+=('--')+boundary+(CRLF); 
        body+=('Content-Disposition: form-data; name="' + paramName + '"; filename="' + filename + '"')+(CRLF);
        body+=('Content-Type: application/octet-stream')+(CRLF);  
        body+=('Content-Transfer-Encoding: binary')+(CRLF); 
        body+=(CRLF); 
        body+=abc;  
        body+=(CRLF);
        
        return body;
    }
    
    global String addFileDataNew(String paramName, String filename, String abc, final string boundary) {
        
        String body = '';  
        body+=('--')+boundary+(CRLF); 
        body+=('Content-Disposition: form-data; name="' + paramName + '"; filename="' + filename + '"')+(CRLF);
        body+=('Content-Type: application/octet-stream')+(CRLF);  
        body+=('Content-Transfer-Encoding: binary')+(CRLF); 
        body+=(CRLF); 
        body+=abc;  
        //body+=(CRLF); commented bcz endstring character
        
        return body;
    }
    
    global String addCloseDelimiter(final string boundary) {
        
        String body = ''; 
        body+=('--')+boundary+('--')+(CRLF); 
        return body; 
    } 
    
    
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('Select Id,created_by__c,Document_Process_Date__c,WorkspaceId__c,Workspace_Name__c,Created_Date__c from File_Importer_Log_Entry__c where  CreatedDate = today' );

    } 

    global void execute(Database.BatchableContext BC, list<File_Importer_Log_Entry__c> obj){  
        //String boundary = '---------------JKKJLJLJLJLJL';
        //List<blob> lstBody;
        String boundary = '---------------BOUNDARY_STRING'; 
        
        Blob bodyBlb;
        for(File_Importer_Log_Entry__c M : obj){
            string fileName = 'Log'+M.Id ;
            string fileType = 'text/plain';
           bodyBlb = Blob.valueOf('Created By:'+M.created_by__c+'/n'+'Document Process Date :'+M.Document_Process_Date__c+'/n'+'WorkspaceId :'+M.WorkspaceId__c+'/n'+'Workspace Name'+M.Workspace_Name__c+'/n'+'Created Date :'+M.Created_Date__c);
           
           String body = ''; 
            body+=(CRLF); 
            
            String first =  addSimpleFormData('name', '{"doc_profile":{"name": "'+fileName+'","extension":"'+fileType+'"}}', boundary);
            body+=first; 
            String second = addFileData('file', fileName, EncodingUtil.base64Encode(bodyBlb), boundary);
            
            body+=second; 
            String third = addCloseDelimiter(boundary); 
            body+=third; 
            
            String loginToken = MSOP_iManage_Service.getLoginToken();
            String targetUrl = 'https://test.com/api/v1/folders/folderName/documents';
            HttpRequest req = new HttpRequest();
            req.setEndpoint(targetUrl);
            req.setHeader('x-auth-token', loginToken);
            req.setHeader('Content-Type', 'application/json');
            req.setMethod('POST'); 
            req.setBody(Body);
            Http http = new Http();
            HttpResponse res = http.send(req);
            system.debug('**********res : ' + res); 
        }
  
    }
    
    global void finish(Database.BatchableContext BC){                 

	}
}