You need to sign in to do that
Don't have an account?
Arul Bernard I 14
How to get attachments from salesforce using java and REST web service
Hi,
I need to retrieve the attachment from Salesforce using java. Can anyone help me out with the solution
I need to retrieve the attachment from Salesforce using java. Can anyone help me out with the solution
Please try below code.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Attachment;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
public class AttachmentQuery {
public static void main(String[] args) throws NumberFormatException, IOException, URISyntaxException {
//Create a new connectionconfig to your Salesforce Org
ConnectorConfig sfconfig = new ConnectorConfig();
//Use your salesforce username = email
sfconfig.setUsername("yourusername");
//Use your saleforce password with your security token look like: passwordjeIzBAQKkR6FBW8bw5HbVkkkk
sfconfig.setPassword("passwordwithsecuritytoken");
EnterpriseConnection partnercon = null;
try {
// create a salesforce connection object with the credentials supplied in your connectionconfig
partnercon = Connector.newConnection(sfconfig);
QueryResult describeGlobalResult = partnercon.query("selectName, Body from Attachment where ContentType='application/pdf'");
System.out.println(describeGlobalResult.getRecords().length);
} catch (ConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------------
Please mark best if it helps you. Thanks in advance.
Regards,
Pawan Kumar
But actually. I want to know how to write Apex web service to retrieve an attachment
@RestResource(urlMapping='/Account/*')
global with sharing class AccountManager
{
@HttpGet global static Account getCAccountById()
{
RestRequest request = RestContext.request;
accId = request.requestURI.substring( request.requestURI.lastIndexOf('/')+1);
List<Attachment> att=[Select a.Id,a.ContentType,a.ParentId,a.Parent.Type,a.Parent.Name,a.BodyLength From Attachment ];
return result;
}