0

The following class will get the content(a maximum of 10 MB) in encode format and I convert the content into decode format based on the my requirement into three parts. Now I want to add the all three blobs into single Blob to insert this content into attachment.

But while I am trying to add all three blobs into single blob, It is showing following error.

Even I gone through answer. But result comes without success.

Arithmetic expressions must use numeric arguments

Here is the class:

@RestResource(urlMapping='/stringoncase/*')
        global with sharing class RESTCaseController {
        @HttpPost
        global static String createNewCase(String Casenumber,String filename,String content1,String content2,String content3) {
                Case ca = [Select id,status,Origin from case where casenumber=:Casenumber];
            //ca.Status = status;
            //ca.Origin = web;
            //insert ca;

            blob base1 = EncodingUtil.base64Decode(content1);
            blob base2 = EncodingUtil.base64Decode(content2);
            blob base3 = EncodingUtil.base64Decode(content3);

            Attachment attachment = new Attachment();
            attachment.Body = Blob.valueOf(base1.toString() + base2.toString() + base3.toString());
            attachment.Name = filename;
            attachment.ParentId = ca.id;
            attachment.ContentType='multipart/form-data';//'application/json';
            insert attachment;

            return 'sucess';
        }
    }

I provide the input to the class using postman like the following

{

        "Casenumber" :"00001060",
        "filename" : "5_MBfiel.pdf",
        "content1" : "------Encoded data------",
        "content2" : "",
        "content3" : ""

}

How could I add these three blobs into single Blob?

5
  • I guess you cannot concatenate blob, you need to convert them to string. Before and after split the blob content must not be changed otherwise it would corrupt the data.
    – Raul
    Oct 26, 2017 at 11:04
  • Please edit your question and share some code using which we can reproduce the issue?
    – Raul
    Oct 26, 2017 at 11:16
  • @RahulSharma, I modified the question. Please check it.
    – KS Kumaar
    Oct 26, 2017 at 11:24
  • I meant how do you pass the parameters, do you do some encoding while calling it from a visualforce? You can get better support when someone can reproduce your error.
    – Raul
    Oct 26, 2017 at 11:30
  • @RahulSharma, I got your question. Now i updated my question again. I give content using rest service. Please go through it again.
    – KS Kumaar
    Oct 26, 2017 at 11:36

1 Answer 1

2

Assuming that you have in content1, content2, content3 Base64 representations of text (not binary content), and assuming you just want to generate an attachment with the concatenation of those 3 strings, you could do the following

public class CaseController {
    global static String createNewCase(String Casenumber,String filename,String content1,String content2,String content3) {
        Case ca = [Select id,status,Origin from case where casenumber=:Casenumber];

        blob base1 = EncodingUtil.base64Decode(content1);
        blob base2 = EncodingUtil.base64Decode(content2);
        blob base3 = EncodingUtil.base64Decode(content3);

        Attachment attachment = new Attachment();
        attachment.Body = Blob.valueOf(base1.toString() + base2.toString() + base3.toString());
        attachment.Name = filename;
        attachment.ParentId = ca.id;
        insert attachment;
    }
}

Assuming that content1, content2 and content3 are 3 chunks of the SAME valid file, but in Base64 format, you could do...

public class CaseController {
    global static String createNewCase(String Casenumber,String filename,String content1,String content2,String content3) {
        Case ca = [Select id,status,Origin from case where casenumber=:Casenumber];

        blob base123 = EncodingUtil.base64Decode(content1 + content2 + content3);

        Attachment attachment = new Attachment();
        attachment.Body = base123;
        attachment.Name = filename;
        attachment.ParentId = ca.id;
        insert attachment;
    }
}
2
  • I tried as you said, But giving the following error System.StringException: BLOB is not a valid UTF-8 string
    – KS Kumaar
    Oct 26, 2017 at 11:11
  • "Assuming that you have in content1, content2, content3 Base64 representations of (UTF-8) text, not binary content". Otherwise you cannot concatenate, because it would make no sense in terms of file structure. Oct 26, 2017 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.