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
SalesforceJimSalesforceJim 

Dynamically Accessing sObject Fields

Hi,

 

I am trying to writh a dymanic class method, which would allow me to pass the sObject and field name as parameters in order to return the value from a single field. I have found the reverences to dynamic SOQL in the apex docs and am able to run a dynamic query to return an sObject which represents a single record of an Account type.

 

What I am having trouble with is then how to extract the field from that sObject (using the dynamic reference to the field name passed to the method) and return this out of the method as a string.

 

 

 

This is my current class method. At the moment, it is only dynamic for the field, and the sObject is hard-coded as being an Account. I have tried using the DescribeFieldResult method but believe this is not the correct approach.

 

private Object getObject(Id i, String s){

 

String r;

String sQuery = 'SELECT Id,' + s + 'FROM Account WHERE Id:= id';

Account a = Database.query(sQuery);

// Schema.DescribeFieldResult f = Schema.sObjectType.Account.fields.AS400_ID__c;

//Schema.DescribeFieldResult f = Account.AS400_ID__c.getDescribe();

//r = f.getDefaultValue();

return r;

}

 

Thanks

 

Jim

SalesforceJimSalesforceJim

Just to update on my post. I am trying to call the class as follows getAccountFields.getObject(cr.Property__c, 'AS400_ID__c') in order to add the return value as part of a string (on an email subject).

 

I have 2 problems

 

1. I am returning an object, not a string.

2. The gall to this method generates the following error:
Save error: Method does not exist or incorrect signature: getAccountFields.getObject(Id, String), but I think this is because it is returning an object, when the call is being used in place of a String value.

The method is currently

 

 public Object getObject(Id id, String s){

  String r;

  String sQuery = 'SELECT Id,' + s + 'FROM Account WHERE Id:=' + id;   Account a = Database.query(sQuery);

  Object o = a.get(s);

  return o;

//Problem is that this method is returning an object and not a string

}

Michael_KahleMichael_Kahle

Hi Jim,

 

you need to Cast the Object into a String.

 

 

public String getObject(Id id, String s) { String sQuery = 'SELECT Id,' + s + ' FROM Account WHERE Id = \'' + id +'\''; Account a = Database.query(sQuery); return String.ValueOf(a.get(s)); }

 

 This will only return a String, so it won't work with DateTime, Decimal or other kinds of fields.

 

SalesforceJimSalesforceJim

Hi Michael,

 

Thank you for your response. This is great.

 

I was looking for information on casting in the Apex reference but not looking in the right area!

 

Is this the Static String method where the argument in my case 'a.get(s)' is of type 'anyType x*'. I am assuming this is the case, as the return type of the get method on an sObject is an Object and there is no explicit conversion from Object to String.

 

I now have the issue that my call to the method still gives the same error when I save the class in Eclipse.

 

I am saying: 

getAccountFields.getObject(cr.Property__c, 'AS400_ID__c')

 

Which generates the message;

Method does not exist or incorrect signature: getAccountFields.getObject(Id, String) 

 

Is this the correct way of referenceing a method within a class.

 

Thanks so much for your help on this so far

 

 

 

 

Michael_KahleMichael_Kahle

Hi Jim,

 

to resolve this issue i would need to know what exactly "getAccountFields" is. Is it a method which returns a class? Is it a variable or attribute name?

 

In case of a Variable or Attribute i need to view the rest of the code to give an answer.

 

But, if it is a method which returns the class, you just forgot the method call brackets :

 

 

String AS400_ID = getAccountFields().getObject(cr.Property__c, 'AS400_ID__c');

 


 

 

SalesforceJimSalesforceJim

Hi Michael

 

getAccountFields is a public class and gotObject is a public method within this class. The ideas is that this would provide a re-0usable way of returning field values from a specific account.

 

The full call to the method is

 

mail.setSubject(CRRecordTypes.get(cr.RecordTypeId).Name +' Request' + ' [AS400 ID: ' + getAccountFields.getObject(cr.Property__c, 'AS400_ID__c') + '+ CR: ' + cr.Name + ' ]');

 

Regards

 

Jim

Michael_KahleMichael_Kahle

Hi Jim,

 

you can not call a method from a class without instanciating it first or declare the method static.

 

 

So there are two possibitities for you now.

 

First is instanciating the Class like this :

 

 

// Start of the trigger/Controller/Class/etc getAccountFields aFields = new getAccountFields(); // change the assignmentline to this mail.setSubject(CRRecordTypes.get(cr.RecordTypeId).Name +' Request' + ' [AS400 ID: ' + aFields.getObject(cr.Property__c, 'AS400_ID__c') + '+ CR: ' + cr.Name + ' ]');

 

 

 

Second is declare the method as a static method :

 

 

public static String getObject(Id id, String s) { String sQuery = 'SELECT Id,' + s + ' FROM Account WHERE Id = \'' + id +'\''; Account a = Database.query(sQuery); return String.ValueOf(a.get(s)); }

 

In your case i would recommend to change the method to a static method.

 

 

SalesforceJimSalesforceJim

Thanks Michael,

 

This was indeed the problem and I have now resolved all of these issues and deployed my code.

 

You have been a great help, so thanks once again.

 

James

Seema ASeema A
public static String getObject(Id id, String s)
{
String sQuery = 'SELECT Id,' + s + ' FROM Account WHERE Id = \'' + id +'\'';
Account a = Database.query(sQuery);
return String.ValueOf(a.get(s));
}


I want to display values in my page, also I should be able to edit them , how it can be done since I can't access sObject fields on my page.