Find your content:

Search form

You are here

How to get an Id of another object

 
Share

I have a Soql Query where I need to get an id of another object. Here is the query

list<object1__c> odj = [select field1__c from object1__c where object2__c=:dataid];

If it was in the same object1__c then we can get it by

dataid = apexpages.currentpage().getparameters().get('dataid');

But here Im getting it from the object2...Then how can I get it


Attribution to: Eagerin Sf

Possible Suggestion/Solution #1

Thanks for providing more clarity on your question, can you update the question itself with the code snippets so its easier to see what your asking.

Firstly you need to query for the Id and Name, then pass these to the SelectOption. This will ensure that when you need to query object2 by object1 you have the correct Id.

public list<selectoption> gettempname() {
    list<selectoption> option = new list<selectoption>(); 
    for(CnP_PaaS__CnP_Designer__c design : [select id, name from CnP_PaaS__CnP_Designer__c limit 10] ) 
       option.add(new selectoption(design.id,design.name));
    return option; 
}

Given your comment you are binding your apex:selectList value to a controller variable called 'Template'. You will then find once your page is submitted back (either via apex:commandButton or apex:actionSupport) this will contain the Id of the select item from the picklist. Thus you will have what you need to able to query object2 by the selected Id of object1. Something like this...

List<Object2__c> results = [Select Name from Object2__c where Object1__c = :Template];

If you update your question with a better overview of what your doing, I can make my answer a bit more specific. But hopefully you get the key aspect, which is to populate your picklist / selectlist with the Id and Name. Hope this helps!


Attribution to: Andrew Fawcett
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3931

My Block Status

My Block Content