locked
Plug-in - Delete step RRS feed

  • Question

  • Hi all,

    I wrote a plug-in with two steps for Create and Delete of a custom entity. Here is the scenario. For the Opportunity entity we have a custom entity (opportunitymaterials). Any time a material is added or deleted, a flag on the Opportuntiy entity need to be set. Create part works fine. My issue is with the Delete part. Since TargetMessage type of Delete step is Moniker, I don't know how to get the OpportunityID I wanted. Moniker.id returns the materialID. Here is the code I am using.

    s the opportunityid is not available with Moniker, I am trying to query back the opportunitymaterial entity to get the opportunityid. on this line (BusinessEntityCollection oppty = (BusinessEntityCollection)response.BusinessEntityCollection;) I get BusinessEnityCollection count as 0. Hence, control doesn't enter the for loop.

    Am I doning something wrong?

     

     

    public class OpportunityMaterialsPlugin : IPlugin

    {

     

    public void Execute(IPluginExecutionContext context)

    {

     

    try

    {

     

    switch (context.MessageName)

    {

     

    case "Create":

     

    if (context.InputParameters.Contains(ParameterName.Target)

    && context.InputParameters[

    ParameterName.Target] is DynamicEntity

    && context.MessageName.Equals(

    MessageName.Create.ToString()))

    {

     

    DynamicEntity entityimage = (DynamicEntity)context.PostEntityImages["opportunitymaterialxml"];

     

    Lookup opportunitykey = (Lookup)entityimage["opportunityid"];

     

    Guid opportunityid = opportunitykey.Value;

     

    ICrmService service = context.CreateCrmService(true);

     

    DynamicEntity optyDynamicEntity = new DynamicEntity();

    optyDynamicEntity.Name =

    EntityName.opportunity.ToString();

     

    CrmBooleanProperty prop = new CrmBooleanProperty();

    prop.Name =

    "materialsadded";

    prop.Value =

    new CrmBoolean();

    prop.Value.Value =

    true;

    optyDynamicEntity.Properties.Add(prop);

    optyDynamicEntity[

    "opportunityid"] = new Key(opportunityid);

     

    TargetUpdateDynamic targetUpdate = new TargetUpdateDynamic();

    targetUpdate.Entity = optyDynamicEntity;

     

    UpdateRequest update = new UpdateRequest();

    update.Target = targetUpdate;

     

    UpdateResponse updresponse = (UpdateResponse)service.Execute(update);

    }

     

    break;

     

    case "Delete":

     

    if (context.InputParameters.Properties.Contains("Target"))

    {

     

    Moniker monikerentity = null;

    monikerentity = (

    Moniker)context.InputParameters.Properties[ParameterName.Target];

     

    Guid opportunitymaterialid = monikerentity.Id;

     

    ICrmService service = context.CreateCrmService(true);

    //Get Opportunityid for the given opportunitymaterialid

    QueryByAttribute optyLineItem = new QueryByAttribute();

    optyLineItem.ColumnSet =

    new AllColumns();

    optyLineItem.EntityName =

    "opportunitymaterial";

    optyLineItem.Attributes =

    new string[] { "opportunitymaterialid" };

    optyLineItem.Values =

    new object[] {new Guid(opportunitylineitemid.ToString())};

     

     

    RetrieveMultipleRequest request = new RetrieveMultipleRequest();

    request.Query = optyLineItem;

    request.ReturnDynamicEntities =

    true;

     

    RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);

     

    BusinessEntityCollection oppty = (BusinessEntityCollection)response.BusinessEntityCollection;

     

    foreach (DynamicEntity b_entity in oppty.BusinessEntities)

    {

     

    if (!b_entity.Properties.Contains("opportunityid"))

     

    continue;

     

    Guid opportunityid = new Guid();

    opportunityid = (

    Guid)((Guid)b_entity.Properties["opportunityid"]);

     

     

    QueryByAttribute optyMaterials = new QueryByAttribute();

    optyMaterials.ColumnSet =

    new AllColumns();

    optyMaterials.EntityName =

    "opportunitymaterial";

    optyMaterials.Attributes =

    new string[] { "opportunityid" };

    optyMaterials.Values =

    new object[] { opportunityid };

     

    BusinessEntityCollection opty = service.RetrieveMultiple(optyMaterials);

     

    if (opty.BusinessEntities.Count == 0)

    {

     

    DynamicEntity optyDynamicEntity = new DynamicEntity();

    optyDynamicEntity.Name =

    EntityName.opportunity.ToString();

     

    CrmBooleanProperty prop = new CrmBooleanProperty();

    prop.Name =

    "materialsadded";

    prop.Value =

    new CrmBoolean();

    prop.Value.Value =

    false;

    optyDynamicEntity.Properties.Add(prop);

    optyDynamicEntity[

    "opportunityid"] = new Key(opportunityid);

     

    TargetUpdateDynamic targetUpdate = new TargetUpdateDynamic();

    targetUpdate.Entity = optyDynamicEntity;

     

    UpdateRequest update = new UpdateRequest();

    update.Target = targetUpdate;

     

    UpdateResponse updresponse = (UpdateResponse)service.Execute(update);

    }

    }

    }

     

    break;

    }

    catch

     

    (Exception ex)

    {

     

    throw new InvalidPluginExecutionException(string.Format("An unexpected error occurred in the Opportunity materials plug-in: {0}", ex.Message), ex);

    }

    }

    }

    Wednesday, December 1, 2010 7:18 PM

Answers

All replies