locked
Codes for Lookup Field Custom Workflow RRS feed

  • Question

  • I have this requirement:

    "Contact records must have an Account Number Lookup field which is populated from the Account Number field in the Account Entity."

    I am having problem writing the code that would fetch the Account Number entered in Account Entity and bringing them to the Lookup field created in Contact Entity. I would appreciate it if anyone who has been writing similar codes could help me out. My codes are pasted below:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using System.Threading.Tasks;
    using System.Activities;
    using Microsoft.Xrm.Sdk.Workflow;
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Metadata;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;

    namespace LookupFieldWorkflow20170613
    {
        public abstract class WorkFlowActivityBase : CodeActivity
        {
            public sealed class LocalWorkflowContext
            {
                internal IServiceProvider ServiceProvider
                {
                    get;
                    private set;
                }
                /*
                internal IOrganizationService OrganizationService
                {
                    get;
                    private set;
                }
                */
                internal IWorkflowContext WorkflowExecutionContext
                {
                    get;
                    private set;
                }
                internal ITracingService TracingService
                {
                    get;
                    private set;
                }
                private LocalWorkflowContext()
                {
                }
                internal LocalWorkflowContext(CodeActivityContext executionContext)
                {
                    if (executionContext == null)
                    {
                        throw new ArgumentNullException("serviceProvider");
                    }
                    // Obtain the execution context service from the service provider.
                    this.WorkflowExecutionContext = (IWorkflowContext)executionContext.GetExtension<IWorkflowContext>();
                    // Obtain the tracing service from the service provider.
                    this.TracingService = (ITracingService)executionContext.GetExtension<ITracingService>();
                    // Obtain the Organization Service factory service from the service provider
                    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)executionContext.GetExtension<IOrganizationServiceFactory>();
                    // Use the factory to generate the Organization Service.
                    // this.OrganizationService = factory.CreateOrganizationService(this.WorkflowExecutionContext.UserId);
                    IOrganizationService _service = factory.CreateOrganizationService(WorkflowExecutionContext.UserId);
                    if (WorkflowExecutionContext.InputParameters.Contains("Target") && WorkflowExecutionContext.InputParameters["Target"] is EntityReference)
                    {
                        if (((EntityReference)WorkflowExecutionContext.InputParameters["Target"]).LogicalName != "contact")
                            return;
                        Entity Contact = _service.Retrieve("contact", ((EntityReference)WorkflowExecutionContext.InputParameters["Target"]).Id, new ColumnSet("esloe_clientaccountnumber"));
                        //   Entity Account = _service.RetrieveMultiple("account"), ((EntityReference)WorkflowExecutionContext.InputParameters["Target"].Id, new Columset("accountid", "accountnumber");
                        //Check to ensuer that the Account Number field in Account Entity is not empty.
                    }
                }
                public Boolean accountNumberNotEmpty(IOrganizationService _serviceProxy, IWorkflowContext context)
                {
                    QueryExpression query = new QueryExpression
                    {
                        EntityName = "account",
                        ColumnSet = new ColumnSet("accountid"),
                        Criteria = new FilterExpression
                        {
                            Conditions = { new ConditionExpression { AttributeName = "accountnumber", Operator = ConditionOperator.GreaterThan, Values = { " 0" } } }
                        }
                    };
                        //Map the value in the Account Number field to the Client Account Number Lookup field in Contact
                      
                        EntityCollection accountNumberRecord = _serviceProxy.RetrieveMultiple(query);
                    if (accountNumberRecord.Entities.Count > 0)
                    {
                        (Entity)
                    }
                }
                internal void Trace(string message)
                {
                    if (string.IsNullOrWhiteSpace(message) || this.TracingService == null)
                    {
                        return;
                    }
                    if (this.WorkflowExecutionContext == null)
                    {
                        this.TracingService.Trace(message);
                    }
                    else
                    {
                        this.TracingService.Trace(
                            "{0}, Correlation Id: {1}, Initiating User: {2}",
                            message,
                            this.WorkflowExecutionContext.CorrelationId,
                            this.WorkflowExecutionContext.InitiatingUserId);
                    }
                }
            }
           
            protected override void Execute(CodeActivityContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("serviceProvider");
                }
              
                
                try
                {
                 
                    ExecuteCRMWorkFlowActivity(context, localcontext);
                }
                catch (FaultException<OrganizationServiceFault> e)
                {
                    localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", e.ToString()));
                    // Handle the exception.
                    throw;
                }
                finally
                {
                   }
            }
            public virtual void ExecuteCRMWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext crmWorkflowContext)
            {
               
            }


    LOE

    Thursday, June 15, 2017 1:14 PM

Answers

  • Problem was solved when I placed support request on Microsoft. An Engineer, Ankit (this Engineer has been very wonderful when it comes to solving my code problems), was able to solve the problem.

    Thank you.

    Longinus.


    LOE

    Friday, July 14, 2017 5:08 PM