In this blog we will learn how to insert file type attachment in salesforce by Rest API with the help of workbench :
There is basically four subfields of file field in salesforce :
- FieldName__Name__s : Name of the attachment file.
- FieldName__Body__s : Body of the attachment file.
- FieldName__Length__s : Length of the attachment file.
- FieldName__ContentType__s : Type of the attachment file. It is a picklist field which means it accepts only the values that are listed in picklist values. It has values like :
application/html
application/java-archive
application/javascript
application/msword
application/octet-stream
application/octet-stream;type=unknown
application/pdf .... etc....
When inserting attachment with rest api only three subfields are required Name, Body and ContentType. Length will automatically generated after insertion of record. In The place of Body__s subfield you have to give Base64 of that file which you want to insert. In the place of Name__s you have to give the name of the file and In the place of ContentType__s you have to give the type of that file. Let's get Start :
Create a record with attachment :
Here i am creating one record with and inserting text file as attachment :
Method :- Post
URL:- /services/data/v50.0/sobjects/Knowledge__kav
Request Body :-
In the place of Attachment__Body__s , I am giving converted base64 of text file.
Response :-
Record Created:-