I have an entity named ace_s_main which contains three fields.
- The first field is a text box named ace_name
- The second field is a lookup field named ace_mystate
- The third field is a textbox named ace_mystatetext
The lookup field ace_mystate links back to the field named ace_state on the entity named ace_s_state
I have a plugin that fires post-op on ace_s_main during an update.
The plugin is supposed to call a protected string method named GetValueFromLookup and pass it the name of the field to pull the value from.
Once GetValueFromLookup receives the passed information and runs, it is supposed to look at the current record that is undergoing the update, and pull the current value out from the ace_mystate lookup field, then return that value as a string.
My problem is I am encountering an error in the very first TRY section of the GetValueFromLookup method when the plugin attempts to access and pull the data from the lookup field.
I get a tracking message ‘Object not set to an instance of an object’
I am hoping someone can explain to me what I am doing incorrectly here and can show me what specific change I need to make to my code to fix it while keeping the general structure intact.
Here is the ExecutePostMainUpdate section.
protected void ExecutePostMainUpdate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
ITracingService TracingSvc = localContext.TracingService;
TracingSvc.Trace("Plugin has started.");
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
Entity ace_s_main = (Entity)context.InputParameters["Target"];
//var TextBoxValue = GetValueFromTextbox(service, localContext, ace_s_main, "ace_mystatetext");
var LookupBoxValue = GetValueFromLookup(service, localContext, ace_s_main, "ace_mystate");
}
catch (Exception ex) { throw new Exception("Error 100 in top level code. Initial TRY Failure. Code is not even entering business logic." + ex.Message); }
}
else
{
throw new InvalidPluginExecutionException("Error 200 in top level code. The code went to the ELSE section instead of executing IF portion. ");
}
}
Here is the GetValueFromLookup method where the error occurs. I placed a comment in this method so you can see exactly where it occurs. The problem occurs with the code that starts between line 10 and line 15 below.
protected string GetValueFromLookup(IOrganizationService EntityService, LocalPluginContext EntityLocalContext, Entity PassedEntity, string PassedField)
{
IPluginExecutionContext context = EntityLocalContext.PluginExecutionContext;
EntityService = EntityLocalContext.OrganizationService;
ITracingService TracingSvc = EntityLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
PassedEntity = (Entity)context.InputParameters["Target"];
///THIS IS WHERE THE ERROR IS OCCURRING --- THE PLUGIN IS BOMBING OUT HERE VIA CATCH ERROR 500 SHOWN BELOW IN THE CUSTOM CATCH AREA
try
{
var FieldValue = PassedEntity.GetAttributeValue<EntityReference>(PassedField);
var FieldValueAsString = FieldValue.Name.ToString();
if (String.IsNullOrEmpty(FieldValueAsString))
{
try
{
var result = "The field is null or empty";
throw new InvalidPluginExecutionException("The Value of The Lookup is: " + result);
//return result;
}
catch (Exception ex) { throw new Exception("Error 300 - Catch Error in " + ex.Message); }
}
else
{
try
{
var result2 = FieldValueAsString;
throw new InvalidPluginExecutionException("The Value of The Lookup is: " + result2);
//return result2;
}
catch (Exception ex) { throw new Exception("Error 400 - Error in ELSE/TRY GetValueFromLookup methpd: " + ex.Message); }
}
}
// THIS IS WHERE THE CODE IS GOING. IT IS NOT SUCCESSFULLY ACCESSING AND PULLING THE VALUE OUT FROM THE LOOKUP FIELD.
catch (Exception ex) { throw new Exception("Error 500 - Error in top level of GetValueFromLookup method: " + ex.Message); }
}
return null;
}
What is odd to me, is that I have a method named GetValueFromTextbox that operates almost the exact same as GetValueFromLookup, it just looks at a textbox field named ace_mystatetext that I used for testing and the GetValueFromTextbox method works fine, so I am thinking there is something in the GetValueFromLookup method that isn’t coded correctly or is missing that is causing this to occur, given that similar code works fine pulling values from a text box.
I have also included the code for GetValueFromTextbox below so you can see how that is almost identical to the GetValueFromLookup code.
protected string GetValueFromTextbox(IOrganizationService EntityService, LocalPluginContext EntityLocalContext, Entity PassedEntity, string PassedField)
{
IPluginExecutionContext context = EntityLocalContext.PluginExecutionContext;
EntityService = EntityLocalContext.OrganizationService;
ITracingService TracingSvc = EntityLocalContext.TracingService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
PassedEntity = (Entity)context.InputParameters["Target"];
try
{
string MyFieldValue = PassedEntity.GetAttributeValue<String>(PassedField);
if (String.IsNullOrEmpty(MyFieldValue))
{
try
{
var result = "The ID is null or empty";
throw new InvalidPluginExecutionException("The Value of The Textbox is: " + result);
// return result;
}
catch (Exception ex) { throw new Exception("Catch Error in " + ex.Message); }
}
else
{
try
{
var result2 = MyFieldValue;
throw new InvalidPluginExecutionException("The Value of The Textbox is: " + result2);
// return result2;
}
catch (Exception ex) { throw new Exception("Error in ELSE/TRY GetValueFromTextbox methpd: " + ex.Message); }
}
}
catch (Exception ex) { throw new Exception("Error in top level of GetValueFromTextbox method: " + ex.Message); }
}
return null;
}
If anyone can explain to me what I am doing incorrectly in my GetValueFromLookup method and can show me what specific change I need to make to my code to fix it while keeping the general structure intact, I would greatly appreciate it.