I have a class below where I am inserting a new merchandise object and also creating an attachment to be attached with this merchandise object(in notes and attachment object) using a http POST . I am running into a JSON PARSE ERROR which says: Cannot deserialize instance of base64 from VALUE_STRING value when I test the below JSON from the workbench:

  "name" : "Pencil",
"id" : "a0e37000000lKep",
"attachs": [{
    "Body": "d29ybGQ=",
    "ContentType": "text/plain",
    "Name": "hello.txt"

Apex Class which creates a Merchandise object and attachment object:

global with sharing class MerchandiseManager {

    global static Merchandise__c getMerchandiseById() {
        RestRequest req = RestContext.request;        
        String merchId = req.requestURI.substring(
        Merchandise__c result = 
                       [SELECT Name,Description__c,Price__c,Total_Inventory__c
                        FROM Merchandise__c 
                        WHERE Id = :merchId];
        return result;

    global static String createMerchandise(String name,String id,List<Attachment> attachs) {
        Merchandise__c m = new Merchandise__c(
        insert m;

    list<attachment> attachmentToInsert = new list<attachment>();

    for (Attachment att :attachs) {
            new Attachment(parentId = m.Id, name = att.name, 
                ContentType = att.ContentType, Body = att.body));
        return m.Id;

What is going wrong here? How can I correct this to create a new merchandise object and attach the attachment to it?

1 Answer 1


I just tested serializing and deserializing an attachment in Salesforce, and JSON.serialize gave me something like this:


However, it wouldn't convert it back into an actual attachment, giving a similar error about not being able to handle base64 data.

This suggests to me that you're going to have to manually deserialize the values, perhaps like this:

global static String createMerchandise(String name,String id,List<Wrapper> attachs) {
// Skipping prior code
Attachment[] attachments = new Attachment[0];
for(Wrapper item: attachs) {
        new attachment(
insert attachments;

You'll have to adjust your JSON to match. Wrapper is just a plain class:

class Wrapper {
    String name, body, mimeType;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .