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
Upton_XUpton_X 

Combine Two Related List

Hello everyone,

Through searching various past threads it looks like this questions has been asked a few times, however I am hoping to get some specific help.

I've just last week started dedicating myself to learning to develop (SF99!)  So I understand this is beyond my range, but this is a need at my current Org, and I thought it would be fun to actually try to do some development.

Note* I understand that a junction object could be an option here.  Given the users that will be using these objects, the fewer clicks the better so I need to stick with this model.

 

We have a Contract Agreement Object that has 5 lookup fields for parties that can be related to the Contract Agreement.  On the Party Object, I have 5 list.  I would like to combine them into one list, and display them on the Party object.  Here is what I have for code so far,  I'm a newbie so I'm sure I am far off, but I'm excited to learn.

public class CombineContractAgreement {
public CombineContractAgreement(ApexPages.StandardController controller) {
    
}

   Public List <Contract_Agreement__C> AllRelatedAgreements;
    
    Public List<Contract_Agreement__c>GetAllRelatedAgreement()
    {
        AllRelatedAgreements = [SELECT id, RecordTypeId, APXT_Redlining_Status__c, APXT_Redlining__Effective_Date__c, Agreement_Name__c FROM Contract_Agreement__c 
                                
                                WHERE //Current record is value on any 1 of 5 lookup fields on the Contract Agreement Record.//
 								return AllRelatedAgreements 
                               }
 

Any help you can provide will be greatly apprchated.




 

Best Answer chosen by Upton_X
vishal-negandhivishal-negandhi

Hi Upton, 

Sorry, I missed your comment here. 

public class CombineContractAgreement {
	Id LegalEntityId;
public CombineContractAgreement(ApexPages.StandardController controller){
    LegalEntityId = ((SB_Legal_Entity__c)controller.getRecord()).Id;
    }
    
    public List<APXT_Redlining__Contract_Agreement__c>GetAllRelatedAgreement()
    {
     List <APXT_Redlining__Contract_Agreement__c> AllRelatedAgreements;
        AllRelatedAgreements = [SELECT id, RecordTypeId, APXT_Redlining__Status__c, APXT_Redlining__Effective_Date__c,
        Agreement_Name__c FROM APXT_Redlining__Contract_Agreement__c WHERE Customer_Legal_Entity__c = :LegalEntityId OR Customer_Legal_Entity_2__c = :LegalEntityId OR 
                                Customer_Legal_Entity_3__c = :LegalEntityId OR Party_4__c = :LegalEntityId OR Party_5__c = :LegalEntityId];
        							return AllRelatedAgreements;
    }
}


.getRecord() returns the original object and so we need to modify our code a bit to get the Id. 

I hope this helps.

All Answers

vishal-negandhivishal-negandhi

Hi there, 

From what I understood, you'll be on the Party record page and you'll want to see all agreements from 5 different related lists in a single list
Based on the above, try below code

public class CombineContractAgreement {
	Id PartyId; // this to store the current party id
public CombineContractAgreement(ApexPages.StandardController controller) {
	PartyId = (Party__c) controller.getRecord().Id;	// change the object api name if Party__c is incorrect
}

   public List<Contract_Agreement__c>GetAllRelatedAgreement()
   {
    List <Contract_Agreement__C> AllRelatedAgreements;    
     AllRelatedAgreements = [SELECT id, RecordTypeId, APXT_Redlining_Status__c, APXT_Redlining__Effective_Date__c,       Agreement_Name__c  FROM Contract_Agreement__c  WHERE Lookup1__c = :PartyId OR Lookup2__c = :PartyId OR Lookup3__c =: PartyId OR Lookup4__c = :PartyId OR Lookup5__c = :PartyId];
 		return AllRelatedAgreements 
    }
}
Add the lookup field API names in the WHERE clause, but rest all should work.
Upton_XUpton_X

Thanks a ton Vishal,  You're a huge help.

I'm close, but I am getting an error on Line 4

"Incompatible types since an instance of Id is never an instance of SB_Legal_Entity__c"

"Party" is actually Legal Entity - API Name SB_Legal_Entity__C.  I've tried to search for this issue, but it seems fairly specific?

public class CombineContractAgreement {
	Id LegalEntityId;
public CombineContractAgreement(ApexPages.StandardController controller){
    LegalEntityId =(SB_Legal_Entity__c)controller.getRecord().Id;
    }
    
    public List<APXT_Redlining__Contract_Agreement__c>GetAllRelatedAgreement()
    {
     List <APXT_Redlining__Contract_Agreement__c> AllRelatedAgreements;
        AllRelatedAgreements = [SELECT id, RecordTypeId, APXT_Redlining__Status__c, APXT_Redlining__Effective_Date__c,
        Agreement_Name__c FROM APXT_Redlining__Contract_Agreement__c WHERE Customer_Legal_Entity__c = :LegalEntityId OR Customer_Legal_Entity_2__c = :LegalEntityId OR 
                                Customer_Legal_Entity_3__c = :LegalEntityId OR Party_4__c = :LegalEntityId OR Party_5__c = :LegalEntityId];
        							return AllRelatedAgreements;
    }
}
Thanks Again for all the help!
vishal-negandhivishal-negandhi

Hi Upton, 

Sorry, I missed your comment here. 

public class CombineContractAgreement {
	Id LegalEntityId;
public CombineContractAgreement(ApexPages.StandardController controller){
    LegalEntityId = ((SB_Legal_Entity__c)controller.getRecord()).Id;
    }
    
    public List<APXT_Redlining__Contract_Agreement__c>GetAllRelatedAgreement()
    {
     List <APXT_Redlining__Contract_Agreement__c> AllRelatedAgreements;
        AllRelatedAgreements = [SELECT id, RecordTypeId, APXT_Redlining__Status__c, APXT_Redlining__Effective_Date__c,
        Agreement_Name__c FROM APXT_Redlining__Contract_Agreement__c WHERE Customer_Legal_Entity__c = :LegalEntityId OR Customer_Legal_Entity_2__c = :LegalEntityId OR 
                                Customer_Legal_Entity_3__c = :LegalEntityId OR Party_4__c = :LegalEntityId OR Party_5__c = :LegalEntityId];
        							return AllRelatedAgreements;
    }
}


.getRecord() returns the original object and so we need to modify our code a bit to get the Id. 

I hope this helps.

This was selected as the best answer
Upton_XUpton_X

Hi Vishal -

This is very helpful, I was able to use this and go back and understand why line 4 needed to be updated.

I'm beginning to truly understand how much I don't know.  Fun.

Questions:

Okay so I have this class - now I need to create a VF Page or LWC to display it on the Legal Entity Page?User-added image

I've searched, and here is the code for the VisualForce component.  Can you please let me know if I'm on the right track?
Erorr: Line 0 Unknown constructor 'CombineContractAgreement.CombineContractAgreement()'

<apex:component controller="CombineContractAgreement"> 
    <apex:form >
    	<apex:pageBlock title = "Related Agreements">
         <apex:pageBlockSection columns= "5" >
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:component>
Again, huuuge thanks.


 

vishal-negandhivishal-negandhi

Hi Upton, 

You need to use a Visualforce page here [ideally, a lightning component for Lightning experience, but that would need some hands-on lightning components, so let's start with a visualforce page].

Here's the basic code to start with
 

<apex:page StandardController="SB_Legal_Entity__c" extensions="CombineContractAgreement" lightningStylesheets="true">
	<apex:pageBlock title="All Related Agreements">
		<apex:pageBlockTable value="{!AllRelatedAgreement}" var="item">
			<apex:column value="{!item.Agreement_Name__c}" />
			<apex:column value="{!item.APXT_Redlining__Status__c}" />
			<apex:column value="{!item.APXT_Redlining__Effective_Date__c}" />
		</apex:pageBlockTable>
	</apex:pageBlock>
</apex:page>
 

You can play around if you wish, here's the visualforce guide to help you : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_pageBlockTable.htm

Good luck :)
 

Upton_XUpton_X

This is awesome - I'm (vishal really) is so close.  I tried to build a lwc but I think that will take me a few months to get the handle of.  The last question I think is how can I get the name to link to the Contract page.  I've tried to plug in an <apex:outputlink> which appears to work, but I'm a little off on the final result.  I've gone to this page (https://www.salesforcetutorial.com/outputlink-example/) which suggest I plug in "salesforceinstance.lightning.force.com/{!r.ID}" (included in the code below, but I am reciving an error "Unknown property 'SB_Legal_Entity__cStandardController.r'".

I am assuming that I need to eddit the public class, but I am unsure of how to do that.

 

 

Visualforce Page 
User-added image

.vfp
 

<apex:page StandardController="SB_Legal_Entity__c" extensions="CombineContractAgreement" lightningStylesheets="true">
	<apex:pageBlock title="All Related Agreements">
		<apex:pageBlockTable value="{!AllRelatedAgreement}" var="item">
            <apex:column>
                <apex:outputLink value="https://myinstance--sb1.lightning.force.com/{!r.ID}">{r.Name}</apex:outputLink>
            </apex:column>    
            <apex:column value="{!item.Name}" />
			<apex:column value="{!item.Agreement_Name__c}" />
			<apex:column value="{!item.APXT_Redlining__Status__c}" />
			<apex:column value="{!item.APXT_Redlining__Effective_Date__c}" />
		</apex:pageBlockTable>
	</apex:pageBlock>
</apex:page>


.apxc
 
public class CombineContractAgreement {
    Id LegalEntityId;
public CombineContractAgreement(ApexPages.StandardController controller){
    LegalEntityId = ((SB_Legal_Entity__c)controller.getRecord()).Id;
    }
    
    public List<APXT_Redlining__Contract_Agreement__c>GetAllRelatedAgreement()
    {
     List <APXT_Redlining__Contract_Agreement__c> AllRelatedAgreements;
        AllRelatedAgreements = [SELECT Name, RecordTypeId, APXT_Redlining__Status__c, APXT_Redlining__Effective_Date__c,
        Agreement_Name__c FROM APXT_Redlining__Contract_Agreement__c WHERE Customer_Legal_Entity__c = :LegalEntityId OR Customer_Legal_Entity_2__c = :LegalEntityId OR 
                                Customer_Legal_Entity_3__c = :LegalEntityId OR Party_4__c = :LegalEntityId OR Party_5__c = :LegalEntityId];
        							return AllRelatedAgreements;
    }

Could you please help me link the .Name to the associated record?
vishal-negandhivishal-negandhi

Hi Upton, 


The error you get is because you're using r.Fieldname which is from the example on the link, whereas on your page you need to use item.fieldname since that's what we have set our var on the page block table. 

Try the below and see if this works
 

<apex:outputLink value="/{!item.ID}">{item.Name}</apex:outputLink>