Need help creating plugin.

Unanswered Need help creating plugin.

  • Dienstag, 29. Juli 2008 04:35
     
     

     

    Hi Guys,

     

    Can anyone help in creating a small plugin that gets the "Company Name" from the Related Lead (Recipient field on phone call) and stick in another field on the phone call form. I can't figure out how to retrieve the Company name from the Lead lookup on the phone call form. Any help much appreciated!

Alle Antworten

  • Dienstag, 29. Juli 2008 05:11
     
     

    This is what i have guys, any help much aprreciated.

     

    Code Snippet

    using System;

    using Microsoft.Crm.Sdk;

    using Microsoft.Crm.SdkTypeProxy;

    namespace MyPlugins

    {

    public class PhoneCallUpdateHandler : IPlugin

    {

    public void Execute(IPluginExecutionContext context)

    {

    DynamicEntity entity = null;

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

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

    lead newlead = new lead();

    newlead.leadid = new Key();

    newlead.leadid.Value = leadlookup;

    entity.Properties.Add(new StringProperty("leadcompany", newlead.companyname));

    ICrmService service = (ICrmService)context.CreateCrmService(true);

    service.Update(newlead);

    }

    }

     

     

  • Dienstag, 29. Juli 2008 05:13
     
     

    Maybe This My Code will help you ?

     

     

     

    using System;

    using System.Collections.Generic;

    using System.Text;

    //using Microsoft.Crm.Sdk;

    //using Microsoft.Crm.Sdk.Query;

    //using Microsoft.Crm.SdkTypeProxy;

    using Microsoft.Crm.SdkTypeProxy.Metadata;

    using System.Reflection;                    // ref to crm

    using System.IO;

    using System.Xml;

     

    using SubScriberPlugin.CrmServiceRef;

    using System.Net;

    namespace SubScriberPlugin

    {

      public class Plugin : Microsoft.Crm.Sdk.IPlugin

        {

         public void Execute(Microsoft.Crm.Sdk.IPluginExecutionContext context)

            {

                Microsoft.Crm.Sdk.DynamicEntity entity = null;

     

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

                        context.InputParameters.Properties["Target"] is Microsoft.Crm.Sdk.DynamicEntity )

                {

         entity = (Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"];

                    //Check to make sure entity is a Vehicle entity

                    if (entity.Name != "cs1_bankaccount") { return; }

                }

                else { return; }

                try

                {

                   QueryExpression query = new QueryExpression();

                    query.EntityName = "cs1_contacttobankaccount";

                    query.ColumnSet = new AllColumns();

                    query.Criteria = new FilterExpression();

                    query.Criteria.FilterOperator = LogicalOperator.And;

                    ConditionExpression condition1 = new ConditionExpression();

                    condition1.AttributeName = "cs1_bankaccountid";

                    condition1.Operator = ConditionOperator.Equal;

                condition1.Values = new object[] { "A08CA26F-3953-DD11-94F3-0003FF04D7B5" };

                    query.Criteria.Conditions = new ConditionExpression[] { condition1 };

                    CrmAuthenticationToken token = new CrmAuthenticationToken();

                    token.AuthenticationType = 0;

                   token.OrganizationName = context.OrganizationName;                      

                    CrmService s = new CrmServiceRef.CrmService();

                    s.CrmAuthenticationTokenValue = token;

                    s.Credentials = CredentialCache.DefaultCredentials;

                    BusinessEntityCollection be = s.RetrieveMultiple(query);

                    /*ICrmService service = (ICrmService)context.CreateCrmService(true);

                    Microsoft.Crm.Sdk.BusinessEntityCollection be;

                    be = service.RetrieveMultiple(query);

                    */

                } catch (System.Web.Services.Protocols.SoapException ex)  {                throw new Microsoft.Crm.Sdk.InvalidPluginExecutionException("An error occurred in the AccountCreateHandler plug-in.", ex);           }        }

     

  • Dienstag, 29. Juli 2008 05:52
     
     

    Unfortunetly it does not help me too much but thanks for your code.

  • Mittwoch, 30. Juli 2008 04:49
     
     

    Can anyone else help me? thanks.

  • Mittwoch, 30. Juli 2008 06:04
     
     

    try this

     

    but before you  need define images and give him alias . my alias is "test" in this Example

     

    you define this at the PLUGIN TOOL in title Images

     

    DynamicEntity entityPost = (DynamicEntity)context.PostEntityImages.Properties["test"];

     

    DynamicEntity entityPre = (DynamicEntity)context.PreEntityImages.Properties["test"];

     

    string cs1_accountnumAfterChange = entityPost.Properties["cs1_accountnum"].ToString();

    string cs1_accountnumBeforeChange = entityPre.Properties["cs1_accountnum"].ToString();

     

  • Mittwoch, 30. Juli 2008 11:38
     
     

    I Managed to put something together, though the plugin goes and gets the data it seems to fail on service.update(entity). plugin is running on pre. Any ideas much appreciated!

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.Text;

    using Microsoft.Crm.Sdk;

    using Microsoft.Crm.SdkTypeProxy;

     

    namespace Microsoft.Crm.Sdk.Walkthrough

    {

    // this is the phone call update plugin.

    public class PhoneCallUpdateHandler : IPlugin

    {

    public void Execute(IPluginExecutionContext context)

    {

    // define a new Dynamic Entity as null

    DynamicEntity entity = null;

    // Check if the input parameters property bag contains a target

    // of the update operation and that target is of type DynamicEntity.

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

    context.InputParameters.Properties["Target"] is DynamicEntity)

    {

    // Obtain the target business entity from the input parmameters.

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

    // Verify that the entity represents a phonecall.

    if (entity.Name != EntityName.phonecall.ToString()) { return; }

    }

    else

    {

    return;

    }

    try

    {

    //throw new InvalidPluginExecutionException("exception at start");

    //we check if the category is empty then continue, else we stop.

    if (entity.Properties.Contains("category") == false)

    {

    // Start the Service request

    ICrmService service = context.CreateCrmService(true);

    // ICrmService service = (ICrmService)context.CreateCrmService(true);

    //Get the GuId of the lead.

    Guid leadlookup = ((Lookup)entity.Properties["regardingobjectid"]).Value;

    // define stringpost as a string and input the leadlookup (GUID) into stringpost.

    string stringpost;

    stringpost = leadlookup.ToString();

    // set leadguid as a GUID

    Guid leadguid = leadlookup;

     

     

    // retrieves the company name

    lead leadinformation = (lead)service.Retrieve(EntityName.lead.ToString(), leadguid, new Query.AllColumns());

    // add the newvalue (company name) into the category field

    entity.Properties["category"] = leadinformation.companyname;

    entity.Properties["subject"] = leadinformation.subject;

     

    //Update the phone call

    //ICrmService service = context.CreateCrmService(true);

    service.Update(entity);

    }

    else { return; }

    }

    // this is the exception thrown if there is an error

    catch (System.Web.Services.Protocols.SoapException ex)

    {

    throw new InvalidPluginExecutionException("An error occurred in the AccountCreateHandler plug-in.", ex);

    }

    }

    }

    }

     

     

  • Mittwoch, 30. Juli 2008 12:14
     
     

    this is what i got from the Diagnostic:

     

    Code Snippet

    >MSCRM Error Report:
    --------------------------------------------------------------------------------------------------------
    Error: An error occurred in the AccountCreateHandler plug-in.

    Error Number: 0x80040216

    Error Message: An unexpected error occurred.

    Source File: Not available

    Line Number: Not available

    Request URL: http://192.168.1.15:5555/test/activities/phone/edit.aspx

    Stack Trace Info: [SoapException: Server was unable to process request.]
       at Microsoft.Crm.Extensibility.SdkTypeProxyCrmServiceWrapper.InternalInvoke(MethodInfo methodInfo, Object[] parameters)

     

     

     

  • Mittwoch, 30. Juli 2008 13:42
     
     

    I am just wondering how can you get the newlead.companyname?  The companyname is null because you only have the leadid from the phone call entity.  To get the companyname, you have to retrieve the value first from the web service.  So, before the line entity.Properties.Add(new StringProperty("leadcompany", newlead.companyname));

     

    You have to add this:

     

    Code Snippet

    ICrmService service = context.CreateCrmService(true);

    lead retrieveLead = service.Retrieve(EntityName.lead.ToString(), leadlookup, new ColumnSet(new String[] {"leadid", "companyname" })) as lead;

     

     

    and then add the properties like this:

     

    Code Snippet
    entity.Properties.Add(new StringProperty("leadcompany", retrieveLead.companyname));

     

     

    the retrieveLead can also be done in dynamic entity.

  • Mittwoch, 30. Juli 2008 14:05
     
     
    Jude, thanks for your suggestion. though i have already been able to retrieve lead information. i am just on stuck on the CRM system crashing on the service.Update(entitiy); line. i don't know why. Any other ideas much appreciated.

     

  • Mittwoch, 30. Juli 2008 14:10
     
     

    What is the detail of the SoapException?  Look into the SoapException.Detail.InnerText.

     

     

  • Mittwoch, 30. Juli 2008 14:25
     
     

    Here are some errors i found from the Microsoft CRM diagnostic Tool.

     

    Code Snippet
    Inner Exception: System.Web.Services.Protocols.SoapException: Server was unable to process request.
    Detail: <detail><error>
      <code>0x80040216</code>
      <description>An unexpected error occurred.</description>
      <type>Platform</type>
    </error></detail>
       at Microsoft.Crm.Extensibility.SdkTypeProxyCrmServiceWrapper.InternalInvoke(MethodInfo methodInfo, Object[] parameters)
       at Microsoft.Crm.Extensibility.SdkTypeProxyCrmServiceWrapper.Update(BusinessEntity entity)
       at Microsoft.Crm.Sdk.Walkthrough.PhoneCallUpdateHandler.Execute(IPluginExecutionContext context) in C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\AccountCreatePlugin\AccountCreatePlugin\Class1.cs:line 78

     

     

    Code Snippet

    >MSCRM Error Report:
    --------------------------------------------------------------------------------------------------------
    Error: An error occurred in the AccountCreateHandler plug-in.

    Error Number: 0x80040216

    Error Message: An unexpected error occurred.

    Source File: Not available

    Line Number: Not available

    Request URL: http://192.168.1.15:5555/test/activities/phone/edit.aspx

    Stack Trace Info: [SoapException: Server was unable to process request.]
       at Microsoft.Crm.Extensibility.SdkTypeProxyCrmServiceWrapper.InternalInvoke(MethodInfo methodInfo, Object[] parameters)

     

     

    Code Snippet
    CrmSoapExtension detected non-CrmException - report will be sent to Watson:
    System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.

     

     

     

  • Donnerstag, 31. Juli 2008 02:55
     
     
    please, can anyone else help?

     

  • Donnerstag, 31. Juli 2008 08:38
    Moderator
     
     

     

    see the plugin registration tool..

     

    Regards,

    Imran

     

  • Donnerstag, 31. Juli 2008 13:13
     
     

    I see that you have a NullReferenceException which leads to Object reference not set to an instance of an object.  This happens becasue you did not check if the Property existed. 

     

    E.g. when you write, ((Lookup)entity.Properties["regardingobjectid"]).Value, you are assuming that regardingobjectid existed.  What happen if this activity is standalone?  You need to do this:

     

    Code Snippet

    if(entity.Properties.Contains("regardingobjectid"))

    {

    ((Lookup)entity.Properties["regardingobjectid"]).Value = new Guid();

    }