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
ran67ran67 

retrive a Query using sobject

HI

How to declare a query by using sobject  in salesforce .

for releated list in salesforce

bob_buzzardbob_buzzard

Are you looking to retrieve related records based on a particular record instance?  If so, you can use something like the following (account/contacts used in this example).

 

Assuming you have the account record in a variable called 'acc', you can execute:

 

List<Contact> conts=[select id, FirstName, LastName from Contact where AccountId=:acc.id];

 

ran67ran67

no i want to retrive a query by using the sobject can u help me without hardcording the contact here

thanks in advances

bob_buzzardbob_buzzard

Ah, so you are looking to create a dynamic soql query where the sobject name isn't known until run time?

ran67ran67

yes am trying for it only can u help me for that

 

 

thanks in advance

 

bob_buzzardbob_buzzard

You can do this with dynamic soql:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_soql.htm

 

If you have the sobject name in a string, e,g, 'sobjname', you can then query back the record via something like:

 

String queryStr='select id from ' + sobjname;

List<sobject> = Database.query(queryStr);

 

ran67ran67

hi

here how can i make the contact has dynamic sobject can u please help me to do this

public class contactExtension {
    private final Contact c; //User sobject
    
    //initializes the private member variable u by using the getRecord method from the standard controller
    public contactExtension(ApexPages.StandardController stdController) {
        this.c = (Contact)stdController.getRecord();
    }
    
    //builds a picklist of account names based on their account id
    public List<selectOption> getaccts() {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
        options.add(new selectOption('', '- None -')); //add the first option of '- None -' in case the user doesn't want to select a value or in case no values are returned from query below
        for (Account account : [SELECT Id, Name FROM Account]) { //query for Account records
            options.add(new selectOption(account.id, account.Name)); //for all records found - add them to the picklist options
        }
        return options; //return the picklist options
    }
}

thanks in advance

bob_buzzardbob_buzzard

Sorry, I don't understand what you are asking for.  Is there a part of the code you have posted that you are looking to change?

ran67ran67

yes the part of the code only 

see here i have been   hardcoded the account now i want to use the dynamic sobject instead of account  how can i write for it 

Query

please help me

controller

 public List<selectOption> getaccts() {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
        options.add(new selectOption('', '- None -')); //add the first option of '- None -' in case the user doesn't want to select a value or in case no values are returned from query below
        for (Account account : [SELECT Id, Name FROM Account]) { //query for Account records
            options.add(new selectOption(account.id, account.Name)); //for all records found - add them to the picklist options
        }
        return options; //return the picklist options
    }

bob_buzzardbob_buzzard

How will your code know the name of the sobject to be retrieved?

ran67ran67

actually i want to  to pass sobject name in visualforce page  and i will use the component 

can u guide me how to write  a query for

it

thanks in advance

bob_buzzardbob_buzzard

So if you have the sobject name, you can retrieve the details as I posted earlier - that built a query string based on the sobject name being available in a variable.

asish1989asish1989

Hi.....

  you want to change on this line ....?

 

    for (Account account : [SELECT Id, Name FROM Account]) { //query for Account records
            options.add(new selectOption(account.id, account.Name)); //for all records found - add them to the picklist options
        }

 

so try this...

  String accts = 'SELECT Id, Name FROM Account';
        
   List <sObject> slist  = Database.query(accts);

 

   for (sObject s:slist) { //query for Account records
            options.add(new selectOption(s.Id, s.Id)); //for all records found - add them to the picklist options
        }

 

By Using this Account Name Pick list Account Id is comming Name Is not comming.

Here I have given s.id that's why Id is comming but If I give s.Name then an error is comming

Field expression not allowed for generic SObjec

 

so please help me ....

 

Thanks

  asish

asish1989asish1989

Hi

   I got the solution..of upcomming error..

  I need to write...

   options.add(new selectOption(s.Id, String.ValueOf(s.get('Name'))));

 

so total  code is...

     public List<selectOption> getaccts() {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
        options.add(new selectOption('', '- None -')); //add the first option of '- None -' in case the user doesn't want to select a value or in case no values are returned from query below
        /*for (Account account : [SELECT Id, Name FROM Account]) { //query for Account records
            options.add(new selectOption(account.id, account.Name)); //for all records found - add them to the picklist options
        }*/
       String accts = 'SELECT Id, Name FROM Account';
       

   List <sObject> slist  = Database.query(accts);

 

   for (sObject s:slist) { //query for Account records
            options.add(new selectOption(s.Id, String.ValueOf(s.get('Name')))); //for all records found - add them to the picklist options
        }
        return options; //return the picklist options
    }

 

Did this post solve your problem then please mark it solved

 

 

Thanks

asish

ccusicccusic
//Record is some sObject record passed in with at least an Id
String objectName = record.getSObjectType().getDescribe().getName();
String recordId = record.Id;
String query = 'SELECT Id, Name FROM ' + objectName + ' WHERE Id = :recordId LIMIT 1';
record = (sObject) Database.query(query);
String recordName = (String) record.get('Name');
This should work without having to typecast the sObject, keeping it generic