function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aditya MohanAditya Mohan 

List of Attachments returned to Custom Button has no value in Javascript

I am trying to access a list of Attachments related to a Contact from Custom Button during OnClick Javascript in a Javascript Array. But, the array doesn't display any value. It's blank.

Apex Class:
global class ZipAndSendAttachments {
webservice static List<AttachmentWrapper> getAttachments(String contactId)  {
    List<Attachment> lstAttachments = new List<Attachment>();
    lstAttachments = [SELECT Id, Name, ContentType, Body, ParentId
                      FROM Attachment
                      WHERE ParentId = :contactId];

    List<AttachmentWrapper> attWrapper = new List<AttachmentWrapper>();
    AttachmentWrapper a;
    for(Attachment att : lstAttachments){
        a = new AttachmentWrapper();
        a.attName = att.Name;
        a.attEncodedBody = EncodingUtil.base64Encode(att.Body);
        attWrapper.add(a);
    }

    System.debug('attWrapper '+attWrapper);
    return attWrapper;
}

global class AttachmentWrapper {
    public String attEncodedBody {get; set;}
    public String attName {get; set;}
}}

Javscript Button:
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
    {!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
    {!REQUIRESCRIPT("/resource/JSZip/Stuk-jszip-3a39a71/dist/jszip.min.js")} 

    var queryResult = [];
    var query = "SELECT Id, ParentId FROM Attachment WHERE ParentId = '{!Contact.Id}'"; 
    queryResult.push(sforce.connection.query(query)); 
    if(queryResult != null)
    {result = sforce.apex.execute("ZipAndSendAttachments","getAttachments",{contactId:"{!Contact.Id}"});
console.log('result::' + result);
    }

 
Arunkumar RArunkumar R
Hi Aditiya,

Can you try the below one,
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
{!REQUIRESCRIPT("/resource/JSZip/Stuk-jszip-3a39a71/dist/jszip.min.js")} 

var attachments = sforce.connection.query("SELECT Id, ParentId FROM Attachment WHERE ParentId = '{!Contact.Id}'"); 
records = attachments.getArray("records"); 

if(records.length > 0) 
{
var result = sforce.apex.execute("ZipAndSendAttachments","getAttachments",{contactId:"{!Contact.Id}"});
console.log('result::' + result);
}

 
Aditya MohanAditya Mohan

Hi Arun,
Thanks for your reply.
We need to get attachments while after invoking getAttachments function. That is: 

var result = sforce.apex.execute("ZipAndSendAttachments","getAttachments",{contactId:"{!Contact.Id}"});
console.log('result::' + result);

Here the result is blank, instead of attachments list
Arunkumar RArunkumar R
Hi Aditya,

  Since already you have queried attachment in button itself and checked the list size there. Again you are doing same in class and adding it to wrapper instance.

could you try and see the response in alert box,
 
alert('result::' + result);

What exactly you are trying to display on console.log?