Quantcast
Channel: Forum CRM Development
Viewing all articles
Browse latest Browse all 1000

Set Customers on Service Activity form with Sdk

$
0
0

Hi All,

Recently I did a plugin to populate some data from a new custom entity into service activity. I managed to populate most of the field except customer field. When debug, there is no errors occur. Just the field do not display anything......

Following is the code in the plugin.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Discovery;


namespace Service_Activity_Plugin
{
    public class ServiceActivity:IPlugin 
    {
        public string subject = "";
        public EntityReference services, customer;
        public static Entity customers;
        public void Execute(IServiceProvider serviceProvider)
        {
            Entity entity;                     

            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

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

            // Check if the input parameters property bag contains a target
            // of the create operation and that target is of type DynamicEntity.
            if (context.InputParameters.Contains("Target") &&
               context.InputParameters["Target"] is Entity)
            {
                // Obtain the target business entity from the input parmameters.
                entity = (Entity)context.InputParameters["Target"];

                // Verify that the entity represents an account.
                if (entity.LogicalName != "serviceappointment") { return; }
            }
            else
            {
                return;
            }

            try
            {                
                Guid equipment,activityid;

                if (context.Stage == 20)
                {
                    if (entity.Contains("new_equipment"))
                    {
                        equipment = ((EntityReference)entity["new_equipment"]).Id;
                        activityid = (Guid)entity["activityid"];

                        //PopulateServActivity(service, equipment,activityid);
                        
                        QueryExpression query = new QueryExpression("new_equipment");
                        ColumnSet cols = new ColumnSet(new string[] { "new_customer", "new_services", "new_name" });

                        ConditionExpression cond = new ConditionExpression("new_equipmentid", ConditionOperator.Equal, equipment.ToString());

                        FilterExpression filter = new FilterExpression(LogicalOperator.And);
                        filter.AddCondition(cond);

                        query.ColumnSet = cols;
                        query.Criteria = filter;

                        RetrieveMultipleResponse resp = RetrieveMultiple(query, service);

                        if (resp.EntityCollection.Entities.Count() > 0)
                        {
                            Entity myEnt = resp.EntityCollection.Entities[0];

                            subject = myEnt["new_name"].ToString();

                            services = (EntityReference)myEnt["new_services"];
                            customer = (EntityReference)myEnt["new_customer"];

                            customers = new Entity("activityparty");
                            customers["partyid"] = new EntityReference("account", new Guid(customer.Id.ToString()));                            

                        }
                        
                        entity.Attributes.Add("customers",new Entity[] {customers});
                        entity.Attributes.Add("serviceid", services);
                        entity.Attributes.Add("subject", subject);
                        entity.Attributes.Add("regardingobjectid", new EntityReference("new_equipment",equipment));
                        entity.Attributes.Add("regardingobjectidname", ((EntityReference)entity["new_equipment"]).Name);
                    }
                    else
                    {
                        throw new Exception("Equipment cannot blank.");
                    }
                }
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            }

            catch (Exception ex)
            {
                tracingService.Trace("ContactImportPlugin: {0}", ex.ToString());
                throw;
            }
        }
        private static RetrieveMultipleResponse RetrieveMultiple(QueryExpression query, IOrganizationService crmService)
        {
            RetrieveMultipleRequest rm = new RetrieveMultipleRequest();
            rm.Query = query;
            return crmService.Execute(rm) as RetrieveMultipleResponse;
        }

    }
}

Any help is much appreciated.

Thanks.

Regards,
Teh


Viewing all articles
Browse latest Browse all 1000

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>