Send csv File

Send .csv File to Another Org in Salesforce

In this blog, we will see how to send the CSV to another org Using Salesforce Integration

First, you need to upload a CSV file, Check the below link for the same:

Upload a .csv file 

After you have created the CSV file you need to send it to the other org, which is your Target Org. In your Target Org, go to SetUp>Create>Apps and create a new org, and generate the Client Id and Client Secret by enabling OAuth Settings. You can select Full Access for this example.

dont miss out iconDon't forget to check out: How Variable Types Operate in the Lightning Component Framework

After this you need to create an Apex Class in your Target Org:

@RestResource(urlMapping = '/getFile/*')
global with sharing class TargetClass {
    @HttpPost
    global static Boolean TargetClass(){
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
        Blob b = req.requestBody; // This is the file which you will receive from source org.
        Boolean returnBooleanValue;
        if(b!=null){
            returnBooleanValue = true;
        } else{
            returnBooleanValue = false;
        }
        return returnBooleanValue;
    }
}

Now, go to your Source Org and Create Remote Site Setting, enter the URL for your Target Org and save.

Now, to Developer Console and Create an Apex Class, this class will return the access token for your Target Org.

public class SalesforceIntegrationRESTClass {
    private static final string ClientId= ‘Your_Target_Org_Client_Id’  //Get details from Connected app in the Setup
    private static final string clientSecret='Your_Target_Org_Client_Secret';   //Get details from  Connected app in the Setup
    private static final string username='Target_Org_Username';//Target org user name)
    private static final string password='Target_Org_Password';//Target org Password)
    public class deserializeResponse {
        public String id;
        public String access_token;
   }
   public static String ReturnAccessToken (){        
       String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
       Http h = new Http();
       HttpRequest req = new HttpRequest();
       req.setBody(reqbody);
       req.setMethod('POST');
       req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        HttpResponse res = h.send(req);
        system.debug('@@@@[email protected]@'+res.getbody());
        deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
        system.debug('@@@@[email protected]@'+resp1 );
        System.debug('#### resp1.access_token = '+resp1.access_token);
        return resp1.access_token;
    }
    
}

In your, Source Org Create an Apex Class where you will upload a csv file and send it to Target Org.

public class RegisterandSaveCSVFileClass {    
    public Project__c newProject {get; set;}
    public Attachment attachment {get;set;}    
    public RegisterandSaveCSVFileClass(){
        newProject = new Project__c();
        attachment = new Attachment();
    }
    public void saveButtonAction(){
        System.debug('newProject.Name'+newProject.Name);
        if(newProject.Name != null){
            insert newProject;               
            if(attachment.Name.endsWith('.csv')){
                attachment.parentid = newProject.id;
                insert attachment;   
                sendFileForIntegration(csvFileBody);                             
            }else{
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'You need to upload only CSV document');
                ApexPages.addMessage(myMsg); 
                return ;
            }
        }
    } 
    @future(callout = true)
    public static void sendFileForIntegration(Blob data){        
        HttpRequest req = new HttpRequest();
        String accessToken;
        accessToken = SalesforceIntegrationRESTClass.returnAccessToken();        
        String endPoint = ‘Your Target Org URL’; 
//For Example, your Target Org URL is https://login.salesforce.com, and for mapping you have used getFile(check the URL Mapping in your Target Class above), so the complete endPoint URL will be: https://login.salesforce.com/services/apexrest/YOUR_TARGET_ORG_NAMESPACE/getFile
        req.setHeader('Authorization','Bearer ' + accessToken);
        req.setEndpoint(endpoint);
        req.setBodyAsBlob(data);        
        req.setHeader('Content-Type','application/csv');
        req.setMethod('POST');
        req.setTimeout(120000);        
        HttpResponse response = new Http().send(req);
        System.debug('response:'+response.getBody());                
    }       
}

dont miss out iconCheck out another amazing blog by Krati here: How To Create A Lightning Web Component | The Salesforce Developer Guide

Check the Response debug and you will receive the boolean value true.

Responses

Popular Salesforce Blogs