I’m finishing a plugin but have one small problem.
I’m trying to use a lookup field with a if.
When I try with the == operator, nothing happens. With the Equals() method, I got an exception saying the object is not instantiated…What am I doing wrong?
The plugin is executed on pre-operation when a record is created.
EntityReference modeleContrat = (EntityReference)target.Attributes["new_modeldecontrat"];
throw new InvalidPluginExecutionException(modeleContrat.Name);
if (modeleContrat.Name == ("Contrat d'unité"))
{
I’m assuming the code you put into the question is used by you to test somehow as it currently wont run due to the exception.
The error you’re getting
Object not Instantiatedmeans exactly what it says on the tin, there is no value in yourmodeleContratvariable.So the code should be a bit more defensive, like this:
As for
modeleContratnot being instantiated. As your plugin is running on Pre-Create and you are retrieving the attribute fromTargetI’m assuming that somewhere your taking the input parameters out of the plugin context to retrieve it.This is fine, however
Targetwill only contain the attributes that are set on the record being saved. If you don’t set it on the record, it will be null.