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
Hitesh chaudhariHitesh chaudhari 

How to add coloumn to list retrive from SOQL Query

Hi, 

I had executed a soql query and put the query output in  a list. Now I want to add a custom coloumn in the list.

Following is the way I am used it in code :

List<CustomObject> mylist = [select id,name,ipaddress from CustomObject];

I want to add a custom coloumn and display this all list in a table 

Can anyone suggest me how to achive this???

Best Answer chosen by Hitesh chaudhari
Chandrashekhar GoudChandrashekhar Goud

Vf page change Like this 

<apex:pageBlock title="Account List" id="acct">
            <apex:pageBlockTable value="{!accList}" var="acc" columns="5">
                <apex:column value="{!acc.Name}"/>
                <apex:column value="{!acc.Type}"/> 
                <apex:column  >
                    <apex:repeat  value="{!contList}" var="cont">
                        <apex:outputText value="{!cont.name}" ></apex:outputText>,
                    </apex:repeat>
                </apex:column>
            </apex:pageBlockTable>
</apex:pageBlock>
User-added image
see the  output

All Answers

Harsh Chandra 23Harsh Chandra 23
Hi Nick,

Adding new column to list is same as you are adding standard field. Example)
List<CustomObject> mylist = [select id,name,ipaddress, CustomField1__c, CustomField2__c  from CustomObject];

If you have to display the same in VF page then there are several ways to do like using "pageBlockTable", using apex:repeat etc.

Detail here:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_display_field_values.htm
https://salesforce.stackexchange.com/questions/161233/how-to-display-list-values-in-vf-page

Thanks,
Harsh
Chandrashekhar GoudChandrashekhar Goud

Hi

If you want to show a extra column then use this Code

Apex class:

public with sharing class sample
{

    public List<Account> acc {get;set;}
    public sample()
    {
        acc = [SELECT Name, AccountNumber FROM Account];
    }  
    
}

Vf page:

<apex:page controller="sample" sidebar="false" >
<apex:pageBlock title="Pageblock Table">
        <apex:pageblockTable value="{!acc}" var="a">
            <apex:column value="{!a.Id}"/>
            <apex:column value="{!a.Name}"/>            
            <apex:column value="{!a.AccountNumber }"
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>
Hitesh chaudhariHitesh chaudhari
The field I want to add is not a custom field realted to the object or this field contains data from another objects which i need to get from another query and merge with current record in the list.


 
Chandrashekhar GoudChandrashekhar Goud
Is there any relationship with this Object ?
Hitesh chaudhariHitesh chaudhari
Yes but that record is realted to multiple records so what I want is put a field from second object (which contians duplicate)and store that field details in String format and append this String to every record in the list.
Chandrashekhar GoudChandrashekhar Goud

see this code

public with sharing class AssociateController {

    public String selectedValue { get; set; }
    public List<String> strSearch { get; set; }  
    public List<Account> accList{get;set;}
    public List<Opportunity> oppList{get;set;}
    public List<Contact> contList{get;set;}
    
    public PageReference searchMethod() {
        
    accList = new List<Account>();
    oppList = new List<Opportunity>();
    contList = new List<Contact>();
    
    String searchItem = '*'+selectedValue+'*';
    
  
     List<Account> acc= [SELECT Name, (SELECT Name, Type FROM Opportunities),(SELECT Name, email FROM Contacts) FROM Account where Name = :searchItem];
    
   
    accList = acc;

     If(accList!=null && accList.size()>0){

         contList = new List<Contact>(acc[0].Contacts);

         oppList = new List<Opportunity>(acc[0].Opportunities);

    }
    return null; 
    }
    
    public AssociateController(){
    strSearch = new List<String>();
    
    for(Account a: [SELECT id, Name From Account]){
        strSearch.add(a.name);
    }
    
    
    }

    
}
<apex:page controller="AssociateController" docType="html-5.0">
<apex:form >
    <apex:inputText list="{!strSearch}" value="{!selectedValue}"/>
    <apex:commandButton value="Search Lead and Contact" action="{!searchMethod}" reRender="acct,cont,opprty" status="actStatus"/>
    
    <apex:actionStatus id="actStatus">
        <apex:facet name="start">
             <img src="/img/loading.gif"/>
        </apex:facet>
    </apex:actionStatus>
    
    <apex:pageBlock title="Account List" id="acct">
        <apex:pageBlockTable value="{!accList}" var="acc">
            <apex:column value="{!acc.Name}"/>
            <apex:column value="{!acc.Type}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    
    <apex:pageBlock title="Contacts" id="cont">
        <apex:pageblockTable value="{!contList}" var="con">
          <apex:column value="{!con.name}"/>
          <apex:column value="{!con.email}"/>
     </apex:pageblockTable>
    </apex:pageBlock>
    
    <apex:pageBlock title="Oppertunity List" id="opprty">
        <apex:pageBlockTable value="{!oppList}" var="opp">
            <apex:column value="{!opp.name}"/>
            <apex:column value="{!opp.StageName}"/>
        </apex:pageBlockTable>
    
    </apex:pageBlock>
   
 </apex:form>
 
</apex:page>
child to parent :
 
public class nbsp {
  
    public List<Contact> con {get;set;}
    public nbsp()
    {
        con = [SELECT Name, Languages__c ,account.Name,account.Website,account.AccountNumber FROM Contact];
    }  
}



<apex:page Controller="nbsp">
<apex:pageBlock title="Pageblock Table">
        <apex:pageblockTable value="{!con}" var="a">
            <apex:column value="{!a.account.Name}"/>
            <apex:column value="{!a.Name}"/>            
            <apex:column value="{!a.Languages__c }" />
             <apex:column value="{!a.account.AccountNumber }" />
             <apex:column value="{!a.account.Website }" />
            	
            	
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>


 
Hitesh chaudhariHitesh chaudhari
List<Account> acc= [SELECT Name, (SELECT Name, Type FROM Opportunities),(SELECT Name, email FROM Contacts) FROM Account where Name = :searchItem];


Is this type of queries crating separate row if inner query is giving output as multiple rows.

For example :
Your outer query based on account if there are 10 records present in account obj

and our inner queries (Contact and Opportunities) having multiple records then this query returns 10 records or more than that.


Also can you please provide output of your table for my referance.
Chandrashekhar GoudChandrashekhar Goud

Hi 

Output of the code Is:

User-added image
Inner queries will retrieve multiple records , you can see Contacts  multiple

Hitesh chaudhariHitesh chaudhari
Acutally  I want to all contact name in a single string seprated by ';'
That string will be deplayed in account cloumn with heading Related Contact.

it will be like this :
----------------------------------
Account Name || Type || Related Contact
Account 1         ||  T2    ||  Contact 1 ; Contact 2
Chandrashekhar GoudChandrashekhar Goud

Vf page change Like this 

<apex:pageBlock title="Account List" id="acct">
            <apex:pageBlockTable value="{!accList}" var="acc" columns="5">
                <apex:column value="{!acc.Name}"/>
                <apex:column value="{!acc.Type}"/> 
                <apex:column  >
                    <apex:repeat  value="{!contList}" var="cont">
                        <apex:outputText value="{!cont.name}" ></apex:outputText>,
                    </apex:repeat>
                </apex:column>
            </apex:pageBlockTable>
</apex:pageBlock>
User-added image
see the  output
This was selected as the best answer
Hitesh chaudhariHitesh chaudhari
it really helps. Thanks Chandrashekhar. you really give me great way to solve the problem
Hitesh chaudhariHitesh chaudhari
Hi Chandrashekhar,

One small question, why you created nbsp class. what is the role in this situation
Chandrashekhar GoudChandrashekhar Goud

Hi

No Bro ... Already nbsp class created in my org i have edited that class.
  
If You want exact same output use like this 

<apex:outputText value="{!cont.name}" ></apex:outputText>,&nbsp;