Last active
February 4, 2020 03:03
-
-
Save Sunil02kumar/db93e00befce2ed17bca6d00bfc37051 to your computer and use it in GitHub Desktop.
Migrate Attachments from one Salesforce to another Salesforce Org
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SK_AttachmentJSONParser { | |
public Integer totalSize; | |
public boolean done; | |
public cls_records[] records; | |
public class cls_records { | |
public String Id; | |
public string ParentId; | |
public String Name; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SK_AttachmentMigrationHelper { | |
public static string findAttachmentDetails(string sourceOrgRecId,string sourceOrgURL,string access_Token,string currOrgParentRecId){ | |
string returnString=''; | |
Map<string,string> headersMap = new Map<string,string>(); | |
headersMap.put('Authorization', 'Bearer ' + access_Token); | |
headersMap.put('Content-Type', 'application/json'); | |
string EndPointURL= SourceOrgURL +'/services/data/v45.0/query/?q=select+id,parentid,Name+from+attachment+where+id=\''+sourceOrgRecId+'\''; | |
HTTPRequest req = generateHTTPRequest(EndPointURL,'GET',headersMap,null); | |
HTTPResponse res = performCallout(req); | |
system.debug('****response body for '+sourceOrgRecId+':'+res.getBody()); | |
if(res.getStatusCode()==200){ | |
string JSONString= res.getBody(); | |
if(JSONString!='' && JSONString!=null){ | |
SK_AttachmentJSONParser JSONDetails=(SK_AttachmentJSONParser) System.JSON.deserialize(JSONString, SK_AttachmentJSONParser.class); | |
if(JSONDetails.totalSize>0){ | |
string attachmentId,attachParentId,attachmentName; | |
for(SK_AttachmentJSONParser.cls_records ss: JSONDetails.records){ | |
attachmentName = ss.Name; | |
attachmentId = ss.Id; | |
attachParentId = ss.ParentId; | |
} | |
if(attachmentId!=null && attachmentId!=''){ | |
EndPointURL= SourceOrgURL +'/services/data/v45.0/sobjects/Attachment/'+sourceOrgRecId+'/body'; | |
HTTPRequest req1 = generateHTTPRequest(EndPointURL,'GET',headersMap,null); | |
HTTPResponse res1 = performCallout(req1); | |
blob blobContent=null; | |
if(res1.getStatusCode()==200){ | |
blobContent=res1.getBodyAsBlob(); | |
Attachment att= new Attachment(); | |
att.parentId = currOrgParentRecId; | |
att.name= attachmentName; | |
att.body= blobContent; | |
insert att; | |
system.debug('***attachment inserted successfully:'+att.Id); | |
returnString ='SUCCESS:Id-'+att.Id; | |
} | |
} | |
} | |
} | |
}else{ | |
system.debug('***Some issue with API Call:'+res.getBody()); | |
} | |
return returnString; | |
} | |
public static HTTPRequest generateHTTPRequest(string EndPointURL,string reqMethod, Map<string,string> headersMap,string reqBody){ | |
HttpRequest req = new HttpRequest(); | |
//set HTTPReq headers | |
if(headersMap.size()>0){ | |
for(string ss:headersMap.keyset()){ | |
req.setheader(ss,headersMap.get(ss)); | |
} | |
} | |
req.setEndpoint(EndPointURL); | |
req.setMethod(reqMethod); | |
if(reqBody!='' && reqBody!=null){ | |
req.setBody(reqBody); | |
} | |
return req; | |
} | |
public static HttpResponse performCallout(Httprequest req){ | |
Http h1 = new Http(); | |
HttpResponse resp = new HttpResponse(); | |
if(!Test.isRunningTest()){ | |
resp= h1.send(req); | |
} | |
return resp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment