function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mhlangano KhumaloMhlangano Khumalo 

How to insert a record with multiple attachments in SFDC using REST

Below is my class.
@RestResource(urlMapping='/V1/IndividualKYC/*')
global with sharing class IndividualKYCManager {

  //Create
  @HttpPost
    global static String createIndividualKYC(String a,Boolean termsandconditions,String phone, 
    String idnum, String name,String photo, String email, ID entity, String address1, String address2, String suburb, String nationality, Integer postcode, String summary) {
       
        Individual_KYC__c ikyc= new Individual_KYC__c ();
            
        ikyc.Name=name;
        ikyc.Email__c=email;
        ikyc.ID_Number__c=idnum;
        ikyc.Phone__c=phone;
        ikyc.Terms_and_Conditions__c = termsandconditions;
        ikyc.Address_Line_1__c = address1;
        ikyc.Address_Line_2_Optional__c= address2;
        ikyc.Suburb__c= suburb;
        ikyc.Nationality__c= nationality;
        ikyc.Post_Code__c= postcode;
        ikyc.Profile_Summary__c= summary ;
        
        string before = photo;
        Blob beforeblob = Blob.valueOf(before);
        string paramvalue = EncodingUtil.base64Encode(beforeblob);
        
        ikyc.Photo__c = paramvalue ; 
        insert ikyc;
        String ikycid=ikyc.id;
        return ikycid;
          
    }
    
    global void createAtt(String  ikycid)
    {
    
      String pId=ikycid;
        
     
     string before2 = 'Testing base 64 encode';
     Blob beforeblob = Blob.valueOf(before2);
     string paramvalue = EncodingUtil.base64Encode(beforeblob);
     //System.debug(before2 + ' is now encoded as: ' + paramvalue);
     
         Attachment attachmet = new Attachment (ParentId = pId,
                                           Body = EncodingUtil.base64Decode(paramvalue),
                                           ContentType = 'application/vnd.ms-excel',
                                           Name = 'SendViaMyPhone');
          insert attachmet ;
    
    }
}
I want to insert a record & create 2 attachments in one POST. The challenge is wont allow me to have 2 inserts (ikyc & attachmet) in one method (createIndividualKYC), which is why i've split them. Im looking for a way to insert a record with 2 attachements (pdf. & jpg.) In one POST.
 
Best Answer chosen by Mhlangano Khumalo
Mhlangano KhumaloMhlangano Khumalo
Thanks @pcon . Ive figured it out.
 
@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 = Blob.valueOf(att.base64),            
            ContentType = 'image/jpg',
            ParentId = ikyc.id
        ));
        }
        insert attachments;
        return ikycid;    
    }
}
And the query in JSON looks like this
 
{
"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": "/AAQSkZJRgABAQEAYABgAAD/4RFARXhpZgAATU0AK",
"type": "id",
"base64": "4AAQSkZJRgAB/4RFARXhpZgAATU0AK"

}
}


 

All Answers

pconpcon
What I would do is to include the base 64 encoded attachment to the post your createIndividualKYC method. And then have that method create the object and then create the attachments afterwords.

What do you mean "the challenge?"
Mhlangano KhumaloMhlangano Khumalo
Thanks @pcon . Ive figured it out.
 
@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 = Blob.valueOf(att.base64),            
            ContentType = 'image/jpg',
            ParentId = ikyc.id
        ));
        }
        insert attachments;
        return ikycid;    
    }
}
And the query in JSON looks like this
 
{
"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": "/AAQSkZJRgABAQEAYABgAAD/4RFARXhpZgAATU0AK",
"type": "id",
"base64": "4AAQSkZJRgAB/4RFARXhpZgAATU0AK"

}
}


 
This was selected as the best answer