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];
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 } }
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 }
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.
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
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
//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
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:
no i want to retrive a query by using the sobject can u help me without hardcording the contact here
thanks in advances
Ah, so you are looking to create a dynamic soql query where the sobject name isn't known until run time?
yes am trying for it only can u help me for that
thanks in advance
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:
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
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?
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
}
How will your code know the name of the sobject to be retrieved?
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
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.
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
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