Community
Showing results for 
Search instead for 
Do you mean 
Reply

System.ArgumentNullException in SetValue

Tuned Listener
Posts: 6
Country: United States

System.ArgumentNullException in SetValue

Hi!

 

I'm new to ACT! and tried spending time doing examples using the sdk. Now the problem I encountered upon discovering things was that when I SetValue to my custom opportunity field, I get System.ArgumentNullException. I wonder why it'd thrown that error. Here are my codes which I tried to use:

 

                 Act.Framework.Opportunities.OpportunityList ol = ACTFM.Opportunities.GetOpportunities(null);

                MutableEntityFieldDescriptor oppF = ACTFM.Opportunities.GetMutableEntityFieldDescriptor("MyCustomField1", FieldNameType.Alias);
                MutableEntityFieldDescriptor oppF2 = ACTFM.Opportunities.GetMutableEntityFieldDescriptor("MyCustomField1", FieldNameType.Alias);
               
                foreach (Act.Framework.Opportunities.Opportunity op in ol)
                {
                    MessageBox.Show(oppF.Name);

                    MessageBox.Show(oppF2.Name);

            try

                {
                    //op.Fields[oppF.Name, true] = "123123";
                    //op.OpportunityFields[oppF.Alias, FieldNameType.Alias] = "123";


                    oppF.SetValue(op, "123");
                    oppF2.SetValue(op, true);


                    //op.Fields[oppF.Name, true] = "123123";                   
                   // op.Fields["MyCustomField1", FieldNameType.Alias] = "123";
                    op.Update();
                  }
                   catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }

 

                 }

    What I really want is to set a value for my custom field in the opportunity table. oppF  and oppF2 are not null because they return a field name.  I also tried those commented statements but still throw the same error. What am I doing wrong? 

 

Here is the Error:

 

System.ArgumentNullException: Value cannot be null.

Parameter name: format

    at System.String.Format(IFormatProvider provider, String format, Object[] args) 

    at System.String.Format(String format, Object arg0, Object arg1)

    at Act.Framework.Opportunities.Opportunity.OpportunityFieldCollection.SetData(MutableEntityColumn column, Object data)

    at Act.Framework.MutableEntities.MutableEntity.FieldCollection.set_Item(String name, FieldNameType fieldNameType, Object value) 

Tuned Listener
Posts: 6
Country: United States

Re: System.ArgumentNullException in SetValue

In addition to my post, I tried to make another application to set data to Opportunity Field 1. I could set data to it programmatically  whether its in full-access or read-only but what happened was after I restart my application, and write data to it, it'd thrown System.ArgumentNullException. However, I noticed that if I write data to a  read-only field with a data on it, it would throw the error. Probably read-only fields can only be set if it is empty..

 

Any ideas regarding this matter?

 

Thanks in advance! 

New Member
Posts: 1
Country: Canada

Re: System.ArgumentNullException in SetValue

Just hit similar problem myself so posting to this old thread hoping it may help the next guy.

 

This exception hit my project when there was an IMPLIED TYPE CONVERSION between the new value and the field type. In my case the example was a NUMBER field but the incoming value was the STRING representation.

 

The resolution shown here explicitly checks and changes the value to match the field type prior to calling SetValue(). (There may be a more elegant solution available ... but this is pretty easy in most circumstances.)

 

TIP: The example conversion may not suffice for all situations. In "real" code you may want to check for other conversion issues, such as null value and using the FieldDescriptor to determine if null is permitted or resetting to default value, etc.)

 

Example code:

 

---

 

  object newValue = (incoming value)

  Opportunity myOpportunity = (target entity)

 

  MutableEntityFieldDescriptor fd;

  fd = _framework.Opportunities.GetMutableEntityFieldDescriptor(

      "MyField", FieldNameType.Alias);

 

  // Explicit type check

  if ( newValue.GetType() == fd.PropertyType )

  {

      fd.SetValue( myOpportunity, newValue );

  }

  else

  {

      // explicit convert prior to setting the value

      object convertedValue = Convert.ChangeType( newValue, fd.PropertyType );

      fd.SetValue( myOpportunity, convertedValue );

  }

 

 

---

Aside: Also stumbled upon the (apparently faulty) GetOpportunityFieldDescriptor() function(s) <grrr> - resolved by using the generic / parent function GetMutableEntityFieldDescriptor().