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
Austin P.ax231Austin P.ax231 

object.GetType().GetField Null Value being returned

All,

I'm getting a null value being returned when utilizing the GetField method:

Code snippet below:

System.Reflection.FieldInfo fi = sObj[j].GetType().GetField("Booked_Revenue_Goal__c",System.Reflection.BindingFlags.IgnoreCase);

When I look at the sObj[j].GetType() results I notice some errors that have been returned (Highlighted in RED).

Any advice will be appreciated.  Thanks in advance!

{Name = "Participant__c" FullName = "RSM.BDC3.DataAccess.SFDC.Participant__c"}

[System.RuntimeType]: {Name = "Participant__c" FullName = "RSM.BDC3.DataAccess.SFDC.Participant__c"}

base {System.Reflection.MemberInfo}: {Name = "Participant__c" FullName = "RSM.BDC3.DataAccess.SFDC.Participant__c"}

Assembly: {RSM.BDC3.DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}

AssemblyQualifiedName: "RSM.BDC3.DataAccess.SFDC.Participant__c, RSM.BDC3.DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

Attributes: Public | Serializable | BeforeFieldInit

BaseType: {Name = "sObject" FullName = "RSM.BDC3.DataAccess.SFDC.sObject"}

ContainsGenericParameters: false

DeclaringMethod: 'sObj[j].GetType().DeclaringMethod' threw an exception of type 'System.InvalidOperationException'

DeclaringType: null

FullName: "RSM.BDC3.DataAccess.SFDC.Participant__c"

GenericParameterAttributes: 'sObj[j].GetType().GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'

GenericParameterPosition: 'sObj[j].GetType().GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'

GUID: {4391e7b1-c90f-3df1-9475-c383263e473a}

HasElementType: false

IsAbstract: false

IsAnsiClass: true

IsArray: false

IsAutoClass: false

IsAutoLayout: true

IsByRef: false

IsClass: true

IsCOMObject: false

IsContextful: false

IsEnum: false

IsExplicitLayout: false

IsGenericParameter: false

IsGenericType: false

IsGenericTypeDefinition: false

IsImport: false

IsInterface: false

IsLayoutSequential: false

IsMarshalByRef: false

IsNested: false

IsNestedAssembly: false

IsNestedFamANDAssem: false

IsNestedFamily: false

IsNestedFamORAssem: false

IsNestedPrivate: false

IsNestedPublic: false

IsNotPublic: false

IsPointer: false

IsPrimitive: false

IsPublic: true

IsSealed: false

IsSerializable: true

IsSpecialName: false

IsUnicodeClass: false

IsValueType: false

IsVisible: true

MemberType: TypeInfo

Module: {RSM.BDC3.DataAccess.dll}

Namespace: "RSM.BDC3.DataAccess.SFDC"

ReflectedType: null

StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute}

TypeHandle: {System.RuntimeTypeHandle}

TypeInitializer: null

UnderlyingSystemType: {Name = "Participant__c" FullName = "RSM.BDC3.DataAccess.SFDC.Participant__c"}

justin.mahjustin.mah
Funny, I'm getting the exact same problem as you Austin
 
I tried following the code from here
 
 
private DataTable CreateDataTable(String[] fieldList, QueryResult qr, String tableName)
{
    DataTable dt = new DataTable(tableName);
    for (int i=0;i<fieldList.Length;i++)
    {
        dt.Columns.Add(fieldList[i]);
    }
    for (int i=0;i<qr.records.Length;i++)
    {
        DataRow dr = dt.NewRow();
        sObject record = qr.records[i];
        for (int j=0;j<fieldList.Length;j++)
        {
            System.Reflection.FieldInfo fi = record.GetType().GetField(fieldList[j]);
            dr[fieldList[j]] = fi.GetValue(record);
        }
        dt.Rows.Add(dr);
    }
    return dt;
}
 
But it's always returning a NULL value.
However if I place a watch on the record object, you can see the fields and the values of the fields of interest. I'm also curious as to how to pull this data back dynamically.
SuperfellSuperfell
Are you using .NET 1.1 or 2.0 (I believe DevAngels code was for 1.1)
justin.mahjustin.mah

using .NET 2.0...

why wouldn't it work in 2.0?

justin.mahjustin.mah
if it doesn't work in .NET 2.0, is there any way around it? I'm or do I have to download the partner wsdl?
SuperfellSuperfell
because the proxy classes generated in .NET 2.0 have a different structure to the ones in 1.1 (in 1.1 there are fields, in 2.0 the fields are private and there are property getters/setters).
SuperfellSuperfell
You should be able to make it work in .NET 2.0, look at the generated classes, and use the relevant reflection methods to access the getters on the properties instead of the fields.
justin.mahjustin.mah

Thanks SimonF,

It took me a little while to hunt down the methods on how to get the data back, so I figured anyone looking at this thread can benefit from the code that I came up with that works for .NET 2.0

private DataTable CreateDataTable(String[] fieldList, sforce.QueryResult qr)
{
   DataTable dt = new DataTable();
   for (int i = 0; i < fieldList.Length; i++)
   {
      dt.Columns.Add(fieldList[i]);
   }
   for (int i = 0; i < qr.records.Length; i++)
   {
      DataRow dr = dt.NewRow();
      sforce.sObject record = qr.records[i];
      for (int j = 0; j < fieldList.Length; j++)
      {
         dr[fieldList[j]] = record.GetType().GetProperty(fieldList[j]).GetGetMethod().Invoke(record, null);
      }
      dt.Rows.Add(dr);
   }
   return dt;
}

The GetProperty has a GetGetMethod, so basically you have a method that you can invoke to get the data from the field you are interested in. You need to pass in the record object, and null as its parameters (no parameters are required for a get method).

The only other thing to watch for is that I found that the string you specify in GetProperty is case sensitive. It must match exactly as how Salesforce sees that field.

I'm sure all you advanced people know this, but I hope this helps the newbies like me!

saariko.ax241saariko.ax241
Justin, I love you !!!  :-)
 
 
been trying to get this code working.   thanks.
BrianO'HalloranBrianO'Halloran

Justin,

   Your post just saved me hours.. 

Thank you....