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
vanrentvanrent 

Custom object - VF page / Apex Constructor not defined

This is my first go at a VF page. I've got a lot of info off the boards & youtube on how to pull together data from 2 tables into one list, but haven't been able to successfully accomplish it with my custom object.

I've tried to simply the process to just display records from my custom object in a list, but still unable to do that.

A little background...
I've created a custom object, Jobs__c, that stores construction jobs. I've also created a junction object to link the job to the account and have a related list on the job showing all of the accounts, and a related list on the account showing all their jobs. I would like to have a related list on the account showing all the completed tasks at the job level. Since I couldn't get that far, I backed up & just tried to display a list of jobs, showing the name, city and id.

Here's the code from my VF page:
<apex:page standardController="Jobs__c" extensions="WrapperDemoClass">
  <apex:form >
    <apex:pageblock >
      <apex:pageblockTable value="{!wrapperObj}" var="Rec">
         <apex:column value="{!Rec.J.name}"/>
      </apex:pageblockTable>
    </apex:pageblock>
    </apex:form>
</apex:page>

And from the controller:
public with sharing class WrapperDemoClass {
 Public List<WrapperClassEx> WrapperList{get;set;}
    public WrapperDemoClass(ApexPages.StandardController controller) {     
    }  
   Public List<WrapperClassEx> getwrapperObj(){
      List<Jobs__c> JList = [Select id,name, Job_City__c from Jobs__c limit 10];
      WrapperList = New List<WrapperClassEx>();
      for(Jobs__c J: JList){
        WrapperList.add(New WrapperClassEx(J, name, Job_City__c));
      }
      return WrapperList;
   }  
   Public Class WrapperClassEx{
     Public Id JId{get;set;}
     Public string JobName{get;set;}
     Public string JobCity{get;set;}
   
     Public WrapperClassEx(ID J, string JName, string JCity){
        JId = J;
        JobName=JName;
        JobCity=JCity;                  
     }
   }  
}

I am sure I've missed something obvious, and am still working on it, but I'm at a loss. Any help would be appreciated.
Thanks!
Pankaj_GanwaniPankaj_Ganwani
Hi,

If you want to show the related list on Account page then you should do standardcontroller = "Account" instead of Job__c and perform soql on junction object as below:

public with sharing class WrapperDemoClass {
 Public List<WrapperClassEx> WrapperList{get;set;}
 private Id accountId;
    public WrapperDemoClass(ApexPages.StandardController controller) {  
               accountId = controller.getId();
    }  
   Public List<WrapperClassEx> getwrapperObj(){
      List<Jobs__c> JList = [Select Jobs__c, Jobs__r.Name, Jobs__r.City__c where Account__c =:accountId and Jobs__r.status='Completed'];//Query on junction object
      WrapperList = New List<WrapperClassEx>();
      for(Jobs__c J: JList){
        WrapperList.add(New WrapperClassEx(J.Jobs__c, J.Jobs__r.name, Jobs__r.Job_City__c));
      }
      return WrapperList;
   }  
   Public Class WrapperClassEx{
     Public Id JId{get;set;}
     Public string JobName{get;set;}
     Public string JobCity{get;set;}
   
     Public WrapperClassEx(ID J, string JName, string JCity){
        JId = J;
        JobName=JName;
        JobCity=JCity;                  
     }
   }  
}
vanrentvanrent
Thanks for the speedy reply, Pankaj!

Can you please clarify... If Jobs__c is my custom object, then is Jobs__r the tasks for that object?

Thanks,
Connie
Pankaj_GanwaniPankaj_Ganwani
Hi,

Jobs__r is used to refer the field of the parent object record like in your example say Job_Account__c is a junction object and we want to fetch related job name and city, then we have to use Jobs__r.Name and Jobs__r.City(cross object formula) in SOQL.

One more thing, Can you please clarify what you want to display as related list on Account detail page either(Tasks(standard object) which are completed) or Jobs(related via junction object which are completed)?
vanrentvanrent
Using my junction object (Job_Acct_Junction__c), I already have a related list displaying the jobs. What I would like is an additional related list displaying the completed tasks for the job on the account page layout. Should I really be using the junction object instead of the Jobs object?

When I inserted your code, I got the following:
[Error] Error: WrapperDemoClass Compile Error: unexpected token: where at line 8 column 79

This corresponds to:
List<Jobs__c> JList = [Select Jobs__c, Jobs__r.Name, Jobs__r.Job_City__c where Account__c =:accountId and Jobs__r.status='Completed'];//Query on junction object
just before ' where Account__c'

Really appreciate your help!
 
Pankaj_GanwaniPankaj_Ganwani
Hi,

I have few questions before proceeding with it:
1. You want to show the completed Tasks(standard object) related to those Jobs(WhatId) which are linked with Account, right?
2. If above statement is true, there can be multiple jobs associated with single Account via Junction object, In that case do we have to show the Tasks records corresponding to all jobs which are completed?

Can you please clarify these things so that I can attempt to give you assistance?
vanrentvanrent
Multiple jobs per account and multiple accounts per job; many-to-many relationship.

Yes, all tasks for all jobs, linked to that account.
Pankaj_GanwaniPankaj_Ganwani
Hi,

Thanks for the clarifications.


public with sharing class WrapperDemoClass {
 private Id accountId;
    public WrapperDemoClass(ApexPages.StandardController controller) {  
               accountId = controller.getId();
    }  
   Public List<Task> getTasks()
  ​{
      Set<Id> setJobId = new Set<Id>();
      for(Job_Acct_Junction__c objJun : [Select Jobs__c where Account__c =:accountId])
      {
            setJobId.add(objJun.Jobs__c);
      }  
      
      return [select Id, Status, Subject from Task where WhatId IN : setJobId and Status = 'Completed'];
}  

}

VF page:


<apex:page standardController="Account" extensions="WrapperDemoClass">
  <apex:form >
    <apex:pageblock >
      <apex:pageblockTable value="{!Tasks}" var="Rec">
         <apex:column value="{!Rec.Id}"/>
         <apex:column value="{!Rec.Subject}"/>
      </apex:pageblockTable>
    </apex:pageblock>
    </apex:form>
</apex:page>

Can you please try to use above mentioned code? Make sure give the proper API names of the fields if I have missed them.
vanrentvanrent
I just want to be sure I know which fields should be refereced...

Here's the custom fields from my junction object:
custom fields

I believe the only line that needs changed is:
 for(Job_Acct_Junction__c objJun : [Select Jobs__c where Account__c =:accountId])

I'm getting a message:
Error: WrapperDemoClass Compile Error: unexpected token: where at line 9 column 56

which is again at the Where statement.

Am I correct in assuming that Account__c is account ID from the junction object & should be AcctID__c? Also, what object does accountId belong to?