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
Shwetal DesaiShwetal Desai 

how to set Master Record Id in VF page of Detail Object?

I have two custom objects with master-detail relationships.
Detail custom object's New button has overrided with the Visual force page.
now, I want to access Master's Record Id to set in Detail's Field in "Save" method in EXTENSION class. This method is to insert the detail record . 
 
How can i Do this?
 
Thanks in Advance.
 
 
Shwetal
 
Luke@TWSLuke@TWS
I wrote an extension to query the master record for the ID and then set detail records master that way. I only have one master record though.

You'll need to use something like:

Code:
private final Detail__c detail;
private final Master__c master;

public IntegrationExtension(ApexPages.StandardController controller) {
    detail = (Detail__c)controller.getRecord();

master = [select id from Master__c where id = :ApexPages.currentPage().getParameters().get(detail.id + '_lkid')]; if (detail.Master__c == null) { detail.Master__c = master.Id; }
}



Message Edited by Luke@TWS on 10-14-2008 08:47 AM
Shwetal DesaiShwetal Desai
Problem is solved
Thank you for the solution...
dfalsodfalso
Hi, I think I have the same issue, but this code isn't working for me.
I have a custom object called Project (the 1), and a custom object called Phase (the many), with a lookup relationship to Project (projects have multiple phases). It's a lookup because the phase has a 1-many relationship with another object - financial note - and so on. I'm also using a custom page to override the New page for my "many" object.
So I tried rewriting your code to this:
Code:
public class SetDetailRelationshipKey 
{
 private final CDEPhase__c detail;
 private final CDEProject__c master;
 
 public SetDetailRelationshipKey(ApexPages.StandardController controller) {
 
     detail = (CDEPhase__c)controller.getRecord();
     master = [select id from CDEProject__c where id = :ApexPages.currentPage().getParameters().get(detail.id + '_lkid')];
 
     if (detail.CDEProject__c == null) {
         detail.CDEProject__c = master.Id;
     }
 }
}

 
and it doesn't work.
I wasn't sure a good way to debug, so I rewrote it like this:
Code:
public class SetDetailRelationshipKey 
{
 private final CDEPhase__c detail;
 private final CDEProject__c master;
 
private final Id currentpageID;

 public SetDetailRelationshipKey(ApexPages.StandardController controller) {
 
     detail = (CDEPhase__c)controller.getRecord();
     this.currentpageID = ApexPages.currentPage().getParameters().get(detail.id + '_lkid');
     //master = [select id from CDEProject__c where id = :ApexPages.currentPage().getParameters().get(detail.id + '_lkid')];
 
     //if (detail.CDEProject__c == null) {
     //    detail.CDEProject__c = master.Id;
     //}
 }
 
 public Id currentpageIDdom
 { 
  get { return currentpageID; }
 }
 
 public string domtest 
 {
  get { return 'test'; }
 }
 
 public CDEPhase__c detailtest 
 {
  get { return detail; }
 }
}

 and tried displaying the various properties on the page, but it appears my currentpageID field is not getting set. I tried looking up some of the functions in the documentation and couldn't find anything (they still list $CurrentPage). I'd really want to find some good documentation on the Parameters usage, but no luck there.
Any ideas?

Thanks for any help you can spare.

dfalsodfalso
FYI, as a test, I did this (pretty much directly out of the Apex Language Reference):
Code:
public class SetDetailRelationshipKey 
{
 private final CDEPhase__c detail;
 private final CDEProject__c master;
 
public String getdetailstring() { return '' + detail.id + ''; } 
 
 public SetDetailRelationshipKey(ApexPages.StandardController controller) 
 {
     this.detail = (CDEPhase__c)controller.getRecord();
 }
}

 and then tried to display it as this in my page:
Code:
<apex:pageBlock title="Testing">
 <apex:outputPanel id="testpanel">
  <p/>
  {!detailstring}
  <p/>
 </apex:outputPanel>
</apex:pageBlock>

 
it displays as "null"
Luke@TWSLuke@TWS
Are you doing this from a VF page which is used to override the New action of a detail object?
dfalsodfalso
Yes.
Because you're asking me that, I'm guessing that SF doesn't actually generate the new record ID until insertion time?

Based on a dump of the getParameters() passed into the controller, I think I can get away with just looking for a key value with suffix of "_lkid". But how is this issue commonly dealt with?

Thanks for your help.
Luke@TWSLuke@TWS
Indeed. You need a VF page using the standard controller and a controller extension. As I remember when you don't have the standard master.id field present on the page it does not get populated and you have to set it using the extension.
dfalsodfalso
I am using a standard controller with an extension, actually.
But I didn't put the ID (nor the relationship ID) on the page, though. I'd think I wouldn't want to put those fields on the page as standard input fields. Maybe make them hidden?
Luke@TWSLuke@TWS
Yes have Master__c.Detail__c (whatever your objects are called) as a hidden field so that you can set the value of it. VF doesn't seem to query the fields unless you are using them on your page somewhere
dfalsodfalso
Still no luck. I tried putting this on my page:
Code:
<apex:inputHidden value="{!CDEPhase__c.ID}"/>
<apex:inputHidden value="{!CDEPhase__c.CDEProject__c}"/>

and also as outputFields, and neither seem to be populated. BTW, I did this in my extension class (for the time being):
Code:
private final Id currentpageID;
private final Map<String, String> currentpageParams;
private final String currentpageIDString;
private final String currentpageParentIDString;

public String propCurrentPageIDString { get { return currentpageIDString; } }
public String propCurrentpageParentIDString { get { return currentpageParentIDString; } } 

//these lines are in the constructor:
this.currentpageParams = ApexPages.currentPage().getParameters();
this.currentpageIDString = (String)detail.ID;
this.currentpageParentIDString = this.currentpageParams.get(currentpageIDString + '_lkid');
//

public void writeParams() { //loop through existing params (get key and value) and write to table, so can see what's going on: List<domtemp__c> newRecs = new List<domtemp__c>(); Set<String> mapKeySet = this.currentpageParams.keySet(); for (String g : mapKeySet) { domtemp__c d = new domtemp__c(); d.Name = g; d.string2__c = this.currentpageParams.get(g); newRecs.add(d); } insert newRecs; } public void writeIDs() { List<domtemp__c> newRecs = new List<domtemp__c>(); { domtemp__c d = new domtemp__c(); d.Name = 'child id'; d.string2__c = this.currentpageIDString; newRecs.add(d); } { domtemp__c d = new domtemp__c(); d.Name = 'parent id'; d.string2__c = this.currentpageParentIDString; newRecs.add(d); } insert newRecs; }

 where domtemp__c is a table with the String2__c string field. I'm basically just trying to capture information so I can see it b/c I don't know a better way to debug this (is there a better way?). I've attached these methods (writeParams and writeIDs) to buttons on the page, and push them to insert the values into the table. I'm still getting blank values in domtemp__c for both the parent ID and the child ID. So I'm still doing something wrong.
Luke@TWSLuke@TWS
System.debug('hello'); for debugs. They then show up in the system log.

How have you got your objects and vf pages setup?

I have a VF page which has custom related lists. I have setup the object with an overriden New action so it goes to my new page when I click new on the related list. This takes me to my vf page with standard controller. I have a hidden field for the master record which I set in the extension. Can't see any reason why yours would not work if it is setup in the same way.
dfalsodfalso
I've got an object called CDEProject__c which right now is completely standard (no custom page overrides or anything). I've got an object called CDEPhase__c which has a field CDEProject__c of type "Lookup".
I'm overriding the "New" SB&L action with my custom page. The custom page has my controller extension.

Luke@TWSLuke@TWS
Ah I see. I have mine setup with a Master-Detail Relationship field
dfalsodfalso
Yes, I'm thinking that's the problem (based on the OP's setup as well).
I did a lookup relationship because my CDEPhase__c object will be a parent to another object, and the way I understand it (and through trying to configure it master/detail originally), I can't use master/detail chained like that.
dfalsodfalso
Update for anyone who reads this in the same situation as me:
I am now using the following as my relationship ID assignment. I'm not loving it, and am worried about how the parameters keys work (what is the lkid anyway? Could there be more than one? Is there documentation somewhere?), but this works for me:

Code:
public class SetDetailRelationshipKey 
{
 private final CDEPhase__c detail;
 private final CDEProject__c master;
 private final Map<String, String> currentpageParams;

 public SetDetailRelationshipKey(ApexPages.StandardController controller) 
 {
     this.detail = (CDEPhase__c)controller.getRecord();
     this.currentpageParams = ApexPages.currentPage().getParameters();

  Set<String> mapKeySet = this.currentpageParams.keySet(); 
  for (String g : mapKeySet)
  {
   if (g.endsWith('_lkid'))
   {
    master = [select id from CDEProject__c where id = :ApexPages.currentPage().getParameters().get(g)];
    this.detail.CDEProject__c = master.Id;
    break;
   } 
  }
 }
}

 
and you don't need to put a hidden field in the page.