Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

Set Lookup value into Null

$
0
0

Hello,

I have two entities, called "Project" and "Program". 

In those both entities, also have same field, which is "End Customer" with type lookup to Account.

the question is, how to set lookup value "End Customer" into null in "Program" entity, if "Project" have null value?

this is my code :

// <copyright file="ChangeEndCustomer.cs" company="Microsoft">
// Copyright (c) 2017 All Rights Reserved
// </copyright>
// <author>Microsoft</author>
// <date>11/15/2017 4:34:10 PM</date>
// <summary>Implements the ChangeEndCustomer Workflow Activity.</summary>
namespace MFS_SOLUTION.MFS_WORKFLOW
{
    using System;
    using System.Activities;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using System.Linq;

    public sealed class ChangeEndCustomer : CodeActivity
    {
        /// <summary>
        /// Executes the workflow activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        protected override void Execute(CodeActivityContext executionContext)
        {
            // Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();

            if (tracingService == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
            }

            tracingService.Trace("Entered ChangeEndCustomer.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
                executionContext.ActivityInstanceId,
                executionContext.WorkflowInstanceId);

            // Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

            if (context == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
            }

            tracingService.Trace("ChangeEndCustomer.Execute(), Correlation Id: {0}, Initiating User: {1}",
                context.CorrelationId,
                context.InitiatingUserId);

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            try
            {
                // TODO: Implement your custom Workflow business logic.
                Entity entity = null;

                if (context.InputParameters != null && context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    entity = (Entity)context.InputParameters["Target"];
                }
                else
                {
                    entity = service.Retrieve(context.PrimaryEntityName, ((Guid)context.PrimaryEntityId), new ColumnSet(true));
                }

                Guid ProjectID = new Guid();
                Guid EndCustomerID = new Guid();
                Guid ProgramID = new Guid();
                Guid NullCustomerID = new Guid();

                Entity RetrieveProject = service.Retrieve("ibiz_project", entity.Id, new ColumnSet(true));

                ProjectID = RetrieveProject.GetAttributeValue<Guid>("ibiz_projectid");
                EndCustomerID = RetrieveProject.GetAttributeValue<EntityReference>("ibiz_endcustomer").Id;

                ConditionExpression CheckProject = new ConditionExpression();
                CheckProject.AttributeName = "ibiz_project";
                CheckProject.Operator = ConditionOperator.Equal;
                CheckProject.Values.Add(ProjectID);

                FilterExpression FilterProject = new FilterExpression();
                FilterProject.Conditions.Add(CheckProject);

                QueryExpression queryProject = new QueryExpression("ibiz_program");
                queryProject.ColumnSet = new ColumnSet(true);
                queryProject.Criteria.AddFilter(FilterProject);

                EntityCollection resultProject = service.RetrieveMultiple(queryProject);

                if (resultProject.Entities.Any())
                {
                    foreach (Entity eProject in resultProject.Entities)
                    {
                        ProgramID = eProject.GetAttributeValue<Guid>("ibiz_programid");

                        if (EndCustomerID != null)
                        {
                            Entity ProgramToUpdate = new Entity("ibiz_program");
                            ProgramToUpdate["ibiz_programid"] = ProgramID;
                            ProgramToUpdate["ibiz_endcustomer"] = new EntityReference("account", EndCustomerID);
                            service.Update(ProgramToUpdate);
                        }
                        else
                        {
                            Entity ProgramToUpdate = new Entity("ibiz_program");
                            ProgramToUpdate["ibiz_programid"] = ProgramID;
                            ProgramToUpdate.Attributes.Add("ibiz_endcustomer", null);
                            service.Update(ProgramToUpdate);
                        }
                    }
                }
            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                tracingService.Trace("Exception: {0}", e.ToString());

                // Handle the exception.
                throw;
            }

            tracingService.Trace("Exiting ChangeEndCustomer.Execute(), Correlation Id: {0}", context.CorrelationId);
        }
    }
}


the error saying nullreferenceexception, when I tried to remove value in "Project" entity.


Viewing all articles
Browse latest Browse all 79901

Trending Articles



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