user2314263
user2314263

Reputation: 21

Plugin code to update another entity when case is created mscrm 2011

Im new with plugin. my problem is, When the case is created, i need to update the case id into ledger. What connect this two is the leadid. in my case i rename lead as outbound call.

this is my code. I dont know whether it is correct or not. Hope you guys can help me with this because it gives me error. I manage to register it. no problem to build and register but when the case is created, it gives me error.

using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using System.Net;
using System.Web.Services;


/*
 * Purpose: 1) To update case number into lejar
 *         
 * Triggered upon CREATE message by record in Case form.
 */
namespace UpdateLejar
{
    public class UpdateLejar : IPlugin
    {
        /*public void printLogFile(String exMessage, String eventMessage, String pluginFile)
        {
            DateTime date = DateTime.Today;
            String fileName = date.ToString("yyyyMdd");
            String timestamp = DateTime.Now.ToString();

            string path = @"C:\CRM Integration\PLUGIN\UpdateLejar\Log\" + fileName;
            //open if file exist, check file..
            if (File.Exists(path))
            {
                //if exist, append
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.Write(timestamp + " ");
                    sw.WriteLine(pluginFile + eventMessage + " event:  " + exMessage);
                    sw.WriteLine();
                }

            }

            else
            {
                //if no exist, create new file
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.Write(timestamp + " ");
                    sw.WriteLine(pluginFile + eventMessage + " event:  " + exMessage);
                    sw.WriteLine();
                }
            }
        }*/

        public void Execute(IServiceProvider serviceProvider)
        {

            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            //for update and create event
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity targetEntity = (Entity)context.InputParameters["Target"];

                // Verify that the entity represents a connection.
                if (targetEntity.LogicalName != "incident")
                {
                    return;
                }

                else
                {
                    try
                    {

                        //triggered upon create message
                        if (context.MessageName == "Create")
                        {
                            Guid recordid = new Guid(context.OutputParameters["incidentid"].ToString());

                            EntityReference app_inc_id = new EntityReference();
                            app_inc_id = targetEntity.GetAttributeValue<EntityReference>("new_outboundcalllid");
                            Entity member = service.Retrieve("new_lejer", ((EntityReference)targetEntity["new_outboundcallid"]).Id, new ColumnSet(true));
                            //DateTime createdon = targetEntity.GetAttributeValue<DateTime>("createdon");

                            if (app_inc_id != null)
                            {
                                if (targetEntity.Attributes.Contains("new_outboundcallid") == member.Attributes.Contains("new_outboundcalllistid_lejer"))
                                {



                                    member["new_ringkasanlejarid"] = targetEntity.Attributes["incidentid"].ToString();
                                    service.Update(member);
                                }
                            }

                        }

                        tracingService.Trace("Lejar updated.");
                    }


                    catch (FaultException<OrganizationServiceFault> ex)
                    {
                        //printLogFile(ex.Message, context.MessageName, "UpdateLejar plug-in. ");
                        throw new InvalidPluginExecutionException("An error occurred in UpdateLejar plug-in.", ex);
                    }

                    catch (Exception ex)
                    {
                        //printLogFile(ex.Message, context.MessageName, "UpdateLejar plug-in. ");
                        tracingService.Trace("UpdateLejar: {0}", ex.ToString());
                        throw;
                    }
                }
            }
        }
    }
}

Upvotes: 2

Views: 4037

Answers (2)

Raju
Raju

Reputation: 86

Please check,

is that entity containing the attributes or not.

check it and try:

if (targetEntity.Contains("new_outboundcallid"))
((EntityReference)targetEntity["new_outboundcallid"]).Id

Upvotes: 3

tdgtyugdyugdrugdr
tdgtyugdyugdrugdr

Reputation: 826

member["new_ringkasanlejarid"] = targetEntity.Attributes["incidentid"].ToString();

What is new_ringkasanlejarid's type? You're setting a string to it. If new_ringkasanlejarid is an entity reference, this might be causing problems.

You might want to share the error details or trace log, all we can do is assume what the problem is at the moment.

Upvotes: 2

Related Questions