locked
How to read the Entity ID when the plugIn in Pre-Create stage RRS feed

  • Question

  • Hi

    how to Cancel the creation of an Entities record After the some validations has pass. Please give me any sample code.


    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur, - "If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"".
    Thursday, July 14, 2011 6:52 PM

Answers

  • Register your plugin for Pre-Create and throw an InvalidPluginExecutionException:

    throw new InvalidPluginExecutionException("Custom Validation failed");
    
    
    You can pass whatever message you like and the User will see it in an Error dialog.


    --pogo (pat) @ pogo69.wordpress.com
    • Proposed as answer by KumarXRM Thursday, March 29, 2012 12:35 PM
    • Marked as answer by Israel Gujjarlapudi Thursday, March 29, 2012 12:35 PM
    Thursday, July 14, 2011 7:21 PM
  • Without knowledge of your customisations or intended business logic, I can't comment on specifics, but by having a generic Exception handler to catch InvalidPluginExecutionException:

    catch (InvalidPluginExecutionException ex)
    {
      throw new InvalidPluginExecutionException("Unable to create a record, cancelled your operation.", ex);
    }
    

    and re-throwing it as the Inner Exception of your newly created InvalidPluginExecutionException, the original message will be lost to the End User - they will see only the new Message "Unable to create a record, cancelled your operation".  You are also allowing other Exceptions to pass on through.

    If that is your intention, no problem - but otherwise you'll need to expose the original Exception via either:

    catch (Exception ex)
    {
      if (ex is InvalidPluginExecutionException)
      {
        throw ex;
      }
    
      throw new InvalidPluginExecutionException("Unable to create a record, cancelled your operation.", ex);
    }
    or perhaps by composing the new Exception with the Message content of the original Exception.

     


    --pogo (pat) @ pogo69.wordpress.com
    Thursday, July 14, 2011 10:10 PM
  • Registered my PlugIn as Pre-Create,

    Do you suggest any modifications in my code. The PlugIn shouldn't create a record till conditions has pass.

     

    if (context.PrimaryEntityName == "gms_majorprojectallocation")
                    {

                        Guid majorprojectallocationid = new Guid();

                        try
                        {

                            if (context.OutputParameters.Properties.Contains("id"))
                            {
                                majorprojectallocationid = new Guid(context.OutputParameters.Properties["id"].ToString());
                            }
                            else
                            {
                                majorprojectallocationid = (Guid)((Microsoft.Crm.Sdk.Key)((Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"]).Properties[context.PrimaryEntityName + "id"]).Value;
                            }

                            //Fetching the Target gms_majorprojectamendment fields
                            QueryExpression queryMPFunding = new QueryExpression();
                            queryMPFunding.EntityName = "gms_majorprojectallocation";//gms_majorprojectamendment
                            queryMPFunding.ColumnSet = new AllColumns();
                            queryMPFunding.Criteria = new FilterExpression();
                            queryMPFunding.Criteria.AddCondition("gms_majorprojectallocationid", ConditionOperator.Equal, majorprojectallocationid.ToString()); //gms_majorprojectamendmentid

                            RetrieveMultipleRequest retrieveMPFunding = new RetrieveMultipleRequest();
                            retrieveMPFunding.Query = queryMPFunding;
                            retrieveMPFunding.ReturnDynamicEntities = true;
                            RetrieveMultipleResponse retrievedEntMPFunding = (RetrieveMultipleResponse)service.Execute(retrieveMPFunding);

                            if (retrievedEntMPFunding.BusinessEntityCollection.BusinessEntities.Count > 0)
                            {
                                DynamicEntity deMPFunding = (DynamicEntity)retrievedEntMPFunding.BusinessEntityCollection.BusinessEntities[0];

                                CrmMoney dcMPFCommittedAmount = new CrmMoney();
                                dcMPFCommittedAmount.Value = (decimal)0.00;
                                if (deMPFunding.Properties.Contains("gms_committedamount"))
                                {
                                    dcMPFCommittedAmount = (CrmMoney)deMPFunding.Properties["gms_committedamount"];
                                }

                                //Fetching the values of Major Project using Major Project Funding (gms_majorprojectallocationid)
                                #region fetching the Major Project Details
                                String keyMajorProjectID = "";
                                if (deMPFunding.Properties.Contains("gms_majorprojectid"))
                                {
                                    Lookup lkMajorProjectID = (Lookup)deMPFunding.Properties["gms_majorprojectid"];
                                    keyMajorProjectID = lkMajorProjectID.Value.ToString();
                                }
                                QueryByAttribute qeMajorProject = new QueryByAttribute();
                                qeMajorProject.EntityName = "gms_majorproject";
                                qeMajorProject.ColumnSet = new AllColumns();
                                qeMajorProject.Attributes = new String[] { "gms_majorprojectid" };
                                qeMajorProject.Values = new Object[] { keyMajorProjectID };

                                RetrieveMultipleRequest reqMajorProject = new RetrieveMultipleRequest();
                                reqMajorProject.ReturnDynamicEntities = true;
                                reqMajorProject.Query = qeMajorProject;
                                RetrieveMultipleResponse _retrievedMajorProject = (RetrieveMultipleResponse)service.Execute(reqMajorProject);

                                if (_retrievedMajorProject.BusinessEntityCollection.BusinessEntities.Count > 0)
                                {
                                    DynamicEntity deMajorProject = (DynamicEntity)_retrievedMajorProject.BusinessEntityCollection.BusinessEntities[0];

                                    CrmMoney dcMPOriginalAmount = new CrmMoney();
                                    dcMPOriginalAmount.Value = (decimal)0.00;
                                    if (deMajorProject.Properties.Contains("gms_originalamount"))
                                    {
                                        dcMPOriginalAmount = (CrmMoney)deMajorProject.Properties["gms_originalamount"];
                                    }



                                    #region fetching the MajorProjectFunding Details
                                    //Fetching all the records of Major Project Funding using Major Project ID
                                    QueryExpression qeMultiMPFunding = new QueryExpression();
                                    qeMultiMPFunding.EntityName = "gms_majorprojectallocation";//gms_majorprojectamendment
                                    qeMultiMPFunding.ColumnSet = new AllColumns();
                                    qeMultiMPFunding.Criteria = new FilterExpression();
                                    qeMultiMPFunding.Criteria.AddCondition("gms_majorprojectid", ConditionOperator.Equal, keyMajorProjectID.ToString()); //gms_majorprojectamendmentid

                                    RetrieveMultipleRequest retrieveMultiMPFunding = new RetrieveMultipleRequest();
                                    retrieveMultiMPFunding.Query = qeMultiMPFunding;
                                    retrieveMultiMPFunding.ReturnDynamicEntities = true;
                                    RetrieveMultipleResponse retrievedMultiEntMPFunding = (RetrieveMultipleResponse)service.Execute(retrieveMultiMPFunding);

                                    #endregion fetching the MajorProjectMajorProjectFunding Details


                                    //Validating the Committed Amounts of Major Project and Major Project Funding
                                    if (dcMPOriginalAmount.Value >= dcMPFCommittedAmount.Value)
                                    {
                                        if (retrievedMultiEntMPFunding.BusinessEntityCollection.BusinessEntities.Count > 0)
                                        {
                                            CrmMoney dcMPFTotalCommittedAmount = new CrmMoney();
                                            //dcMPFTotalCommittedAmount.Value = (decimal)0.00;


                                            for (int j = 0; j < retrievedMultiEntMPFunding.BusinessEntityCollection.BusinessEntities.Count; j++)
                                            {
                                                DynamicEntity deMultiMajorProjectFunding = new DynamicEntity();
                                                deMultiMajorProjectFunding = (DynamicEntity)retrievedMultiEntMPFunding.BusinessEntityCollection.BusinessEntities[j];

                                                CrmMoney dcMPFjCommittedAmount = new CrmMoney();
                                                dcMPFjCommittedAmount.Value = (decimal)0.00;

                                                if (deMultiMajorProjectFunding.Properties.Contains("gms_committedamount"))
                                                {
                                                    dcMPFjCommittedAmount = (CrmMoney)deMultiMajorProjectFunding.Properties["gms_committedamount"];
                                                    dcMPFTotalCommittedAmount.Value = dcMPFTotalCommittedAmount.Value + dcMPFjCommittedAmount.Value;
                                                }


                                            }

                                            #region fetching the AgnecyFundAllocation Details
                                            String keyAgencyFundingAllocationID = "";
                                            if (deMPFunding.Properties.Contains("gms_agencyfundallocationid"))
                                            {
                                                Lookup lkAgencyFundingAllocationID = (Lookup)deMPFunding.Properties["gms_agencyfundallocationid"];
                                                keyAgencyFundingAllocationID = lkAgencyFundingAllocationID.Value.ToString();
                                            }

                                            //Fetching the records of Agency Funding Allocation using Major Project Funding ID (gms_majorprojectallocationid)
                                            QueryExpression qeAgencyFundingAllocation = new QueryExpression();
                                            qeAgencyFundingAllocation.EntityName = "gms_agencyfundallocation";//gms_majorprojectamendment
                                            qeAgencyFundingAllocation.ColumnSet = new AllColumns();
                                            qeAgencyFundingAllocation.Criteria = new FilterExpression();
                                            qeAgencyFundingAllocation.Criteria.AddCondition("gms_agencyfundallocationid", ConditionOperator.Equal, keyAgencyFundingAllocationID.ToString()); //gms_majorprojectamendmentid

                                            RetrieveMultipleRequest retrieveAgencyFundingAllocation = new RetrieveMultipleRequest();
                                            retrieveAgencyFundingAllocation.Query = qeAgencyFundingAllocation;
                                            retrieveAgencyFundingAllocation.ReturnDynamicEntities = true;
                                            RetrieveMultipleResponse retrievedEntAgencyFundingAllocation = (RetrieveMultipleResponse)service.Execute(retrieveAgencyFundingAllocation);

                                            CrmMoney dcAFAUnCommittedAmount = new CrmMoney();
                                            dcAFAUnCommittedAmount.Value = (decimal)0.00;

                                            CrmMoney dcAFACommittedAmount = new CrmMoney();
                                            dcAFACommittedAmount.Value = (decimal)0.00;
                                            DynamicEntity deAgencyFundingAllocation = new DynamicEntity();
                                            if (retrievedEntAgencyFundingAllocation.BusinessEntityCollection.BusinessEntities.Count > 0)
                                            {
                                                deAgencyFundingAllocation = (DynamicEntity)retrievedEntAgencyFundingAllocation.BusinessEntityCollection.BusinessEntities[0];

                                                if (deAgencyFundingAllocation.Properties.Contains("gms_uncommittedamount"))
                                                {
                                                    dcAFAUnCommittedAmount = (CrmMoney)deAgencyFundingAllocation.Properties["gms_uncommittedamount"];
                                                }

                                                deAgencyFundingAllocation.Properties["gms_committedamount"] = (CrmMoney)dcMPFCommittedAmount;
                                            }

                                            if (dcMPFTotalCommittedAmount.Value <= dcMPOriginalAmount.Value)
                                            {

                                                if (dcMPFCommittedAmount.Value <= dcAFAUnCommittedAmount.Value)
                                                {
                                                    TargetUpdateDynamic updateAFA = new TargetUpdateDynamic();
                                                    updateAFA.Entity = deAgencyFundingAllocation;
                                                    UpdateRequest updRequestAFA = new UpdateRequest();
                                                    updRequestAFA.Target = updateAFA;
                                                    UpdateResponse updResponseAFA = (UpdateResponse)service.Execute(updRequestAFA);
                                                }
                                                else
                                                {
                                                    throw new InvalidPluginExecutionException("Unable to process your request, the Funding Amount of Major Project Funding can not exceed the Uncommitted Amount of Agency Funding Allocation.");
                                                    //throw new  OperationCanceledException("Unable to create a record, cancelled your operation.");
                                                    //throw new InvalidOperationException("Unable to create a record, cancelled your operation.");
                                                }

                                            }
                                            else
                                            {
                                                throw new InvalidPluginExecutionException("Unable to process your request, the Sum of all Funding Amounts of Major Project Funding in Major Project can not exceed the Awarded Amount of Major Project.");
                                                //throw new OperationCanceledException("Unable to create a record, cancelled your operation.");
                                                //throw new InvalidOperationException("Unable to create a record, cancelled your operation.");
                                            }

                                            #endregion fetching the AgnecyFundAllocation Details
                                        }
                                    }
                                    else
                                    {
                                        throw new InvalidPluginExecutionException("Unable to process your request, the Funding Amount of Major Project Funding can not exceed the Committed Amount of Major Project.");
                                        //throw new OperationCanceledException("Unable to create a record, cancelled your operation.");
                                        //throw new InvalidOperationException("Unable to create a record, cancelled your operation.");
                                    }

                                }

                                #endregion fetching the Major Project Details

                            }
                        }
                       
                        catch (InvalidPluginExecutionException ex)
                        {
                            throw new InvalidPluginExecutionException("Unable to create a record, cancelled your operation.", ex);
                        }


                    }


    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur, - "If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"".
    Thursday, July 14, 2011 9:38 PM
  • Pat,

    When I registered in Pre-Create Mode, the PlugIn has not able to read the ID value, but for Post Create event its able to reading the value, any wrong in the Code or in the registration.

    if (context.OutputParameters.Properties.Contains("id"))
                            {
                                majorprojectallocationid = new Guid(context.OutputParameters.Properties["id"].ToString());
                            }
                            else
                            {
                                majorprojectallocationid = (Guid)((Microsoft.Crm.Sdk.Key)((Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"]).Properties[context.PrimaryEntityName + "id"]).Value;
                            }


    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur, - "If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"".
    Wednesday, July 20, 2011 9:19 PM
  • Pat,

    my requirement is,

    The record has to create after the validations. For this do I need to change my code or need changes in my registration tool like Pre-Create and Post Image deployment. Please guide me,

    here my requirement is, my record has to create/update after certain validations has done. so It should be Pre-Create and Pre-Update events

    1.a. For Create, If I register my plugIn as pre-create then the PROBLEM is I cant get the ID because it doesn't have the Id. if so How can I get the ID? please I give me some code sample.

    1.b. If I register my PlugIn as Post-Create, Im getting the values but the PROBLEM is the record is creating even if the Validations has not pass.

     2.a For Update, If I register my PlugIn as Pre-Update then the PROBLEM is when the validations, the attributes showing the Old values. So automatically the values are wrong.

     Please give me some code how can I done this. Here Im pasting my code.

     

     

     


    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur, - "If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"".
    Thursday, July 21, 2011 1:46 AM
  • I have solved after thorough understanding of the PlugIn concept.

    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur. - I am glad I could help!!! - If this post answers your question, please click “Mark as Answer” on the post and “Mark as Helpful”. For More Information, please feel free to visit our website- http://xrmxtensibles.wordpress.com/

    Thursday, March 29, 2012 12:38 PM

All replies

  • Register your plugin for Pre-Create and throw an InvalidPluginExecutionException:

    throw new InvalidPluginExecutionException("Custom Validation failed");
    
    
    You can pass whatever message you like and the User will see it in an Error dialog.


    --pogo (pat) @ pogo69.wordpress.com
    • Proposed as answer by KumarXRM Thursday, March 29, 2012 12:35 PM
    • Marked as answer by Israel Gujjarlapudi Thursday, March 29, 2012 12:35 PM
    Thursday, July 14, 2011 7:21 PM
  • Registered my PlugIn as Pre-Create,

    Do you suggest any modifications in my code. The PlugIn shouldn't create a record till conditions has pass.

     

    if (context.PrimaryEntityName == "gms_majorprojectallocation")
                    {

                        Guid majorprojectallocationid = new Guid();

                        try
                        {

                            if (context.OutputParameters.Properties.Contains("id"))
                            {
                                majorprojectallocationid = new Guid(context.OutputParameters.Properties["id"].ToString());
                            }
                            else
                            {
                                majorprojectallocationid = (Guid)((Microsoft.Crm.Sdk.Key)((Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"]).Properties[context.PrimaryEntityName + "id"]).Value;
                            }

                            //Fetching the Target gms_majorprojectamendment fields
                            QueryExpression queryMPFunding = new QueryExpression();
                            queryMPFunding.EntityName = "gms_majorprojectallocation";//gms_majorprojectamendment
                            queryMPFunding.ColumnSet = new AllColumns();
                            queryMPFunding.Criteria = new FilterExpression();
                            queryMPFunding.Criteria.AddCondition("gms_majorprojectallocationid", ConditionOperator.Equal, majorprojectallocationid.ToString()); //gms_majorprojectamendmentid

                            RetrieveMultipleRequest retrieveMPFunding = new RetrieveMultipleRequest();
                            retrieveMPFunding.Query = queryMPFunding;
                            retrieveMPFunding.ReturnDynamicEntities = true;
                            RetrieveMultipleResponse retrievedEntMPFunding = (RetrieveMultipleResponse)service.Execute(retrieveMPFunding);

                            if (retrievedEntMPFunding.BusinessEntityCollection.BusinessEntities.Count > 0)
                            {
                                DynamicEntity deMPFunding = (DynamicEntity)retrievedEntMPFunding.BusinessEntityCollection.BusinessEntities[0];

                                CrmMoney dcMPFCommittedAmount = new CrmMoney();
                                dcMPFCommittedAmount.Value = (decimal)0.00;
                                if (deMPFunding.Properties.Contains("gms_committedamount"))
                                {
                                    dcMPFCommittedAmount = (CrmMoney)deMPFunding.Properties["gms_committedamount"];
                                }

                                //Fetching the values of Major Project using Major Project Funding (gms_majorprojectallocationid)
                                #region fetching the Major Project Details
                                String keyMajorProjectID = "";
                                if (deMPFunding.Properties.Contains("gms_majorprojectid"))
                                {
                                    Lookup lkMajorProjectID = (Lookup)deMPFunding.Properties["gms_majorprojectid"];
                                    keyMajorProjectID = lkMajorProjectID.Value.ToString();
                                }
                                QueryByAttribute qeMajorProject = new QueryByAttribute();
                                qeMajorProject.EntityName = "gms_majorproject";
                                qeMajorProject.ColumnSet = new AllColumns();
                                qeMajorProject.Attributes = new String[] { "gms_majorprojectid" };
                                qeMajorProject.Values = new Object[] { keyMajorProjectID };

                                RetrieveMultipleRequest reqMajorProject = new RetrieveMultipleRequest();
                                reqMajorProject.ReturnDynamicEntities = true;
                                reqMajorProject.Query = qeMajorProject;
                                RetrieveMultipleResponse _retrievedMajorProject = (RetrieveMultipleResponse)service.Execute(reqMajorProject);

                                if (_retrievedMajorProject.BusinessEntityCollection.BusinessEntities.Count > 0)
                                {
                                    DynamicEntity deMajorProject = (DynamicEntity)_retrievedMajorProject.BusinessEntityCollection.BusinessEntities[0];

                                    CrmMoney dcMPOriginalAmount = new CrmMoney();
                                    dcMPOriginalAmount.Value = (decimal)0.00;
                                    if (deMajorProject.Properties.Contains("gms_originalamount"))
                                    {
                                        dcMPOriginalAmount = (CrmMoney)deMajorProject.Properties["gms_originalamount"];
                                    }



                                    #region fetching the MajorProjectFunding Details
                                    //Fetching all the records of Major Project Funding using Major Project ID
                                    QueryExpression qeMultiMPFunding = new QueryExpression();
                                    qeMultiMPFunding.EntityName = "gms_majorprojectallocation";//gms_majorprojectamendment
                                    qeMultiMPFunding.ColumnSet = new AllColumns();
                                    qeMultiMPFunding.Criteria = new FilterExpression();
                                    qeMultiMPFunding.Criteria.AddCondition("gms_majorprojectid", ConditionOperator.Equal, keyMajorProjectID.ToString()); //gms_majorprojectamendmentid

                                    RetrieveMultipleRequest retrieveMultiMPFunding = new RetrieveMultipleRequest();
                                    retrieveMultiMPFunding.Query = qeMultiMPFunding;
                                    retrieveMultiMPFunding.ReturnDynamicEntities = true;
                                    RetrieveMultipleResponse retrievedMultiEntMPFunding = (RetrieveMultipleResponse)service.Execute(retrieveMultiMPFunding);

                                    #endregion fetching the MajorProjectMajorProjectFunding Details


                                    //Validating the Committed Amounts of Major Project and Major Project Funding
                                    if (dcMPOriginalAmount.Value >= dcMPFCommittedAmount.Value)
                                    {
                                        if (retrievedMultiEntMPFunding.BusinessEntityCollection.BusinessEntities.Count > 0)
                                        {
                                            CrmMoney dcMPFTotalCommittedAmount = new CrmMoney();
                                            //dcMPFTotalCommittedAmount.Value = (decimal)0.00;


                                            for (int j = 0; j < retrievedMultiEntMPFunding.BusinessEntityCollection.BusinessEntities.Count; j++)
                                            {
                                                DynamicEntity deMultiMajorProjectFunding = new DynamicEntity();
                                                deMultiMajorProjectFunding = (DynamicEntity)retrievedMultiEntMPFunding.BusinessEntityCollection.BusinessEntities[j];

                                                CrmMoney dcMPFjCommittedAmount = new CrmMoney();
                                                dcMPFjCommittedAmount.Value = (decimal)0.00;

                                                if (deMultiMajorProjectFunding.Properties.Contains("gms_committedamount"))
                                                {
                                                    dcMPFjCommittedAmount = (CrmMoney)deMultiMajorProjectFunding.Properties["gms_committedamount"];
                                                    dcMPFTotalCommittedAmount.Value = dcMPFTotalCommittedAmount.Value + dcMPFjCommittedAmount.Value;
                                                }


                                            }

                                            #region fetching the AgnecyFundAllocation Details
                                            String keyAgencyFundingAllocationID = "";
                                            if (deMPFunding.Properties.Contains("gms_agencyfundallocationid"))
                                            {
                                                Lookup lkAgencyFundingAllocationID = (Lookup)deMPFunding.Properties["gms_agencyfundallocationid"];
                                                keyAgencyFundingAllocationID = lkAgencyFundingAllocationID.Value.ToString();
                                            }

                                            //Fetching the records of Agency Funding Allocation using Major Project Funding ID (gms_majorprojectallocationid)
                                            QueryExpression qeAgencyFundingAllocation = new QueryExpression();
                                            qeAgencyFundingAllocation.EntityName = "gms_agencyfundallocation";//gms_majorprojectamendment
                                            qeAgencyFundingAllocation.ColumnSet = new AllColumns();
                                            qeAgencyFundingAllocation.Criteria = new FilterExpression();
                                            qeAgencyFundingAllocation.Criteria.AddCondition("gms_agencyfundallocationid", ConditionOperator.Equal, keyAgencyFundingAllocationID.ToString()); //gms_majorprojectamendmentid

                                            RetrieveMultipleRequest retrieveAgencyFundingAllocation = new RetrieveMultipleRequest();
                                            retrieveAgencyFundingAllocation.Query = qeAgencyFundingAllocation;
                                            retrieveAgencyFundingAllocation.ReturnDynamicEntities = true;
                                            RetrieveMultipleResponse retrievedEntAgencyFundingAllocation = (RetrieveMultipleResponse)service.Execute(retrieveAgencyFundingAllocation);

                                            CrmMoney dcAFAUnCommittedAmount = new CrmMoney();
                                            dcAFAUnCommittedAmount.Value = (decimal)0.00;

                                            CrmMoney dcAFACommittedAmount = new CrmMoney();
                                            dcAFACommittedAmount.Value = (decimal)0.00;
                                            DynamicEntity deAgencyFundingAllocation = new DynamicEntity();
                                            if (retrievedEntAgencyFundingAllocation.BusinessEntityCollection.BusinessEntities.Count > 0)
                                            {
                                                deAgencyFundingAllocation = (DynamicEntity)retrievedEntAgencyFundingAllocation.BusinessEntityCollection.BusinessEntities[0];

                                                if (deAgencyFundingAllocation.Properties.Contains("gms_uncommittedamount"))
                                                {
                                                    dcAFAUnCommittedAmount = (CrmMoney)deAgencyFundingAllocation.Properties["gms_uncommittedamount"];
                                                }

                                                deAgencyFundingAllocation.Properties["gms_committedamount"] = (CrmMoney)dcMPFCommittedAmount;
                                            }

                                            if (dcMPFTotalCommittedAmount.Value <= dcMPOriginalAmount.Value)
                                            {

                                                if (dcMPFCommittedAmount.Value <= dcAFAUnCommittedAmount.Value)
                                                {
                                                    TargetUpdateDynamic updateAFA = new TargetUpdateDynamic();
                                                    updateAFA.Entity = deAgencyFundingAllocation;
                                                    UpdateRequest updRequestAFA = new UpdateRequest();
                                                    updRequestAFA.Target = updateAFA;
                                                    UpdateResponse updResponseAFA = (UpdateResponse)service.Execute(updRequestAFA);
                                                }
                                                else
                                                {
                                                    throw new InvalidPluginExecutionException("Unable to process your request, the Funding Amount of Major Project Funding can not exceed the Uncommitted Amount of Agency Funding Allocation.");
                                                    //throw new  OperationCanceledException("Unable to create a record, cancelled your operation.");
                                                    //throw new InvalidOperationException("Unable to create a record, cancelled your operation.");
                                                }

                                            }
                                            else
                                            {
                                                throw new InvalidPluginExecutionException("Unable to process your request, the Sum of all Funding Amounts of Major Project Funding in Major Project can not exceed the Awarded Amount of Major Project.");
                                                //throw new OperationCanceledException("Unable to create a record, cancelled your operation.");
                                                //throw new InvalidOperationException("Unable to create a record, cancelled your operation.");
                                            }

                                            #endregion fetching the AgnecyFundAllocation Details
                                        }
                                    }
                                    else
                                    {
                                        throw new InvalidPluginExecutionException("Unable to process your request, the Funding Amount of Major Project Funding can not exceed the Committed Amount of Major Project.");
                                        //throw new OperationCanceledException("Unable to create a record, cancelled your operation.");
                                        //throw new InvalidOperationException("Unable to create a record, cancelled your operation.");
                                    }

                                }

                                #endregion fetching the Major Project Details

                            }
                        }
                       
                        catch (InvalidPluginExecutionException ex)
                        {
                            throw new InvalidPluginExecutionException("Unable to create a record, cancelled your operation.", ex);
                        }


                    }


    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur, - "If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"".
    Thursday, July 14, 2011 9:38 PM
  • Without knowledge of your customisations or intended business logic, I can't comment on specifics, but by having a generic Exception handler to catch InvalidPluginExecutionException:

    catch (InvalidPluginExecutionException ex)
    {
      throw new InvalidPluginExecutionException("Unable to create a record, cancelled your operation.", ex);
    }
    

    and re-throwing it as the Inner Exception of your newly created InvalidPluginExecutionException, the original message will be lost to the End User - they will see only the new Message "Unable to create a record, cancelled your operation".  You are also allowing other Exceptions to pass on through.

    If that is your intention, no problem - but otherwise you'll need to expose the original Exception via either:

    catch (Exception ex)
    {
      if (ex is InvalidPluginExecutionException)
      {
        throw ex;
      }
    
      throw new InvalidPluginExecutionException("Unable to create a record, cancelled your operation.", ex);
    }
    or perhaps by composing the new Exception with the Message content of the original Exception.

     


    --pogo (pat) @ pogo69.wordpress.com
    Thursday, July 14, 2011 10:10 PM
  • Hi Pradeep,

    you are not able to read ID because it's not generated yet in precreate, you will be able to access it only in post create.


    Mahain : My Dynamics CRM Blog
    Friday, July 15, 2011 5:30 AM
    Moderator
  • if so, how can I can get the ID for the pre-create event.

    should I register it as new "post Image" step for Pre-Create Event?

    Please assist me


    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur, - "If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"".

    Tuesday, July 19, 2011 6:40 PM
  • Pat,

    When I registered in Pre-Create Mode, the PlugIn has not able to read the ID value, but for Post Create event its able to reading the value, any wrong in the Code or in the registration.

    if (context.OutputParameters.Properties.Contains("id"))
                            {
                                majorprojectallocationid = new Guid(context.OutputParameters.Properties["id"].ToString());
                            }
                            else
                            {
                                majorprojectallocationid = (Guid)((Microsoft.Crm.Sdk.Key)((Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"]).Properties[context.PrimaryEntityName + "id"]).Value;
                            }


    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur, - "If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"".
    Wednesday, July 20, 2011 9:19 PM
  • If you need the newly created GUID identifier, you will have to register your plugin in the Post-Create stage, instead of Pre-Create.  In Pre-Create (prior to the entity instance's creation) the Id does not yet exist.
    --pogo (pat) @ pogo69.wordpress.com
    Thursday, July 21, 2011 12:24 AM
  • Pat,

    my requirement is,

    The record has to create after the validations. For this do I need to change my code or need changes in my registration tool like Pre-Create and Post Image deployment. Please guide me,

    here my requirement is, my record has to create/update after certain validations has done. so It should be Pre-Create and Pre-Update events

    1.a. For Create, If I register my plugIn as pre-create then the PROBLEM is I cant get the ID because it doesn't have the Id. if so How can I get the ID? please I give me some code sample.

    1.b. If I register my PlugIn as Post-Create, Im getting the values but the PROBLEM is the record is creating even if the Validations has not pass.

     2.a For Update, If I register my PlugIn as Pre-Update then the PROBLEM is when the validations, the attributes showing the Old values. So automatically the values are wrong.

     Please give me some code how can I done this. Here Im pasting my code.

     

     

     


    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur, - "If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"".
    Thursday, July 21, 2011 1:46 AM
  • I have solved after thorough understanding of the PlugIn concept.

    Thanks, - Israel Pradeep, - Software Engineer & PMP & B.I - Entrepreneur. - I am glad I could help!!! - If this post answers your question, please click “Mark as Answer” on the post and “Mark as Helpful”. For More Information, please feel free to visit our website- http://xrmxtensibles.wordpress.com/

    Thursday, March 29, 2012 12:38 PM