+ Start a Discussion
Mark VermaatMark Vermaat 

Having an issue with a wrapperClass along with Auto page update

Hey Everyone, 

 

I am building an Attachment Manager Type of application for my org.  I am still rather new at the apex advanced stuff and I am stuck.

 

Most of the code is good.  I am getting an error: Illegal assignment from LIST<SObject> to LIST<attachmentWrapperCont.aAttachment>

If I do not do it this way the wrapperclass does not work.  I need it to work so I can get the Selected attachments in there own list.

 

If any can give a suggestion on how to move forward that would be great.

 

Here is the code from my Controller.

public with sharing class attachmentWrapperCont {
	
	public string AttachmentName = Apexpages.currentPage().getParameters().get('Name');
	public string AttachmentType = Apexpages.currentPage().getParameters().get('contentType');
	private string qry {get;set;}
	public boolean selected {get;set;}
	
	//Collection of the Class/wrapper objects
	public List<aAttachment> attachmentList {get; set;}
	
	public class aAttachment{
		public Attachment attach {get;set;}
		public boolean selected {get;set;}
		
		//constructor
		public aAttachment(Attachment a){
			attach = a;
			selected = false;
		}
		
	}
	
	public void runQuery(){
		try{
			attachmentList = Database.query(qry = 'order by '+ sortField+ ' ' + sortDir + ' limit 50');
		}catch (Exception e){
			ApexPages.addMessage(new Apexpages.Message(Apexpages.severity.ERROR, 'Oops'));
		}
		//return attachmentList;
	}
	
	public PageReference processSelected(){
		//new list of attachments that are selected.
		List<Attachment> selectedAttachments = new List<Attachment>();
		
		//cycle through list to check for selected
		for(Attachment att: attachmentList()){
			if(att.selected == true){
				selectedAttachments.add(att);
			}
		}
		
		//List of selected attachments and can do any logic on it
		System.debug('These are the selected Attachments...');
		for(Attachment attach: selectedAttachments){
			system.debug(attach);
		}
		attachmentList = null;
		
		return null;
	}
	
	public PageReference search(){
		qry = 'SELECT id, name, ContentType, bodyLength FROM Attachment WHERE AttachmentName != nill';
		if(!AttachmentType.equals(''))
			qry += ' AND attachmentType LIKE \'%' +String.escapeSingleQuotes(AttachmentType)+'%\'';
		if(!AttachmentName.equals(''))
			qry += ' AND attachmentName LIKE \'%' +String.escapeSingleQuotes(AttachmentName)+ '%\'';
		runQuery();
		
		return null;
	}
	
	//Wrapper/container class In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
	
	
	
	public string sortDir{
		get{
			if(sortDir == null){
				sortDir = 'asc';
			}
			return sortDir;
		}
		set;
	}
	
	public string sortField {
		get{
			if(sortField == null){
				sortField = 'AttachmentName';
			}
			return sortField;
		}
		set;
	}
	
	
	
}

 Any help would be greatly appreciated.

 

Thanks a ton in advance....

jungleeejungleee

Hi Mark

 

There's a change in the runquery() and the processselectecd() methods. You're querying the attachment object  but then you're assigning the returned result which is a list of attachment object to a list of the wrapper class(aAttachment) which is not allowed. I have made the changes in the below methods and also added a VF page block on how to render this. Hope this helps

 

public void runQuery(){
		try{
			//attachmentList = Database.query(qry = 'order by '+ sortField+ ' ' + sortDir + ' limit 50');
			list<Attachment> attachObject = Database.query(qry = 'order by '+ sortField+ ' ' + sortDir + ' limit 50');
			for(attachment a: attachObject){
				adding the attachment to the wrapper class list.
				attachmentList.add(new aAttachment(a));
			}
		}catch (Exception e){
			ApexPages.addMessage(new Apexpages.Message(Apexpages.severity.ERROR, 'Oops'));
		}
		//return attachmentList;
	}
	
	public PageReference processSelected(){
		//new list of attachments that are selected.
		List<Attachment> selectedAttachments = new List<Attachment>();
		
		//cycle through list to check for selected
		for(aAttachment att: attachmentList){
			if(att.selected == true){
				//add the attachement object to the list.
				selectedAttachments.add(att.attach);
			}
		}
		
		//List of selected attachments and can do any logic on it
		System.debug('These are the selected Attachments...');
		for(Attachment attach: selectedAttachments){
			system.debug(attach);
		}
	}

 

<apex:pageBlockTable value="{!attachmentList}" var="att">
		<apex:column headerValue="select">
			<apex:inputCheckbox value="{!att.selected}"/>
		</apex:column>
		<apex:column value="{!att.attach.name}"/>
	</apex:pageblockTable>

  -ಸಮಿರ್

 

 

Mark VermaatMark Vermaat

Hey Jungleee,

 

Thanks for the response, it helps a fair bit,  however I am not able to refer to the AAttachment Class now.  When I set everything up with the few changes you suggests, it says Invalid field attach for SObject Attachment.

 

It says the issue is in the

 

selectedAttachments.add(att.attach);  

 

Any other suggestions?

 

Thanks again for you answers they have been great.