4

I have a Javascript code which is used to call the method of a Controller using the @RemoteAction.

This is my Javascript coding

var arr = new Array('123', '234');
arr.splice(0, 1);
arr.splice(0, 1);
processByteChunkArray(arr);
// Here i tried this to make sure the empty array is passing or not

function processByteChunkArray(getResult) {
    FileUploadController.attachBlob(getResult, function(result, event) {
        //Proceed if there were no errors with the remoting call
        if (event.status == true) {
            alert(result);//This alert is coming
            getResult = JSON.parse(result);
            alert(getResult);//This alert is NOT coming
            processByteChunkArray(getResult);
        } else {
            checkForUploads();
        }
    });
}

This is Controller Coding

global static List<Attachment> attachList = new List<Attachment>();
@RemoteAction
global static List<Attachment> attachBlob(List<Attachment> getResult) {
    Attachment att = new Attachment(
        ParentId = parentId,
        Body = EncodingUtil.Base64Decode(base64BlobValue),
        Name = fileName,
        ContentType = contentType
    );
    if (getResult.size() > 0) {
        for (Attachment atm: getResult) {
            try {
                atm.Body = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(atm.Body) + base64BlobValue);
                attachList.add(atm);
            } catch (Exception e) {
                System.debug('This is in IF CATCH Condition===>' + e.getMessage());
            }
        }
        return JSON.serialize(attachList);
    } else {
        try {
            attachList.add(att);
        } catch (Exception e) {
            System.debug('This is in ELSE CATCH Condition' + e.getMessage());
        }
        return JSON.serialize(attachList);
    }
}

And now my problem is, the List that i am returning on the Controller Method is not coming to Javascript. I tried the Same coding returning the STRING and it is working perfectly. Why not List<Attachment>.

I tried returning both List<Attachment> and Attachment[] but with no result in the Javascript.
Is there any difference between List and Array?

UPDATE:

I updated the coding according to the JSON Serialize and parse. The Serialize is working but the parse is not working. Is there any way to parse the String to an array again and i need to store that array in the getResult variable

Please help...

3
  • possible duplicate: Get List Values in JavaScript Remoting
    – Mr.Frodo
    Jul 13, 2016 at 5:51
  • How are you passing Parentid,filename.. values?
    – Praveen
    Jul 13, 2016 at 5:54
  • @Praveen, I just posted what is necessary of my problem... There is a whole bunch of code actually. But i am sure my question explains my problem..
    – Subhash
    Jul 13, 2016 at 6:11

2 Answers 2

7

I suggest return the JSON string from method like.. first serialize the list and return

global static String attachBlob(List<Attachment> getResult) {

   // your code 

   return JSON.serialize(attachList);
}

I am not sure but I guess you may need to parse the javascript variable ie. result

like this JSON.parse(result). this will give you the array

8
  • 1
    Sounds perfect :) Jul 13, 2016 at 6:09
  • @Ratan, Thanks for the response, Your JSON.serialize(attachList); is returning and even showing in alert. But i want to assign that result to the same getResult in the Javascript Coding. While i am using the JSON.parse(result) it is not restponding and just strucks the coding in javascript... Is there any way to parse the result in javascript?
    – Subhash
    Jul 13, 2016 at 6:14
  • @Subhash JSON.parse(result) this retrun JavaScript array. what do you want? javascript array or simple JSON string ? can you show me the processByteChunkArray method structure
    – Ratan Paul
    Jul 13, 2016 at 6:18
  • Hi @Ratan, I updated my question according to your answer and asked the question too.. Can you help me plz...
    – Subhash
    Jul 13, 2016 at 6:25
  • @Subhash parsing the json string to array JSON.parse() should work. you may be getting issue in further steps.. can you add some detail about the processByteChunkArray method \
    – Ratan Paul
    Jul 13, 2016 at 6:28
2

In addition to @Ratan answer, you need to explicitly set the escape attribute to false which is true by default.

FileUploadController.attachBlob(getResult, function(result, event) {
        //Proceed if there were no errors with the remoting call
        if (event.status == true) {
            alert(result);//This alert is coming
            getResult = JSON.parse(result);
            alert(getResult);//This alert is NOT coming
            processByteChunkArray(getResult);
        } else {
            checkForUploads();
        }
    },{escape:false});
8
  • Hi @Praveen, I ended with following error...Visualforce Remoting Exception: No such column 'attributes' on sobject of type Attachment....It is taking the attributes as a field in attachment object
    – Subhash
    Jul 13, 2016 at 10:03
  • It looks like you should remove the attributes property from each array element by iterating it an for-loop.
    – Praveen
    Jul 13, 2016 at 10:19
  • how to do that...? And it should not get override afterwards, because i am sending the same array to the controller method again and again until the whole file is entered into a List...
    – Subhash
    Jul 13, 2016 at 10:23
  • i don't think iterating over the attachment array element getResult and removing unnecessary properties like attributes would solve your issue.Since the body of the attachment expects an BLOB ,it would throw an error VFRemote.js:117 Visualforce Remoting Exception: Internal Service Error: java.lang.UnsupportedOperationException
    – Praveen
    Jul 13, 2016 at 10:35
  • It is better to create a List<AttachWrapper> wrapper class instead of List<Attachment> to hold Attachment and its body separately.where body should contain the base64 encoded string and rest of the info like ParentId,ContentType...
    – Praveen
    Jul 13, 2016 at 10:43

You must log in to answer this question.

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