locked
Exception handling in Plugins RRS feed

  • Question

  • Hi,

    I have developed a plugin with following code. Shouid I explicitly handle any of the exceptions or just build this and register as  a plugin?

    DynamicEntity

     

    entity = (DynamicEntity)context.InputParameters["Target"];

     

     

    //Check that target entity is Opportunity

     

    if (entity.Name != EntityName.opportunity.ToString())

     

    return;

     

    //Check that this Opportunity is created in the way of conversion from lead

     

    if (!entity.Properties.Contains("originatingleadid"))

     

    return;

     

    //Source Lead Identifier

     

    Guid leadid = ((Lookup)entity["originatingleadid"]).Value;

     

    //Target Opportunity Identifier

     

    Guid opportunityId = (Guid)context.OutputParameters["Id"];

     

    //Create the CRM service from context

     

    ICrmService crmservice = context.CreateCrmService(true);

     

    //Just build the query which will be used to retrieve all child annotations (notes and attachements)

     

    QueryByAttribute query = new QueryByAttribute();

    query.ColumnSet =

    new AllColumns();

    query.EntityName =

    EntityName.annotation.ToString();

    query.Attributes =

    new string[] { "objectid" };

    query.Values =

    new object[] { leadid };

     

    RetrieveMultipleRequest request = new RetrieveMultipleRequest();

    request.Query = query;

    request.ReturnDynamicEntities =

    false;

     

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

     

    foreach (annotation note in response.BusinessEntityCollection.BusinessEntities)

    {

     

    //Just remove Key field of annotation

    note.annotationid =

    null;

     

    //Replace referencing object lookup with newly created contact lookup

    note.objectid =

    new Lookup(EntityName.opportunity.ToString(), opportunityId);

    note.objecttypecode =

    new EntityNameReference(EntityName.opportunity.ToString());

     

    //And Create the annotation record

    crmservice.Create(note);

    }

    Thanks

    Monday, December 14, 2009 12:33 PM

Answers

All replies

  • If you want to provide an user friendly message, you should handle exceptions and throw an InvalidPluginExecutionException
    My blog : http://mscrmtools.blogspot.com You will find: Form Javascript Manager (export/import javascript from forms) ISV.Config Manager (graphical ISV.config edition - export/import) View Layout replicator (customize one view and replicate to others) And others (use tool tag on my blog)
    Monday, December 14, 2009 12:42 PM
    Moderator
  • Hi, Basically the above code copied the Notes from a Lead to an Opportunity on conversion. So, thers is no interaction with any of the users input so that I validate it and throw custom message. Just O want to know is that really required for any exception handling.

    Many Thanks
    Monday, December 14, 2009 12:48 PM
  • it is always nice practice to do exception handling with in your code so that if something goes it does not affect other areas of the system..doing excpetion handling is also helps to recognation and correcting the problems..

    Please read following articles releted to this..

    http://mscrm4ever.blogspot.com/2009/01/crm-40-creating-interactive-plug-ins.html
    http://msdn.microsoft.com/en-us/library/cc151084.aspx
    http://msdn.microsoft.com/en-us/library/bb959578.aspx

    Hope this helps...

    Monday, December 14, 2009 12:54 PM
    Answerer
  • Hi All,

    After reading the above articles, for my case I think theer is no explicit handling o exceptions required. Any exception thrown will be looged in Application event log. Many thanks for the support.
    Monday, December 14, 2009 1:07 PM
  • using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Text;

    using

     

    Microsoft.Crm.Sdk;

    using

     

    Microsoft.Crm.SdkTypeProxy;

    using

     

    System.Collections;

    using

     

    Microsoft.Crm.Sdk.Query;

    using

     

    System.IO;

    namespace

     

    CRM_KTN_DEV.Plugin.QuoteUpdateParentOpportunity

    {

     

    public class QuoteUpdateParentopportunity : IPlugin

    {

     

    /// <summary>

     

    /// this method will be called after a quote has been created or updated

     

    /// fields probability for Engineering and Specialty Chemicals will be uodated on parent opportunity

     

    /// fields revenue will be updated on parent opportunity

     

    /// Plugin Regeistration: Message(Update); Primary Entity(quote); Post Stage; Asynchronous; Parent Pipeline;

     

    /// </summary>

     

    public void Execute(IPluginExecutionContext context)

    {

     

    using (TextWriter log = TextWriter.Synchronized(File.AppendText(@"C:\Program Files\Microsoft Dynamics CRM\CrmLogs\QuoteUpdatesOpportunity_Plugin.txt")))

    {

     

    try

    {

     

    //Make sure that this is a quote

     

    if (context.PrimaryEntityName == EntityName.quote.ToString())

    {

     

    //Get the current quote ID

     

    DynamicEntity quote = (DynamicEntity)context.InputParameters[ParameterName.Target];

     

    Guid quoteID = ((Key)quote.Properties["quoteid"]).Value;

     

    ICrmService service = context.CreateCrmService(false);

     

    //Retrieve the source quote

     

    TargetRetrieveQuote quotesource = new TargetRetrieveQuote();

    quotesource.EntityId = quoteID;

     

    RetrieveRequest getQuote = new RetrieveRequest();

    getQuote.ReturnDynamicEntities =

    true;

    getQuote.Target = quotesource;

     

    ColumnSet quoteCols = new ColumnSet();

    quoteCols.AddColumn(

    "opportunityid");

    quoteCols.AddColumn(

    "ktn_chanceapproval");

    quoteCols.AddColumn(

    "ktn_chancewegetit");

    quoteCols.AddColumn(

    "ktn_probability");

    quoteCols.AddColumn(

    "ktn_revenue");

    quoteCols.AddColumn(

    "ktn_type");

    getQuote.ColumnSet = quoteCols;

     

    //get the sourcequote

     

    RetrieveResponse retrievedQuote = (RetrieveResponse)service.Execute(getQuote);

     

    DynamicEntity SourceQuote = (DynamicEntity)retrievedQuote.BusinessEntity;

     

     

    Guid opportunityId = ((Lookup)SourceQuote.Properties["opportunityid"]).Value;

     

     

    int ktn_Sc_Probability = ((Picklist)SourceQuote.Properties["ktn_probability"]).Value;

     

    decimal ktn_Revenue = ((CrmMoney)SourceQuote.Properties["ktn_revenue"]).Value;

     

    int quote_QuoteType = ((Picklist)SourceQuote.Properties["ktn_type"]).Value;

     

    int ktn_ChanceApproval = ((CrmNumber)SourceQuote.Properties["ktn_chanceapproval"]).Value;

     

    int ktn_Chance_We_Get_It = ((CrmNumber)SourceQuote.Properties["ktn_chancewegetit"]).Value;

     

    //Retrieve the Parentopportunity

     

    TargetRetrieveOpportunity oppTarget = new TargetRetrieveOpportunity();

    oppTarget.EntityId = opportunityId;

     

    RetrieveRequest getOpportunity = new RetrieveRequest();

    getOpportunity.ReturnDynamicEntities =

    true;

    getOpportunity.Target = oppTarget;

     

    ColumnSet opportunityCols = new ColumnSet();

    opportunityCols.AddColumn(

    "ktn_chanceapproval");

    opportunityCols.AddColumn(

    "ktn_chancewegetit");

    opportunityCols.AddColumn(

    "ktn_sc_probability");

    opportunityCols.AddColumn(

    "estimatedvalue");

    getOpportunity.ColumnSet = opportunityCols;

     

     

    //get the ParentOpportunity

     

    RetrieveResponse retrievedOpportunity = (RetrieveResponse)service.Execute(getOpportunity);

     

    DynamicEntity parentOpportunity = (DynamicEntity)retrievedOpportunity.BusinessEntity;

     

     

    // if (quote_QuoteType == 1) //base quote

     

    // { //set Fields values for opportunity

     

    if (parentOpportunity.Properties.Contains("ktn_chanceapproval"))

    {

    log.WriteLine(

    "A");

    ((

    CrmNumber)parentOpportunity.Properties["ktn_chanceapproval"]).Value = ktn_ChanceApproval;

    log.WriteLine(

    "B");

    }

     

    else

    {

     

    if (parentOpportunity.Properties.Contains("ktn_chanceapproval"))

    {

    log.WriteLine(

    "contains Field");

    }

     

    else

    {

    log.WriteLine(

    "does not contain field");

    }

    }

     

    if (parentOpportunity.Properties.Contains("ktn_chancewegetit"))

    {

    ((

    CrmNumber)parentOpportunity.Properties["ktn_chancewegetit"]).Value = ktn_Chance_We_Get_It;

    }

     

    if (parentOpportunity.Properties.Contains("ktn_sc_probability"))

    {

    ((

    Picklist)parentOpportunity.Properties["ktn_sc_probability"]).Value = ktn_Sc_Probability;

    }

     

    if (parentOpportunity.Properties.Contains("estimatedvalue"))

    {

    ((

    CrmMoney)parentOpportunity.Properties["estimatedvalue"]).Value = ktn_Revenue;

    }

     

    //}

     

    //Update the parent Opportunity with the new values

    service.Update(parentOpportunity);

    }

    }

     

    catch (Exception ex)

    {

     

    string logMessage = "Exception in the quote updates opportunity plugin: ";

    log.WriteLine(logMessage + ex.Message);

     

    throw new InvalidPluginExecutionException(logMessage, ex);

    }

    }

    }

    }

    }

    Friday, December 18, 2009 9:18 AM