Send .csv File to Another Org in Salesforce : krati vishnoi

Send .csv File to Another Org in Salesforce
by: krati vishnoi
blow post content copied from  Forcetalks
click here to view original post

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 {
    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;
            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();
        HttpResponse res = h.send(req);
        deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
        system.debug('@@@@access_token@@'+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(){
        if(newProject.Name != null){
            insert newProject;               
                attachment.parentid =;
                insert attachment;   
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'You need to upload only CSV document');
                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, and for mapping you have used getFile(check the URL Mapping in your Target Class above), so the complete endPoint URL will be:
        req.setHeader('Authorization','Bearer ' + accessToken);
        HttpResponse response = new Http().send(req);

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.

The post Send .csv File to Another Org in Salesforce appeared first on Forcetalks.

January 08, 2021 at 11:04PM
Click here for more details...

The original post is available in Forcetalks by krati vishnoi
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.