You need to sign in to do that
Don't have an account?
Rahul 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
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
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.
All Answers
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.
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
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