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
yvk431yvk431 

Populating lookup on a visual force page

Hi Guys,

 

I have a visual force page which will have a lookup field and needs to be prepopulated with the parents id when ever the page is called , We are able to do it by passind the parents(lookup) id , name as query string with the lookups field id .

 

But the problem is when we move the code to other org, the field id wont be the same and the lookup is not getting prepopulated.  Is there any better approach for this.

 

 

yvk431yvk431

Well I think I didnt explained my question properly. 

 

I am talking about custom objects lookup fields , in order to populate the we need to pass values to the html field ids in the query string for ex if I am having accountpayments__c having a lookup to locations__c , I need to prepopulate the location Id on visual force page (standard controller to accountpayments__c).

 

This I am able to do it in the following passion.

 

First from the visual force page i need to get the specific lookup fields html id ,will be in this format  C000B345dfglkv_id & C000B345dfglkv. these ids will be passes as querystring with the following value

 

for C000B345dfglkv_id  i need to pass the locations__c  id

      C000B345dfglkv   i need to pass the name  

 

This is working fine with in the org in which we hardcoded the id field values , but when the we move the package onto different org the fields are not prepopulated as the id values are different on different orgs , and there is no generic method explained by salesforce for this. 

 

Hope you got my requirement.

sforce2009sforce2009

Ok. I got your question. here it goes. Assuming your URL is something like this.

https://salesforce.cominstance/apex/yourPage?lookupVal=yourLookUpIdhere //You dont have to hardcode

 

Page:

<apex:page standardController="Contact" extensions="extContact">
  <apex:form>
      <apex:inputField id="dynFieldName" value="{!Contact.AccountId}"/>
      <script>
          document.getElementById('{!$Component.dynFieldName}_lkid').value = '{!accountId}';
          document.getElementById('{!$Component.dynFieldName}').value = '{!accountName}';
      </script>
  </apex:form>
</apex:page>


Extensions Class:

public class extContact
{
    public string accountId
    {    get;set;    }

    public string accountName
    {    get;set;    }
    
    public extContact(ApexPages.StandardController ctlr)
    {
        accountId = System.currentPageReference().getParameters().get('lookupVal');
        accountName = [select Name from Account where Id =: accountId].Name;
    }
}

 

You will need an extension class. I have explained with contact and account. You can use it for any object.

 

'Hope this is useful

SteveBowerSteveBower

This is a wierd reply.   It's far easier to just have an Action on the Page tag and set the values of the fields in there.

 

It's not exactly clear to me what you're trying to do.  It sounds from your description the you have Class A, and Class B, with a field in B called "Lookup_A". 

 

It sounds like you're calling a VisualForce page using the standardController for Object B, and you want to pre-fill in the value of Lookup_A.

 

Lastly, it sounds like you already have the value for the ID for Object A that you want to use in as the pre-filled value.  (Or, perhaps you don't have the ID, but you have the Name?)

 

If that's the case, then your visualForce page is going to be called with a URL that looks something like:

..../apex/myPage?Id=TheIdOfObjectB&DefaultAId=TheIDOfObjectA

 

You need to write a Custom controller which extends the standard Controller.  

 


    public class Controller_MyExtension {

        public ObjectB B {get; set;}

 

        Controller_MyExtension(ApexPages.StandardController stdController) {

           B = (ObjectA)stdController.getRecord();

           B.Lookup_A__c = ObjectAId = ApexPages.currentPage().getParameters().get('DefaultAId');

 

           // If the parameter you have isn't the ID of object A, but rather the NAME of object A, then you'd

          // need to do a query instead.   Note, you should use Try/catch in case you are given a bogus Name.

          B.Lookup_A__c = [select ID from Object A where name =

                 :ApexPages.currentPage().getParameters().get('DefaultAName') limit 1].id;

        }

   }

 

 

As far as the VF page goes, if the only thing you need is to pre-fill in the lookup, then you can just do:

 

<apex:page standardController="ObjectB" extensions="myExtension">

       <apex:detail subject="{!B.id}"/>

</page>

 

Best, Steve

 

p.s. I'm just typing this in by hand so I'm sure something won't work properly.  :-)

 

SteveBowerSteveBower

Sorry, this line:

 

           B.Lookup_A__c = ObjectAId = ApexPages.currentPage().getParameters().get('Defau ltAId');

 

should have been:

 

           B.Lookup_A__c = ApexPages.currentPage().getParameters().get('Defau ltAId');

 

 

And, when I said "this is a wierd reply", I was referring to the previous posting.

sforce2009sforce2009

I agree with you steve. And thats what exactly I posted in my first reply (populating Task input fields). It seemt it did not work for YVK. So I thought of a workaround again. Not sure if that is correct!

yvk431yvk431

Thanks for the reply guys,

 

I am very sorry that I messed up with my question, actually I got this requirement long back (3 months)

 hope you wont wind.

 

Now here is what I wanted, actually i need to prepulate the lookup on a detail page not on VFP.

For this we are passing the query strings as    

/00a/e?0000abcvcvddf_lkid=0000fghgf000gfh&0000abcvcvddf=test%2Fname

(where 0000abcvcvddf_lkid is html id of the lookup field)

This worked fine in the initial Org , but after we moved it to a packaging Org, its failed becasue the html Id wont be the same for each and every Org. But in the documents given by Salesforce they said "they have no plans to change the html ids but they cannot guarantee it too".

 

please find the Building Custom Links doc in the following

https://na1.salesforce.com/help/doc/en/salesforce_web_integration_links.pdf

verynewbieverynewbie
Did you get this??I am in same page now.ANy help?
yvk431yvk431

hmm to be frank, I dont remember what I did then , its more than 3 years now,  but still cant we just customize the field id in the custom label and call it in the button script or where ever we are redirecting ?

 

--yvk