You need to sign in to do that
Don't have an account?
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"}
{
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;
}
using .NET 2.0...
why wouldn't it work in 2.0?
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!
Justin,
Your post just saved me hours..
Thank you....