0

I am trying to insert attachment using the ajax toolkit but it will always run into the onfailure method and will always return remote invocation failed due to status code error ,the same code was working fine a few days back but suddenly now it wont work , Below is the code

function uploadattachment(parentId)
{       
    var input = document.getElementById('file-upload');

       var parentId = parentId;
       console.log('pId'+parentId);

    var filesToUpload = input.files;

    for(var i = 0, f; f = filesToUpload[i]; i++)
    {
        var reader = new FileReader();     

        // Keep a reference to the File in the FileReader so it can be accessed in callbacks
        reader.file = f; 

        reader.onerror = function(e) 
        {
            switch(e.target.error.code) 
            {
                case e.target.error.NOT_FOUND_ERR:
                    alert('File Not Found!');
                    break;
                case e.target.error.NOT_READABLE_ERR:
                    alert('File is not readable');
                    break;
                case e.target.error.ABORT_ERR:
                    break; // noop
                default:
                    alert('An error occurred reading this file.');
            };
        };     

        reader.onabort = function(e) 
        {
            alert('File read cancelled');
        };

        reader.onload = function(e) 
        {
            var att = new sforce.SObject("Attachment");
            //sforce.connection.serverUrl = '{!$Site.Prefix}/services/Soap/u/40.0';
            att.Name = this.file.name;
            att.ContentType = this.file.type;
            att.ParentId = parentId;
            alert("hello1" + parentId);

           var binary = "";
var bytes = new Uint8Array(e.target.result);
var length = bytes.byteLength;

for (var i = 0; i < length; i++) 
{
    binary += String.fromCharCode(bytes[i]);
}


            att.Body = (new sforce.Base64Binary(binary)).toString();
            console.log('yt'+att.Body);
             alert(att);          

             sforce.connection.create([att],
            {   
                onSuccess : function(result, source) 
                {
                    if (result[0].getBoolean("success")) 
                    {
                        console.log("new attachment created with id " + result[0].id);
                        alert(result[0].id + "success");
                    } 
                    else 
                    {
                        console.log("failed to create attachment " + result[0]);
                        alert("fail");
                        sforce.debug.open();

                    }
                }, 
                onFailure : function(error, source) 
                {
                    alert("ERROR"+error);
                    console.log("An error has occurred " + error+ result[0]);

                }
            });
        };

        var hello=reader.readAsArrayBuffer(f);
           }

    actionfunctionname();


    }

Can anyone please suggest what can be an isue here

Thanks

  • What is the status code? What does the error message say? We need more than just code to figure out what's going on. The more details you can give us, the better we can help you. – sfdcfox May 26 '18 at 13:53
  • The status code is always blank. I am trying to print the error through alert onFailure : function(error, source) { alert("ERROR"+error); console.log("An error has occurred " + error+ result[0]); } and it always says remote invocation failed due to status code : , status code is always blank – Natasha May 26 '18 at 13:57
  • 2
    Natasha, just because someone edited your question does not mean they have the ability or time to answer your question. Directly asking a specific person for help like you did is considered very rude. Also, parroting sfdcfox, details are important. If you're getting errors, don't try to paraphrase them. Include the entire text of the error(s), verbaitm, including the stack trace. – Derek F May 26 '18 at 13:57
  • the community user is trying to add attachments through ajax and we are passimg the parentid of the record on which the attachment needs to be inserted into the function , – Natasha May 26 '18 at 13:58
  • 1
    I'd recommend checking the Network tab of the Developer Console in your browser (F12). Most likely, it's a CORS error if there's no status code, but the console should tell you more. – sfdcfox May 26 '18 at 16:17

Your Answer

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

Browse other questions tagged or ask your own question.