top of page

Creating an httpPost with multiple files using Rest API.

In this post I'll be creating a record in Salesforce with file attachements using Rest api.

The first step is to create a connected app in SF.

Upon clicking save, a Consumer Key and Consumer Secret will be generated which will be used to authenticate access to SF.

Now we should create a class which will allow post. The standard controller object for this class is a custom object called Individual_KYC__c.

@RestResource(urlMapping='/V1/IndividualKYC/*') global with sharing class IndividualKYCManager { global class KYCRequest { String name {get; set;} String email {get; set;} String idnum {get; set;} String phone{get; set;} Boolean termsandconditions {get; set;} String address1 {get; set;} String address2 {get; set;} String suburb {get; set;} String nationality {get; set;} String postcode {get; set;} String summary {get; set;} List<KYCRequestAttachment> attachments {get; set;} }

global class KYCRequestAttachment { String type {get; set;} String base64 {get; set;} } //Create @HttpPost global static String IndividualKYCManager (KYCRequest request){ Individual_KYC__c ikyc=new Individual_KYC__c(); ikyc.Name = request.name; ikyc.Email__c = request.email; ikyc.Phone__c =request.phone; ikyc.ID_Number__c = request.idnum; ikyc.Terms_and_Conditions__c = request.termsandconditions; ikyc.Address_Line_1__c = request.address1; ikyc.Address_Line_2_Optional__c = request.address2; ikyc.Suburb__c = request.suburb; ikyc.Nationality__c = request.nationality; ikyc.Post_Code__c = request.postcode; ikyc.Profile_Summary__c = request.summary; insert ikyc; String ikycid=ikyc.id; Attachment[] attachments = new Attachment[] {}; for (KYCRequestAttachment att : request.attachments) { attachments.add(new Attachment( Name = att.type, Body = EncodingUtil.base64Decode(att.base64),

ContentType = 'application/vnd.ms-excel', ParentId = ikyc.id )); } insert attachments; return ikycid; } }

The class receives the attachment as a Base64 String an converts it into an attachment.

Now lets do Authentication

If authentication is successful, an access token will be generated. Then insert the following JSON in the body. We to use a valid base 64 string. To create one I use this tool. Also set the authentication and content-type keys in the headers.

Header

{ "request" : {

"name" : "John Smith",

"email" : "bsmith@gmail.com",

"idnum" : "8512055039087",

"phone" : "083 485 4565",

"termsandconditions" : true,

"address1" : "39 Zeiss road, Honeydew",

"address2" : "67 Jan Smuts ave, Randburg",

"suburb" : "Honeydew",

"nationality" : "South Africa",

"postcode" : "9834",

"summary" : "Bob Smith is a director at bm investments",

"attachments" : [

{

"type": "id",

"base64": "==sdfghjklxcvbnmefghj"

},

{

"type": "id",

"base64": "==sdfghjklxcvbnmefghj"

}

]

} }

If successful it will return the record id. which can be used again to update the same record on SF.

Refreshing the page will show a new record. Whats great is you can display the attachment like i've done on the pic below using a Visualforce page Frame.

Record in Salesforce.

Hope this helps.


Featured Posts
Recent Posts
Archive
Search By Tags
No tags yet.
Follow Me
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page