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
Naveen Rahul 3Naveen Rahul 3 

combine two different standard objects into single LIST

Hi all,

came up with a doubt,can i combine two different standard objects into single LIST

ie

List<contact> listname =  List<contact>;
List<campaignmember> listname =  List<campaignmember>;

i need to combine above two List into single one may be like (contact.campaignmember.date,contact.campaignmember.name)

i wrote a wrapper where i have access to those two list of data.now just need to combine  and return them as list ,is that possible ??.

please post possible ideas to do that.

 

Thanks
D Naveen Rahul.

RamuRamu (Salesforce Developers) 
Hi Naveen, since you wrote the wrapper class, you can create the list of wrapper class. Example if the wrapper class name is contactwrapper, you can create a list of type List<contactwrapper>. Please review the below article for clear understanding

http://www.minddigital.com/what-is-wrapper-class-and-how-to-use-it-in-salesforce/
Venkat PolisettiVenkat Polisetti
Naveen,

You cannot put to different objects into a list that is defined to have a specific type. You can put them together in an inner class and have it added to the list.
 
/* this is an inner class */
class MemberContact {
     CampaignMember member {get;set;}
     Contact contact {get;set;};
}

List<MemberContact> memberContacts = new List<MemberContact();

MemberContact m = new MemberContact();

m.member = yourmembervariable;
m.contact = yourcontactvariable;

memberContacts.add(m);

I am wondering if you are trying to pull the Campaign members who are also contacts, is that so?

You can get the list directly from the SOQL.
 
CampaignMember[] members = [select	Id, Name, ContactId, Contact.FirstName, 
									Contact.LastName, Contact.Email 
							from	CampaignMember
							where	CampaignId = :yourCampaignId];

/* you can access campaign member and contact like this */

for (CampaignMember c : members) {
	system.debug('Member Id = ' + c.Id);
	system.debug('Contact Email = ' + c.Contact.Email);
}

 
Yoganand GadekarYoganand Gadekar
You would need to use wrapper class to build such a thing.
Here is a article that will help you understand it better.
http://www.cloudforce4u.com/2013/06/how-to-use-wrapper-class-in.html