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
IWyattIWyatt 

C# - Finding if object.FieldArray contains a Field.Name = "OwnerId"

Hey folks, I'm looping through a field array, checking to see if each and every field has a name property equal to "OwnerId" in order to answer the question "Does the object have an OwnerID"?

 

I want to optimize (less code, more efficient) this code a little bit. Do you have any suggestions?

the object "describeobject" in the first line is a DescribeSObjectResult.

sf.Field[] objectfields = describeobject.fields;

bool hasownerfield = false;
foreach (sf.Field objectfield in objectfields)
{
if (objectfield.name == "OwnerId")
{
hasownerfield = true;
}
}

 

Many thanks,

Isaac

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
You have to loop the array at least once, you may find that building a dictionary from field name to field to be generally helpful, although if all you're trying to find out is if there's that one particular field, you're about there (you can break out of the loop once you find the field, no need to keep going).