locked
CRM 2011 Plugin with Database RRS feed

  • Question

  • Hey GUys I use CRM 2011 OnPremises and I am doing a plugin to insert automatically an activity in my database, but I noticed that the activity has a UniquerID and that this ID is not AutoGenerated so how can I generate this ActivityID ?

     

    thanks

    Tuesday, August 30, 2011 12:36 PM

Answers

  • CRM use webservices to create entities. When you create records using webservices or xrm sdk it will create a unique id for you.

    Don't try to add any record in the database directly. It's not supported. More than that, when you create an activity it will create more than one record like phonecall or task then activitypointer and partylist etc.

    Here is sample example from sdk it is creating a followup task (task activity)

    // =====================================================================

    // This file is part of the Microsoft CRM SDK Code Samples.

    //

    // Copyright (C) Microsoft Corporation. All rights reserved.

    //

    // This source code is intended only as a supplement to Microsoft

    // Development Tools and/or on-line documentation. See these other

    // materials for detailed information regarding Microsoft code samples.

    //

    // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY

    // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE

    // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A

    // PARTICULAR PURPOSE.

    // =====================================================================

    //<snippetFollowupPlugin>

    using System;

    using System.ServiceModel;

    // Microsoft Dynamics CRM namespace(s)

    using Microsoft.Xrm.Sdk;

    namespace Microsoft.Crm.Sdk.Samples

    {

    public class FollowupPlugin: IPlugin

    {

    /// <summary>

    /// A plugin that creates a follow-up task activity when a new account is created.

    /// </summary>

    /// <remarks>Register this plug-in on the Create message, account entity,

    /// and asynchronous mode.

    /// </remarks>

    public void Execute(IServiceProvider serviceProvider)

    {

    //Extract the tracing service for use in debugging sandboxed plug-ins.

    ITracingService tracingService =

    (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    //<snippetFollowupPlugin1>

    // Obtain the execution context from the service provider.

    IPluginExecutionContext context = (IPluginExecutionContext)

    serviceProvider.GetService(typeof(IPluginExecutionContext));

    //</snippetFollowupPlugin1>

    //<snippetFollowupPlugin2>

    // The InputParameters collection contains all the data passed in the message request.

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

    context.InputParameters["Target"] is Entity)

    {

    // Obtain the target entity from the input parmameters.

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

    //</snippetFollowupPlugin2>

    // Verify that the target entity represents an account.

    // If not, this plug-in was not registered correctly.

    if (entity.LogicalName != "account")

    return;

    try

    {

    // Create a task activity to follow up with the account customer in 7 days.

    Entity followup = new Entity("task");

    followup["subject"] = "Send e-mail to the new customer.";

    followup["description"] =

    "Follow up with the customer. Check if there are any new issues that need resolution.";

    followup["scheduledstart"] = DateTime.Now.AddDays(7);

    followup["scheduledend"] = DateTime.Now.AddDays(7);

    followup["category"] = context.PrimaryEntityName;

    // Refer to the account in the task activity.

    if (context.OutputParameters.Contains("id"))

    {

    Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());

    string regardingobjectidType = "account";

    followup["regardingobjectid"] =

    new EntityReference(regardingobjectidType, regardingobjectid);

    }

    //<snippetFollowupPlugin4>

    // Obtain the organization service reference.

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    //</snippetFollowupPlugin4>

    // Create the task in Microsoft Dynamics CRM.

    tracingService.Trace("FollowupPlugin: Creating the task activity.");

    service.Create(followup);

    }

    //<snippetFollowupPlugin3>

    catch (FaultException<OrganizationServiceFault> ex)

    {

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

    }

    //</snippetFollowupPlugin3>

    catch (Exception ex)

    {

    tracingService.Trace("FollowupPlugin: {0}", ex.ToString());

    throw;

    }

    }

    }

    }

    }

    I hope this helps.

     


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    • Proposed as answer by Amreek Singh Tuesday, August 30, 2011 1:01 PM
    • Marked as answer by Hecto_reports Tuesday, August 30, 2011 2:35 PM
    Tuesday, August 30, 2011 1:01 PM