I have a C# program where I have 3 ListBoxes. The 1st list includes all CustomObjects. The 2nd list displays all of the CustomField’s for the selected item in the 1st list. The 3rd list should show the fields of a Lookup type CustomField selected in the 2nd ListBox if in fact the type of the CustomField is Lookup (e.g. Lookup type Contact).
Please refer to the source code below.
I am using the Metadata API for this and am having difficulty in 2 areas.
The first problem is because I retrieve the items for the 2nd list using the Metadata API call FileProperties[] lmr = binding.listMetadata(…, I can only get the Fullname string property for each CustomField. I would like to know the type of the CustomField (e.g. type Lookup) and only populate the 3rd list in this case. The FileProperties object doesn’t include this information, just the name of the Object or Field.
The 2nd problem is that I haven’t found a way to display the fields of a Lookup reference object, for example a lookup type of Contact.
Can I retrieve this information using the MetaData API?
In the example C# code below, GetTables lists all of the CustomObjects, and GetFields lists all of the CustomField’s for the selected CustomObject. This all works fine.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using _SFMeta = MyCompany.SFMetadata;
using System.Threading;
namespace MyCompany.Service
{
class SforceMetadata
{
public static List<string> GetTables()
{
List<string> tables = new List<string>();
try
{
_SFMeta.ListMetadataQuery query = new _SFMeta.ListMetadataQuery();
query.type = "CustomObject";
double asOfVersion = 29.0;
_SFMeta.MetadataService binding = new _SFMeta.MetadataService();
_SFMeta.SessionHeader sessHdr = new _SFMeta.SessionHeader();
sessHdr.sessionId = SforceService.SessionID;
binding.SessionHeaderValue = sessHdr;
_SFMeta.FileProperties[] lmr = binding.listMetadata(new _SFMeta.ListMetadataQuery[] { query }, asOfVersion);
if (lmr != null)
{
foreach (_SFMeta.FileProperties n in lmr)
{
tables.Add(n.fullName);
}
}
else
tables.Add("No CustomObject items");
}
catch (SoapException e)
{
tables.Add("Error: " + e.Message);
}
tables.Sort();
return tables;
}
public static List<string> GetFields(string tableName)
{
List<string> fields = new List<string>();
try
{
_SFMeta.ListMetadataQuery query = new _SFMeta.ListMetadataQuery();
query.type = "CustomField";
double asOfVersion = 29.0;
_SFMeta.MetadataService binding = new _SFMeta.MetadataService();
_SFMeta.SessionHeader sessHdr = new _SFMeta.SessionHeader();
sessHdr.sessionId = SforceService.SessionID;
binding.SessionHeaderValue = sessHdr;
_SFMeta.FileProperties[] lmr = binding.listMetadata(new _SFMeta.ListMetadataQuery[] { query }, asOfVersion);
if (lmr != null)
{
foreach (_SFMeta.FileProperties n in lmr)
{
if (n.fullName.Length > tableName.Length + 1)
{
if (n.fullName.Substring(0, tableName.Length) == tableName)
{
fields.Add(n.fullName.Substring(tableName.Length + 1, n.fullName.Length - tableName.Length - 1));
}
}
}
}
else
fields.Add("No CustomField items");
}
catch (SoapException e)
{
fields.Add("Error: " + e.Message);
}
fields.Sort();
return fields;
}
}
Thank you