You are viewing limited content. For full access, please sign in.

Question

Question

How to test FieldValueCollection

SDK
asked on May 8, 2018

I am trying to reassign values to a new template using the code shown below. In some cases, I am finding null values. Usually this is no big deal, just test for null and move on. As you can see, I am trying all the built in tests TryGetValue and ContainsField. The output is null but it is not detected and the Boolean methods return true.

Any ideas around this?

I get the following

ERROR: System.NullReferenceException: Object reference not set to an instance of an object

 using (EntryInfo myEntry = Entry.GetEntryInfo(Convert.ToInt32(newRow.Id), session)) // get an entry
 {
      FVC.Clear();
      FVC = myEntry.GetFieldValues();
      for (int j = 0; j < FVC.Count; j++)
      {
           if (FVC.PositionToName(j) == "Cancelled Status" && FVC.TryGetValue("Cancelled Status",out outval) &&             FVC.ContainsField("Cancelled Status"))
            {                                       
                  if (!string.IsNullOrEmpty(FVC[j].ToString()) )
                                            template.cancelledStatus = FVC[j].ToString();
            }

 

0 0

Replies

replied on May 8, 2018

Do you ever set up the "outval" object that is used in the

FVC.TryGetValue("Cancelled Status",out outval)

Also, it seems that that call is not even needed.  I have not tested it, but I would try to attack it slightly different. Like this:

using (EntryInfo myEntry = Entry.GetEntryInfo(Convert.ToInt32(newRow.Id), session)) // get an entry
{
   FVC.Clear();
   FVC = myEntry.GetFieldValues();
   if (FVC.ContainsField("Cancelled Status"))  // Only process it if the fvc has a Cancelled Status field
   {  
      for (int j = 0; j < FVC.Count; j++)
      {
         // Loop through fields to find the one you want
         if (FVC.PositionToName(j) == "Cancelled Status") // && FVC.TryGetValue("Cancelled Status",out outval) && FVC.ContainsField("Cancelled Status"))
         {                                       
            if (!string.IsNullOrEmpty(FVC[j].ToString()) )
               template.cancelledStatus = FVC[j].ToString();
         }
      }
   }
}

Then when you step through the code, where does it through the error?

replied on May 8, 2018

In your code, you need to get the FVC from the entry and modify the field value in the FVC.  Then push the FVC with the new value(s) back to the entry.

0 0
replied on May 8, 2018

Thanks for the reply.

The problem is that the FVC does not allow you to read a null value. I ended up declaring a variable of type object and assigning it with object obj = EntryInfo.GetFieldValue("field_name"), then testing obj for null.

string value = (outval != null) ? outval.ToString() : string.Empty;

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.