locked
Custom Workflow Question CRM 2011 RRS feed

  • Question

  • Hi there

    I'm new to custom workflows in CRM 2011 and am getting myself confused.

    Please could you point me in the direction of why this is coming back with an "expected non-empty guid" when i know a record exists?

     public class LeadAllocation : CodeActivity
        {
            #region Input/Output Properties
            [Input("office input")]
            [Output("office output")]
            [ReferenceTarget("site")]
            public InOutArgument<EntityReference> Office { get; set; }

            [Output("roundrobin")]
            [ReferenceTarget("new_leadroundrobin")]
            public OutArgument<EntityReference> RoundRobin { get; set; }

            [Input("postcode")]
            public InArgument<String> Postcode { get; set; }
            #endregion

            protected override void Execute(CodeActivityContext executionContext)
            {

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

                var postcode = Postcode.Get<string>(executionContext);
                var area = postcode.Substring(0, 2);
                var au = area.ToUpper();
                var condition = new ConditionExpression("new_prefix", ConditionOperator.Equal, au);

                var filter = new FilterExpression {FilterOperator = LogicalOperator.And};
                filter.Conditions.Add(condition);

                var query = new QueryExpression
                {
                    EntityName = "new_regions",
                    ColumnSet = new ColumnSet(true),
                    Criteria = filter
                };

                var rmr = new RetrieveMultipleRequest {Query = query};

                var areas = (RetrieveMultipleResponse) service.Execute(rmr);

                if (areas.EntityCollection.Entities.Count <= 0)
                {
                    var office = 
                        Office.Get<EntityReference>(executionContext);
                    var officeId = office.Id;
                    var conditionRr = new ConditionExpression("new_leadroundrobinid", ConditionOperator.Equal, officeId);

                    // Create the FilterExpression.
                    var filterRr = new FilterExpression {FilterOperator = LogicalOperator.And};
                    filterRr.Conditions.Add(conditionRr);

                    // Create the QueryExpression object.
                    var queryRr = new QueryExpression
                    {
                        EntityName = "new_leadroundrobin",
                        ColumnSet = new ColumnSet(true),
                        Criteria = filterRr
                    };

                    var rr = new RetrieveMultipleRequest {Query = queryRr};
                    var round = (RetrieveMultipleResponse) service.Execute(rr);
                    if (round.EntityCollection.Entities.Count <= 0) return;
                    var dynRr = round.EntityCollection.Entities[0];
                    if (dynRr.Attributes.Contains("new_leadroundrobinid"))
                    {
                        RoundRobin.Set(executionContext, new EntityReference("new_leadroundrobin", ((Guid)dynRr.Attributes["new_leadroundrobinid"])));
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    var dyn = areas.EntityCollection.Entities[0];

                    if (!dyn.Attributes.Contains("new_siteid")) return;

                    Office.Set(executionContext, dyn.Attributes["new_siteid"]);

                    var office2 = Office.Get<EntityReference>(executionContext);
                    var officeId2 = office.Id;

                    var conditionRr = new ConditionExpression("new_leadroundrobinid", ConditionOperator.Equal,
                        office2);


                    var filterRr = new FilterExpression {FilterOperator = LogicalOperator.And};
                    filterRr.Conditions.Add(conditionRr);

                    var queryRr = new QueryExpression
                    {
                        EntityName = "new_leadroundrobin",
                        ColumnSet = new ColumnSet(true),
                        Criteria = filterRr
                    };

                    var rr = new RetrieveMultipleRequest {Query = queryRr};

                    var round = (RetrieveMultipleResponse) service.Execute(rr);

                    if (round.EntityCollection.Entities.Count <= 0) return;
                    var dynRr = round.EntityCollection.Entities[0];
                    if (dynRr.Attributes.Contains("new_leadroundrobinid"))
                    {
                        RoundRobin.Set(executionContext, new EntityReference("new_leadroundrobin", ((Guid)dynRr.Attributes["new_leadroundrobinid"])));
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }

                                                    
    • Edited by JimmyMac8673 Friday, August 29, 2014 11:59 AM change in code
    Thursday, August 28, 2014 10:11 AM