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
Rahul Kumar DeyRahul Kumar Dey 

Rest API to insert attachments and associate this files with object(parentID)?

Hi guys,

I want to insert attachments and combine this attachments with custom object 'Grade__c' in related list using Rest API web service.
I have created webservice which insert Grade object records but unable to insert attachment at same record-

Here is my code -
@RestResource(urlMapping='/UploadAttachmentRestService/*')
global with sharing class UploadAttachmentRestAPI {
    //In this method we insert custom object(Grade__c) records with attachments, comes from response body
    @httpPost
    global static Id insertGrade(String dt, Integer score, String name){
        Grade__c gradeObj = new Grade__c();
        gradeObj.Date__c = dt;
        gradeObj.Score__c = score;
        gradeObj.Name = name;
        
        insert gradeObj;
        system.debug('Grade ID->>>>'+gradeObj.Id);
        return gradeObj.Id;
       
    }

request body that i pass from workbench -
{
   "dt":"2016-12-10", 
   "score":9876, 
   "name":"Test Grade"
}

And the 'Test Grade' will Inserted.

any solution for attachment part, much appreciate.

Regards,
Rahul 
Best Answer chosen by Rahul Kumar Dey
David Zhu 🔥David Zhu 🔥

The ways of handling Attachment in Classic and Lightning are different, they use different objects. You may tweak the following snipet in Lightning. Consequently, your  request body will have to modified accordingly.
 

global static Id insertGrade(String dt, Integer score, String name,string attachmentName,string blobString){
        Grade__c gradeObj = new Grade__c();
        gradeObj.Date__c = dt;
        gradeObj.Score__c = score;
        gradeObj.Name = name;
        
        insert gradeObj;
        system.debug('Grade ID->>>>'+gradeObj.Id);


       ContentVersion contentVer = new ContentVersion();
       contentVer .ContentLocation = 'S'; 
       contentVer .PathOnClient = attachmentName;
       contentVer .Title = attachmentName;
       contentVer .VersionData = blob.valueOf(blobString); 
       contentVer .FirstPublishLocationId  = gradeObj.Id;

        insert contentVer ;

        return gradeObj.Id;
       
    }

All Answers

David Zhu 🔥David Zhu 🔥

The ways of handling Attachment in Classic and Lightning are different, they use different objects. You may tweak the following snipet in Lightning. Consequently, your  request body will have to modified accordingly.
 

global static Id insertGrade(String dt, Integer score, String name,string attachmentName,string blobString){
        Grade__c gradeObj = new Grade__c();
        gradeObj.Date__c = dt;
        gradeObj.Score__c = score;
        gradeObj.Name = name;
        
        insert gradeObj;
        system.debug('Grade ID->>>>'+gradeObj.Id);


       ContentVersion contentVer = new ContentVersion();
       contentVer .ContentLocation = 'S'; 
       contentVer .PathOnClient = attachmentName;
       contentVer .Title = attachmentName;
       contentVer .VersionData = blob.valueOf(blobString); 
       contentVer .FirstPublishLocationId  = gradeObj.Id;

        insert contentVer ;

        return gradeObj.Id;
       
    }
This was selected as the best answer
Rahul Kumar DeyRahul Kumar Dey
Hi David,

Thanks you so much for your reply, one more thing is- how to add an attachment with "Test Grade" in json body when I post from workbench.

Let say I want to attach an "Test.txt" file with - 
{
   "dt":"2016-12-10", 
   "score":9876, 
   "name":"Test Grade"
}

Actually I don't have any idea how to do it, if any help?

Thank You!!
Rahul
David Zhu 🔥David Zhu 🔥
I take Apex code as a example. It is similar with other programming language.
1. read file into a blob variable
2. string paramvalue = EncodingUtil.base64Encode(blobVariable);   //Use Base64 convert blob into string
   the result is like:  paramvalue = "JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwog"
3. The payload is built like below.

{
   "dt":"2016-12-10", 
   "score":9876, 
   "name":"Test Grade"
   "attachmentName":"Test.txt"
   "blobString":"JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwog"
}

This is minor change in inserGrade method line 15:

contentVer .VersionData = EncodingUtil.base64Decode(blobString);   //decode string to base64
Rahul Kumar DeyRahul Kumar Dey
Thank you so much @David, it works...

User-added image