locked
Custom Workflow question 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?

    I am inputting a postcode that i know has a corresponding record for eg. M1 relates to Manchester. 

    Any advice would be greatly appreciated.

     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;
                    }
                }
            }
        }

    Friday, August 29, 2014 12:31 PM

All replies

  • you wrote

    var office =  Office.Get<EntityReference>(executionContext);
                    var officeId = office.Id;
                    var conditionRr = new ConditionExpression("new_leadroundrobinid", ConditionOperator.Equal, officeId);


    this condition is wrong. officeId will contain a Guid coming from a Site lookup, and after you search for new_leadroundrobin entities that contains the primaryid as your officeId.

    this will never return a result.

    In the second part you do the same, if you have new_siteid you put inside the context (why???), after you retrieve it from the context, and try to find again new_leadroundrobin primaryid as this site_id

    Check first your queryexpression conditions to get exactly what you need inside a console application, after you create the workflow activity.



    My blog: www.crmanswers.net - Rockstar 365 Profile

    Friday, August 29, 2014 1:15 PM