• Jennifer Dos Reis ICO
  • NEWBIE
  • 5 Points
  • Member since 2015
  • Developer
  • ICO Group

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies

I was doing the Trailhead when in the module of "Using sObjects" one of the right answers is "A generic sObject variable can be assigned to any specific sObject, standard or custom. Such as Account or Book__c." But I think this answer is wrong or I`m not understanding very well the question.

With the frase "A generic sObject variable can be assigned to any specific sObject" I understand something like this:

sObject a = new Account();  // This is a generic sObject variable

Contact b = new Contact(); // This is a specific sObject

// A generic sObject variable can be assigned to any specific sObject 

b = (Account) a;  // Error


I understand that the last statement is an error because b is a variable of Contact type no of Account type. For this reason i'm not agree with the frase "A generic sObject variable can be assigned to any specific sObject". 

Am I missing something ? 

Thanks for your help!

I would like to query all users with their related list of a custom object (By the owner relationship). Because I would like to count how many records belongs to each user. For example:
 
[ SELECT Name, (SELECT Count() FROM relationshipName) FROM CustomObject ]

But I don't know which is the relationshipName that i should use. I don't want to create a new lookup field, I would like to use the owner relationship for this. Is it possible ? If not, is there a better way to achieve my purpose?  

Sometimes we need to build a table for showing grouped records by a particular field. Here I show one way to accomplish this. 

For example, suppose we want to show all contacts grouped by company as shown in the following image.

Table of Contacts grouped by Account

In order to group cells in a html table we could use the attribute rowspan but we need to know how many records will be grouped in each Account. For this we could use a controller that allow us order and manage the records that will be shown in the Visualforce page.
 
Here is an example that I made for the visualforce and Apex Code. 

<apex:page standardController="Contact" extensions="contactsByAccount_extension" >

  <table border="1" cellspacing="0" width="60%">
    <thead>
        <th> Account </th>
        <th> Title </th>
        <th> Name </th>
        <th> Email </th>
    </thead>
       
    <apex:repeat value="{!contactsByAccount}" var="key" >
      
      <apex:repeat value="{!contactsByAccount[key].contactList}" var="keyvalue" > 
        <tr> 
           <td rowspan="{!contactsByAccount[key].numOfContacts}" style="display:{!IF(CASESAFEID(keyvalue.id)==CASESAFEID(contactsByAccount[key].firstOfList), 'table-data','none' )};"> {!keyvalue.Account.name} </td>
          <td> {!keyvalue.Title} </td>
          <td> {!keyvalue.Name} </td>
          <td> {!keyvalue.Email} </td>  
        </tr>
      </apex:repeat>
      
    </apex:repeat> 
  </table>
    
</apex:page>

In the Visualforce page we iterate through a Map that comes from the extension controller. This Map group the records by Account. So for each Account we also iterate through the contacts of that Account.

The first cell, that show the name of the Account ONLY should be displayed in the first iteration, and it's not recomended to use apex variables in an apex:repeat, so a possible solution for this is making a method in the controller that let us know if the record is the first of the list. Thus we use in the style attribute of the <td> tag a conditional for display the cell. If the record ID of the current record is equal to the first of the list then display the account name. The rest of the cells are displayed without condition. 

Let's see the extension controller now.

public class ContactsByAccount_extension {

    public ContactsByAccount_extension(ApexPages.StandardController controller) {

    }
    
    public Map<String,contactosListWrapper> getContactsByAccount(){
    
      List<Contact> result = [SELECT Account.name, Title, Name, Email FROM Contact ];
    
      // Group Contacts by Account                                
      Map<String,contactosListWrapper> contactsByAccount = new Map<String,contactosListWrapper>();
      for(Contact cont: result){
        if(null == cont.Account.name) continue;
        contactosListWrapper empresa = contactsByAccount.get(cont.Account.name);
        if(null == empresa){
            contactsByAccount.put(cont.Account.name, new contactosListWrapper(new List<contact>()) );    
        }
        contactsByAccount.get(cont.Account.name).contactList.add(cont);
      }
      
      return contactsByAccount;
    }
    
   // List of contacts and details  
   class contactosListWrapper {
       
       public List<Contact> contactList {get; set;}
       
       public Integer numOfContacts {
          get{
            return contactList.size();
          }
          set;
       }
       
       public Id firstOfList{
          get{
            return contactList[0].Id;
          }
          set;
       }
             
       public contactosListWrapper(List<contact> listContacts){
           contactList = listContacts;
           
       }

The inner class contactosListWrapper is a container of a contact list and give us information about wich is the first and how many of them are. 

The extension controller method getContactsByAccount() makes a query of all the contacts and then they are group in the map by Account. For each Account we make a new entry in the Map with the name of the Account (String) and the list of contacts (contactosListWrapper). 

Sometimes we need to build a table for showing grouped records by a particular field. Here I show one way to accomplish this. 

For example, suppose we want to show all contacts grouped by company as shown in the following image.

Table of Contacts grouped by Account

In order to group cells in a html table we could use the attribute rowspan but we need to know how many records will be grouped in each Account. For this we could use a controller that allow us order and manage the records that will be shown in the Visualforce page.
 
Here is an example that I made for the visualforce and Apex Code. 

<apex:page standardController="Contact" extensions="contactsByAccount_extension" >

  <table border="1" cellspacing="0" width="60%">
    <thead>
        <th> Account </th>
        <th> Title </th>
        <th> Name </th>
        <th> Email </th>
    </thead>
       
    <apex:repeat value="{!contactsByAccount}" var="key" >
      
      <apex:repeat value="{!contactsByAccount[key].contactList}" var="keyvalue" > 
        <tr> 
           <td rowspan="{!contactsByAccount[key].numOfContacts}" style="display:{!IF(CASESAFEID(keyvalue.id)==CASESAFEID(contactsByAccount[key].firstOfList), 'table-data','none' )};"> {!keyvalue.Account.name} </td>
          <td> {!keyvalue.Title} </td>
          <td> {!keyvalue.Name} </td>
          <td> {!keyvalue.Email} </td>  
        </tr>
      </apex:repeat>
      
    </apex:repeat> 
  </table>
    
</apex:page>

In the Visualforce page we iterate through a Map that comes from the extension controller. This Map group the records by Account. So for each Account we also iterate through the contacts of that Account.

The first cell, that show the name of the Account ONLY should be displayed in the first iteration, and it's not recomended to use apex variables in an apex:repeat, so a possible solution for this is making a method in the controller that let us know if the record is the first of the list. Thus we use in the style attribute of the <td> tag a conditional for display the cell. If the record ID of the current record is equal to the first of the list then display the account name. The rest of the cells are displayed without condition. 

Let's see the extension controller now.

public class ContactsByAccount_extension {

    public ContactsByAccount_extension(ApexPages.StandardController controller) {

    }
    
    public Map<String,contactosListWrapper> getContactsByAccount(){
    
      List<Contact> result = [SELECT Account.name, Title, Name, Email FROM Contact ];
    
      // Group Contacts by Account                                
      Map<String,contactosListWrapper> contactsByAccount = new Map<String,contactosListWrapper>();
      for(Contact cont: result){
        if(null == cont.Account.name) continue;
        contactosListWrapper empresa = contactsByAccount.get(cont.Account.name);
        if(null == empresa){
            contactsByAccount.put(cont.Account.name, new contactosListWrapper(new List<contact>()) );    
        }
        contactsByAccount.get(cont.Account.name).contactList.add(cont);
      }
      
      return contactsByAccount;
    }
    
   // List of contacts and details  
   class contactosListWrapper {
       
       public List<Contact> contactList {get; set;}
       
       public Integer numOfContacts {
          get{
            return contactList.size();
          }
          set;
       }
       
       public Id firstOfList{
          get{
            return contactList[0].Id;
          }
          set;
       }
             
       public contactosListWrapper(List<contact> listContacts){
           contactList = listContacts;
           
       }

The inner class contactosListWrapper is a container of a contact list and give us information about wich is the first and how many of them are. 

The extension controller method getContactsByAccount() makes a query of all the contacts and then they are group in the map by Account. For each Account we make a new entry in the Map with the name of the Account (String) and the list of contacts (contactosListWrapper). 

I was doing the Trailhead when in the module of "Using sObjects" one of the right answers is "A generic sObject variable can be assigned to any specific sObject, standard or custom. Such as Account or Book__c." But I think this answer is wrong or I`m not understanding very well the question.

With the frase "A generic sObject variable can be assigned to any specific sObject" I understand something like this:

sObject a = new Account();  // This is a generic sObject variable

Contact b = new Contact(); // This is a specific sObject

// A generic sObject variable can be assigned to any specific sObject 

b = (Account) a;  // Error


I understand that the last statement is an error because b is a variable of Contact type no of Account type. For this reason i'm not agree with the frase "A generic sObject variable can be assigned to any specific sObject". 

Am I missing something ? 

Thanks for your help!

I would like to query all users with their related list of a custom object (By the owner relationship). Because I would like to count how many records belongs to each user. For example:
 
[ SELECT Name, (SELECT Count() FROM relationshipName) FROM CustomObject ]

But I don't know which is the relationshipName that i should use. I don't want to create a new lookup field, I would like to use the owner relationship for this. Is it possible ? If not, is there a better way to achieve my purpose?  
Hi guys,  i have a sting str = (majestic badger, fluffy bunny, scary bear, chicken) ,now i want to split it in such a way that if  i use
 index0 ,majestic badger  should be output
index1,fluffy bunny should be output
index2, scary bear .....like this how i can this?

Hi,

 

I am looking for possible solution to get this result:

 

                          Account Name                                      Contact Name

                     United Oil & Gas Corp.                               Lauren Boyle

                     United Oil & Gas Corp.                               Avi Green

                     sForce                                                            Jake Llorrac

                     University of Arizona                                    Jane Grey

                     University of Arizona                                    Arthur Song

 

 

 Though, I am getting this result with my regular code:

                    

                         Account Name                                       Contact Name

                    United Oil & Gas Corp.                                 Lauren Boyle

                                                                                              Avi Green

                    sForce                                                             Jake Llorrac

                    University of Arizona                                     Jane Grey

                                                                                              Arthur Song

 

 

Now, here is my code on Visualforce Page:

 

<apex:pageblocksection id="pbsAccnt" rendered="true">
    <apex:pageBlockTable id="pbtAccnt" value="{!lstACcount}" var="AC">
        <apex:column headerValue="Account(s)" value="{!AC.Accnt__c}" />
        <apex:column headerValue="Contact(s)" value="{!AC.Cont__c}" />
    </apex:pageBlockTable>
</apex:pageblocksection>

 

I already tried with Wrapper class with <apex:repeat> but it is not working.

 

Any help would be greatly appreciated.

Hi All,

 

        I have a similar req   http://success.salesforce.com/ideaview?id=08730000000BrLkAAK.Any help is greatly appreciated.

 

Thanks.