+ Start a Discussion
Chicco120276Chicco120276 

Master-detail Lookup

Dear All,

 

I have my custom object (say MyCustomObjOne) that has a master-detail relationship to the Accoutn Object. The I have another omy custom object (say MyCustomObjTwo) that has a Lookup relationship to the Account object and a master-detail realtionship to the MyCustomObjOne.

 

I would like that when I create a new record for the MyCustomObjTwo, after I select the Account record I can select only the MyCustomObjOne that are child of the Accoutn record I have just selected.

 

Is it possible?How?

 

BR,
Enrico

waylonatcimwaylonatcim

I'm not sure of a built-in way to accomplish this but 2 things come to mind:

 

1) Write a trigger that makes sure the  MyCustomObjOne is a child of the Account you have selected.

2) Create a visualforce page that populates a drop down box with the  MyCustomObjOne objects based on the Account that is selected.  Then override the save function to associate the Id of the selected  MyCustomObjOne object to the lookup field.

 

I'm also interested in finding out if there are any easier/better solutions out there as this problem comes up for me from time to time as well.

Chicco120276Chicco120276

Hi,

 

thank you for your reply.

I am trying to write a visualforce page in order to get my goal. Unfortunately (maybe due too I am new on this technology) I have some problem.

 

My objects are "commessa" that are chilld of accounts.

 

I have created an inputField where user can select the Account. When user selct account the onchange actionSupport should rerender my drop-down list. 

 

this is the page:

 

<apex:page standardController="Giornaliera__c" extensions="GiornalieraObjExtension" showHeader="false" >
<apex:sectionHeader title="Giornaliera" subtitle="Nuova Giornaliera" />
<apex:form id="giornalieraForm">
    <apex:pageBlock title="Giornaliera pageblock" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Giornaliera Info" columns="1">
            <apex:inputField id="account" value="{!Giornaliera__c.Account__c}">
                <apex:actionSupport event="onchange" rerender="thePageBlockSectionItem"/>
            </apex:inputField>           
            <apex:pageBlockSectionItem id="thePageBlockSectionItem" >
                <apex:outputLabel value="{!$ObjectType.Giornaliera__c.fields.Commessa__c.label}" for="pLabel"/>
                <apex:outputPanel styleClass="requiredBlock" layout="block"/>
                    <apex:actionRegion >
                        <apex:selectList id="commessaLookupPicklist" value="{!Giornaliera__c.Commessa__c}" size="1" rendered="true">
                            <apex:selectOptions value="{!commesseList}"/>     
                        </apex:selectList>
                    </apex:actionRegion>
                </apex:outputPanel>
            </apex:pageBlockSectionItem>     
        </apex:pageBlockSection>
       
    </apex:pageBlock>
</apex:form>
</apex:page>

 

 

and this is my controller extensio:

 

public with sharing class GiornalieraObjExtension {
    private final ApexPages.standardController controller; 
    private final Giornaliera__c obj; 
    private Account account { get; set; }
   
     public Account getAccount(){
         return account;
     }
    
     public void setAccount(Account a){
         account = a;
     }
 
    public GiornalieraObjExtension(ApexPages.StandardController stdController) { 
         this.controller = stdController; 
         this.obj = (Giornaliera__c)stdController.getRecord(); 
     } 
  
     public SelectOption[] getCommesseList() {
          System.debug('getCommesseList'); 
         SelectOption[] commesseList = new SelectOption[]{};
         
         if (account != null) {
             System.debug('account id:' + account.id); 
             String accountIdToFilter = account.id;
            
             for (Commessa__c c : [select id, name from Commessa__c where Account__r.id = :accountIdToFilter order by name]) { 
                 commesseList.add(new SelectOption(c.id,c.name)); 
             }
         } 
         return commesseList; 
     } 
        
}

 

 

 

It seems that the onchange does not rerender "thePageBlockSectionItem".

My question is: rerender means that the  getCommesseList should be called to get new values?

how can I pass to the controller the selected Accoutn in order to select only the object (commesse) that are chould of that account?

 

BR,
Enrico

 

 

forcedev99forcedev99

I hope, in your case, MyCustomObjTwo is the parent of MyCustomObjOne. If that is true, selecting MyCustomObjOne on MyCustomObjTwo record creation does not make sense to me.