Mark-sai Respected Contributor.
Respected Contributor.
667 views

.Net SDK check if the custom field definition exist in RM

I am using  below code to assign custom field value using .Net SDK

FieldDefinition fdDef = new FieldDefinition(rmDB, fieldName);
UserFieldValue ufVal = new UserFieldValue(fieldValue);
rmRecord.SetFieldValue(fdDef, ufVal);

But before setting the value, i need to check if 'fieldName' exist within RM as it will throw error if it is not valid. How do I check if FieldDefination is valid?

0 Likes
4 Replies
Highlighted
Mark-sai Respected Contributor.
Respected Contributor.

Re: .Net SDK check if the custom field definition exist in RM

One way I am currently doing is applying try and catch block around that code and doing nothing inside catch block. 

try
{
FieldDefinition fdDef = new FieldDefinition(rmDB, fieldName);
UserFieldValue ufVal = new UserFieldValue(fieldValue);
rmRecord.SetFieldValue(fdDef, ufVal);
}
catch
{

}

In this way, if the field is not exist or valid, it will simply bypass assigning the value and does not break the code. But there must be a better way to validate this.

0 Likes
JanMartin Outstanding Contributor.
Outstanding Contributor.

Re: .Net SDK check if the custom field definition exist in RM

Have used this method before, it is very slow for many objects/fields.

0 Likes
Micro Focus Expert
Micro Focus Expert

Re: .Net SDK check if the custom field definition exist in RM

You can also use FindTrimObjectByName() which will return a null if the field is not found.

FieldDefinition fld = db.FindTrimObjectByName(BaseObjectTypes.FieldDefinition, "my field") as FieldDefinition;

if (fld != null)
{

}

You might also be tempted to do search for all FieldDefinitions at the start of the process and cache them, then you ust need to call FeildDefinition.IsUsedFor(0 to verify that the Record uses that Field before setting the value.


Blog | Samples | HPE CM 9.3 SDK Docs
**Any opinions expressed in this forum are my own personal opinion and should not be interpreted as an official statement on behalf of MicroFocus**

Re: .Net SDK check if the custom field definition exist in RM

You can also get all field definitions this way (used also for other objects):

TrimMainObjectSearch fds = new TrimMainObjectSearch(db, BaseObjectTypes.FieldDefinition);
TrimSearchClause sc = new TrimSearchClause(db, BaseObjectTypes.FieldDefinition, SearchClauseIds.All);
fds.AddSearchClause(sc);

Now you can export names and other properties that you need:

List<string> userFieldsCM = new List<string>(); 
foreach (var fd in fds)
{
    userFieldsCM.Add(((FieldDefinition)fd).Name);
}

Mind the types.

The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.