I added a plugin on create of a new form it takes l list specified on the form and takes the equipment inside the list to create for each equipment a station equipment.
when I register the plugin on Create it doe not create any station equipment. when i remove the relation between station equipment and the project station (in the code) the records are created.
of course, I tried to use the profiler and debugged the problem, but when debugging the code runs normally without any issue (with relation too).
I tried to register the step on update to debug and got this error "An unexpected error occurred from ISV code." I tried checking the error and I think it means my code is entering an infinite loop. So, I tried to debug again and the code works fine when debugging.
of course, the step is registered on post-operation synchronous.
this is the code below
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity && context.PrimaryEntityId != null)
{
Entity entity = (Entity) context.InputParameters["Target"];
EntityReference projStationRef = new EntityReference("mfex2193_projectstation", context.PrimaryEntityId);
Entity prjStation = Functions.GetProjStationDet(service, context.PrimaryEntityId);
EntityReference station = prjStation.Contains("mfex2193_station") ? prjStation.GetAttributeValue<EntityReference>("mfex2193_station") : null;
EntityReference list = (prjStation.Contains("mfex2193_equipmentlist") ? prjStation.GetAttributeValue<EntityReference>("mfex2193_equipmentlist") : null) ?? throw new InvalidPluginExecutionException("Please choose a list.");
List<Entity> relatedEqu = Functions.GetRelatedEquipments(service, list.Id);
if (relatedEqu != null)
{
List<Entity> insertEnt = new List<Entity>();
foreach (Entity equipment in relatedEqu)
{
var equName = equipment.Contains("Equipment.mfex2193_name") ? equipment.GetAttributeValue<AliasedValue>("Equipment.mfex2193_name").Value.ToString() : "";
EntityReference equRef = new EntityReference("mfex2193_equipment", (Guid)equipment.GetAttributeValue<AliasedValue>("Equipment.mfex2193_equipmentid").Value);
//throw new InvalidPluginExecutionException(projStationRef.Id.ToString());
Entity newEnt = new Entity("mfex2193_stationequipment")
{
//Id = Guid.NewGuid(),
Attributes =
{
new KeyValuePair<string, object>("mfex2193_name",equName),
new KeyValuePair<string, object> ("mfex2193_station", station),
new KeyValuePair<string, object> ("mfex2193_equipment", equRef),
new KeyValuePair<string, object> ("mfex2193_projectstation", projStationRef)
}
};
insertEnt.Add(newEnt);
}
Functions.BulkInsert(service, insertEnt);
}
}