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
Nitin Palmure 5Nitin Palmure 5 

Need help in population parent Object name (lookup) while adding child record in custom visual force page

Hello,

I have multiple problems. But before that I want to share the code that I wrote.

Apex Class:
public with sharing class meetingAttendeeExtension {
    public List<MeetingAttendees__c> listMeetingAttendee {get; set;}
    MeetingAttendees__c meetingAttendee = new MeetingAttendees__c();
    public String KARId = apexpages.currentpage().getParameters().get('id');
    ApexPages.StandardController controller;
    public meetingAttendeeExtension(ApexPages.StandardController controller) {
        listMeetingAttendee = new List<MeetingAttendees__c>();
        listMeetingAttendee.add(meetingAttendee );
        controller = this.controller;
    }
    public void addMeetingAttendee(){
        MeetingAttendees__c meetAtt = new MeetingAttendees__c();
        listMeetingAttendee.add(meetAtt);
    }
    public PageReference saveMeetingAttendees() {
        for(Integer i=0; i<listMeetingAttendee.size(); i++)
        {
            insert listMeetingAttendee;
        }
        return new PageReference('/' + Schema.getGlobalDescribe().get('Key_Account_Report__c').getDescribe().getKeyPrefix() + '/o'); // this works but not what I am trying to acheive
        }
    public PageReference cancel(){
        return new PageReference('/' + controller.getId() );// returns null
    }
}

Visualforce Page: 
<apex:pageBlock >
             <apex:pageBlockSection title="Add Meeting Attendees" columns="1">
             <apex:pageBlockTable value="{!listMeetingAttendee}" var="meetAtt" id="customTable">
             <apex:column headerValue="Key Account Report Name">
                 <apex:inputField value="{!meetAtt.Call_Report__c}"/>
             </apex:column>
             <apex:column headerValue="Attendee Type">
                 <apex:inputField value="{!meetAtt.Attendee_Type__c}"/>
             </apex:column>
             <apex:column headerValue="Attendee Name">
                 <apex:inputField value="{!meetAtt.Attendee_Name__c}"/>
             </apex:column>
             </apex:pageBlockTable>
             </apex:pageBlockSection>
             <apex:pageBlockButtons >
                 <apex:commandButton value="Add Account Row" action="{!addMeetingAttendee}" rerender="customTable"/>
                 <apex:commandButton value="Save Meeting Attendees" action="{!saveMeetingAttendees}"/>
                 <apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>
             </apex:pageBlockButtons>
             
         </apex:pageBlock>
     </apex:form>
</apex:page>

Now there are following questions that I am trying to find but unable to implement.
Meeting_Attendees__c is child to Key_Account_Reports__c(Master Detail Relationship).
1. when I click "Add New Attendees", the name of Parent Key Account report does not poppulate by itself (how to do this)
2. controller.getId() gives me null, i am expecting to get parent ID here, is this right way ?

Solving above will help me acheive following.
1. Auto populate name of parent object so that user need not select it from the huge liste of Key Account Reports
2. create buttons which will navigate user to parent record, detail view page.

I found previous post related  to this https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F000000090tcIAA

but this does not help.

Please suggest some way to acheive above.

Regards, 
Nitin V Palmure
Best Answer chosen by Nitin Palmure 5
Jason Reiber [ACS]Jason Reiber [ACS]
Hi Nitin,

If I'm understanding correctly, you have a related list view on the Key Account Reports screen which shows the related Meeting Attendees, is that correct?  Then you have a button to add a new Meeting Attendee?  Given this scenario, what I've done in the past is created a custom button for the list instead of using the standard New button.  The custom button has the URL of the VF page and includes a separate ID for the parent object.  So your List URL button on the Meeting_Attendees__c object might look something like : 

/apex/AddAttendeesPage?KARID={!Key_Account_Reports__c.Name}

Then in your meetingAttendeeExtension class you can use the same syntax to get the parameter and use it in your code to default the value of the field for Key Account Report.  So your parameter get call would look like : 

public String KARId = apexpages.currentpage().getParameters().get('KARID');

You can use the KARId string to set the Call_Report__c value that is referenced in the VF output

All Answers

Jason Reiber [ACS]Jason Reiber [ACS]
Hi Nitin,

If I'm understanding correctly, you have a related list view on the Key Account Reports screen which shows the related Meeting Attendees, is that correct?  Then you have a button to add a new Meeting Attendee?  Given this scenario, what I've done in the past is created a custom button for the list instead of using the standard New button.  The custom button has the URL of the VF page and includes a separate ID for the parent object.  So your List URL button on the Meeting_Attendees__c object might look something like : 

/apex/AddAttendeesPage?KARID={!Key_Account_Reports__c.Name}

Then in your meetingAttendeeExtension class you can use the same syntax to get the parameter and use it in your code to default the value of the field for Key Account Report.  So your parameter get call would look like : 

public String KARId = apexpages.currentpage().getParameters().get('KARID');

You can use the KARId string to set the Call_Report__c value that is referenced in the VF output
This was selected as the best answer
Vidya DVidya D
You can pass parent record Id as part of Qury string while invoking this page.

If you are invoking this page as prt of button click on master record, then create new button override, and use - New Button or Link, and use Content Source as URL and pass the parent id as parameter.

You will need to retrieve the parameter in the construction of your controller extension.

Refer this
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AvXyIAK
 
Nitin Palmure 5Nitin Palmure 5
Hi Jason and Vidya

Thanks a ton for your insights here ....
I have already used above method,
but return new PageReference('/' + controller.getId(Id) );
is giving be  null error.
Unable to understand where am i going wrong.

Regards, 
Nitin V Palmure
Jason Reiber [ACS]Jason Reiber [ACS]
Nitin,

I'm not sure I understand what you are doing with the code that is giving you a null error, can you paste the code in the context of your apex class?
Nitin Palmure 5Nitin Palmure 5
Yes, sure.

Kindly refer Below (notes in comments):

public with sharing class meetingAttendeeExtension {
    public List<MeetingAttendees__c> listMeetingAttendee {get; set;}
    MeetingAttendees__c meetingAttendee = new MeetingAttendees__c();
    public String KARId = apexpages.currentpage().getParameters().get('callreportId');

 public meetingAttendeeExtension(ApexPages.StandardController controller) {
        listMeetingAttendee = new List<MeetingAttendees__c>();
        listMeetingAttendee.add(meetingAttendee );
   }

public void addMeetingAttendee(){
        MeetingAttendees__c meetAtt = new MeetingAttendees__c();
        listMeetingAttendee.add(meetAtt);
    }
    
 public PageReference saveMeetingAttendees() {
        for(Integer i=0; i<listMeetingAttendee.size(); i++)
        {
            insert listMeetingAttendee;
        }
        return new PageReference('/' + Schema.getGlobalDescribe().get('Key_Account_Report__c').getDescribe().getKeyPrefix() + '/o'); //Here I want user to get redirected to parent record but temporarily it will list all the Key Account Reports.
        }

public PageReference cancel(){
        return new PageReference('/' + KARId ); // this is where i want user to go back to parent record which gives null error.
    }

Regards, 
Nitin V Palmure
Jason Reiber [ACS]Jason Reiber [ACS]
Did you try testing it manually?  So when you click the Add New Attendees, look at the URL and take the value that is passed after "callreportid=" then create the URL that should take you to the parent record, just to be sure that it's not an issue with the wrong ID being passed or something like that.
Nitin Palmure 5Nitin Palmure 5
I was able to achieve what I wanted, Let me share the final code:
 
Apex Extension: 


public with sharing class meetingAttendeeExtension {
    public List<MeetingAttendees__c> listMeetingAttendee {get; set;}
    MeetingAttendees__c meetingAttendee = new MeetingAttendees__c();
    public String KARId = apexpages.currentpage().getParameters().get('callreportId');
    ApexPages.StandardController controller;
    public meetingAttendeeExtension(ApexPages.StandardController controller) {
        listMeetingAttendee = new List<MeetingAttendees__c>();
        if(KARId<>null){
            for(Key_Account_Report__c KAR: [SELECT Id FROM Key_Account_Report__c WHERE Id =:KARId ]){
                meetingAttendee.Call_Report__c = KARId ;
                listMeetingAttendee.add(meetingAttendee );
            }      
        } else{
             listMeetingAttendee.add(meetingAttendee );
        }
        
    }
    public void addMeetingAttendee(){
        MeetingAttendees__c meetAtt = new MeetingAttendees__c();
        meetAtt.Call_Report__c = KARId ;
        listMeetingAttendee.add(meetAtt);
    }
    public PageReference saveMeetingAttendees() {
        for(Integer i=0; i<listMeetingAttendee.size(); i++)
        {
            insert listMeetingAttendee;
        }
        return new PageReference('/' + KARId );
        }
    public PageReference cancel(){
        return new PageReference('/' + KARId );
    }
}

Visualforce Page:

<apex:page standardController="MeetingAttendees__c" extensions="meetingAttendeeExtension">
     <apex:form > 
         <apex:pageBlock >
             <apex:pageBlockSection title="Add Meeting Attendees" columns="1">
             <apex:pageBlockTable value="{!listMeetingAttendee}" var="meetAtt" id="customTable">
             <apex:column headerValue="Key Account Report Name">
                 <apex:inputField value="{!meetAtt.Call_Report__c}"/>
             </apex:column>
             <apex:column headerValue="Attendee Type">
                 <apex:inputField value="{!meetAtt.Attendee_Type__c}"/>
             </apex:column>
             <apex:column headerValue="Attendee Name">
                 <apex:inputField value="{!meetAtt.Attendee_Name__c}"/>
             </apex:column>
             </apex:pageBlockTable>
             </apex:pageBlockSection>
             <apex:pageBlockButtons >
                 <apex:commandButton value="Add Account Row" action="{!addMeetingAttendee}" rerender="customTable" immediate="true"/>
                 <apex:commandButton value="Save Meeting Attendees" action="{!saveMeetingAttendees}"/>
                 <apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>
             </apex:pageBlockButtons>
             
         </apex:pageBlock>
     </apex:form>
</apex:page>

What was the problem:
The proper ID was not getting passed.

Thanks a lot for all the help both.

Regards, 
Nitin V Palmure
 
jeevitha annabathula 5jeevitha annabathula 5
Hi ,
I too have thes two same issue but this coding didn't solve my issue. please can some help me to solve my issue please