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
maz00maz00 

Picklist fields

Hi everyone,

I just used the describeSObject to grab the available values for my picklists.  However, I don't want to query the entire list of fields just to get one.  Is it possible to just query 1 field and get the available fields just for that? Here is my code which is similar to the API documentation? BTW, I know I can use drs.fields[number in here] however, I need to use something like dsr.fields["name"] instead. Can anyone help please?

sforce.DescribeSObjectResult dsr = binding.describeSObject("Account");
Response.Write("Name: " + dsr.name + "<br>");
for (int i=0; i < dsr.fields.Length;i++)
{
sforce.Field field = dsr.fields[i]; //////////////////// Here I want to just use the field name?????????? so I don't have to query the entire table
Response.Write("Field name: " + field.name + "<br>");
Response.Write("Field label: " + field.label + "<br>");
if (field.nameField) Response.Write("This is a name field");
if (field.restrictedPicklist) Response.Write("This is a restricted picklist");
if (field.custom) Response.Write("This is custom field");
if (field.type.Equals(sforce.fieldType.picklist))
{
 Response.Write("Picklist values: <br>");
for (int j=0; j < field.picklistValues.Length; j++)
 Response.Write(">>" + field.picklistValues[j].
value + "<br>");
}
}

 

Thank you
Maz.

DevAngelDevAngel

Hi maz00,

There is no way to have the fields array deserialized into a hashtable or other collections automatically. The best way to handle this is to cache you DescribeSObject results into a structure that will work for you. Examine the following code.  Add the new class MyCache to your project.  This is a very simple class with one static field to hold cached data.  On a windows form add a button to the form and past in the button1_Click code (be sure to wire up the click event).  Add the other two functions.  Once you have this comiled without errors, run the form and click the button.  You should notice about a 2 second delay.  This is for login and describe.  Click the button again.  You'll notice a negligable delay.  This little sample also demonstrates caching the entire describeSObject results as well as caching your binding stub.


public class MyCache
{
      public static System.Collections.Hashtable Cache = new System.Collections.Hashtable();
      public MyCache() { }
}

private void btnHashtable_Click(object sender, System.EventArgs e)
{
      sforce.SforceService binding = null;
      if (!MyCache.Cache.ContainsKey("binding")) 
      {
            binding = new Tester.sforce.SforceService();
            sforce.LoginResult lr = binding.login("username", "password");
            binding.SessionHeaderValue = new Tester.sforce.SessionHeader();
            binding.SessionHeaderValue.sessionId = lr.sessionId;
            binding.Url = lr.serverUrl;
            MyCache.Cache.Add("binding", binding);
      }
      binding = (sforce.SforceService)MyCache.Cache["binding"];

      sforce.PicklistEntry[] pl = GetPickList(binding, "Lead", "Industry");
      for (int i=0;<pl.Length;i++)
            System.Diagnostics.Trace.WriteLine(pl[i].label + " - " + pl[i].value);
}

private Hashtable PutFieldsInHashtable(sforce.DescribeSObjectResult dsr)
{
      Hashtable ht = new Hashtable();
      for (int i=0;i<dsr.fields.Length;i++) 
            ht.Add(dsr.fields[i].name, dsr.fields[i]);
      return ht;
}

private sforce.PicklistEntry[] GetPickList(sforce.SforceService binding, string type, string fieldName)
{
      Hashtable fields = null;

      if (!MyCache.Cache.ContainsKey(type + "Fields"))
      {
            sforce.DescribeSObjectResult dsr = null;
            if (!MyCache.Cache.ContainsKey(type + "Describe")) 
            {
                  dsr = binding.describeSObject(type);
                  MyCache.Cache.Add(type + "Describe", dsr);
            }
            dsr = (sforce.DescribeSObjectResult)MyCache.Cache[type + "Describe"];
            MyCache.Cache.Add(type + "Fields", PutFieldsInHashtable(dsr));
      }
      fields = (Hashtable)MyCache.Cache[type + "Fields"];

      return (sforce.PicklistEntry[])((sforce.Field)fields[fieldName]).picklistValues;
}

pwiedlerpwiedler

Hi Dave,

I am looking at doing the same thing, although I would like to take it a step further, I would like to pull info from the account table using a custom table but I only want the information if it is = to active.

example:  select from account name, billingCity, BillingState where status__c = 'active'

I always get an error.  the field is a picklist. also I ran the code below at ran into an error with the final piece of click code,

      sforce.PicklistEntry[] pl = GetPickList(binding, "Lead", "Industry");
      for (int i=0;            System.Diagnostics.Trace.WriteLine(pl[i].label + " - " + pl[i].value);

It keeps telling me that ; was expected after for(int i=0;< and a ) expected after Length; and again after the i++) states invalid expression term )

I am new to both the WSDL and c#

Thanks

DevAngelDevAngel

Hi pwiedler,

What is the error that is being returned from you query?

 

As to the second error (a compile error?):

That line was munged by the forum server.  The line should read:

for (int i=0;i<pl.Length;i++) {

with the

System.Diagnostices.Trace.WriteLine(....)

being on the next line.

 

pwiedlerpwiedler

OK, I am really confused now, I just ran it on the demo and it returned the correct list. yesterday I kept getting that is was an invalid object.

pwiedlerpwiedler

Hi Dave, sorry to keep this thread going.

This is the code I use to populate the dropdown list,

private void button1_Click(object sender, System.EventArgs e)

{

sforce.SforceService binding = null;

if (!MyCache.Cache.ContainsKey("binding"))

{

binding = new Contract.sforce.SforceService();

sforce.LoginResult lr = binding.login(this.txtUsername.Text, this.txtPassword.Text);

binding.SessionHeaderValue = new Contract.sforce.SessionHeader();

binding.SessionHeaderValue.sessionId = lr.sessionId;

binding.Url = lr.serverUrl;

MyCache.Cache.Add("binding", binding);

}

binding = (sforce.SforceService)MyCache.Cache["binding"];

sforce.PicklistEntry[] pl = GetPickList(binding, "account", "Status__c");

for (int i=0;i<pl.Length;i++)

{

this.cmbCompany.Items.Add(pl[i].label + " - " + pl[i].value);

}

}

how can I tell it that I only want companies that are avctive?

 

Thank You