Wednesday, November 12, 2014

Create a followup activity for campaign response on the customer

It's a little tricky if you want to create a followup activity on the campaign response. As the campaign response is not linked to the customer directly it's impossible to do it via workflow.

There is a customer field in the campaign response which is in fact a row in the activityparty table with a reference to the customer. So this field is not available in the list of fields for workflow substitution. Here is a piece of code which can be run later or used in plugin to create a followup activity

//search for campaign responses created yesterday and create followup activity
                ConditionExpression cr_ce1 = new ConditionExpression("responsecode", ConditionOperator.Equal, 1); //Interested
                ConditionExpression cr_ce2 = new ConditionExpression("createdon", ConditionOperator.Yesterday); //Yesterday
                FilterExpression cr_fe = new FilterExpression(LogicalOperator.And); 
                QueryExpression cr_qe = new QueryExpression();
                cr_qe.EntityName = "campaignresponse";
                cr_qe.Criteria = cr_fe;
                cr_qe.ColumnSet = new ColumnSet("activityid", "subject", "customer","createdon");
                cr_qe.PageInfo.ReturnTotalRecordCount = true;
                EntityCollection cr_coll = _service.RetrieveMultiple(cr_qe);

                string fetchXML = @"<fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"" distinct=""false"">
                                      <entity name=""team"">
                                        <attribute name=""name"" />
                                        <attribute name=""teamid"" />
                                        <attribute name=""queueid"" />
                                        <order attribute=""name"" descending=""false"" />
                                        <filter type=""and"">
                                          <condition attribute=""name"" operator=""eq"" value=""Team Customerservice"" />

                Entity team = _service.RetrieveMultiple(new FetchExpression(fetchXML)).Entities[0];

                foreach (Entity ent in cr_coll.Entities)
                        if (ent.Attributes.Contains("customer"))
                            EntityCollection actParty = (EntityCollection)ent["customer"];
                            //create new task
                            Entity task = new Entity();
                            task.LogicalName = "task";
                            task["description"] = @"Some description";
                            task["scheduledstart"] = DateTime.Now;
                            task["scheduledend"] = (DateTime.Parse(ent["createdon"].ToString())).AddDays(7);
                            task["regardingobjectid"] = (EntityReference)(actParty[0]["partyid"]);
                            task["ownerid"] = new EntityReference(team.LogicalName, team.Id);
                            task["subject"] = ent["subject"];
                            Guid _taskId = _service.Create(task);
                            // add to customer service queue
                            Entity qItem = new Entity();
                            qItem.LogicalName = "queueitem";
                            qItem["objectid"] = new EntityReference(task.LogicalName, _taskId);
                            qItem["queueid"] = (EntityReference)team["queueid"];
                            Guid _itemId = _service.Create(qItem);
                    catch (Exception ex)
                        Helper.Log("Problem while processing campaign response Record with ID : " + ent["activityid"].ToString() + "\n" + ex.Message, w, true);

No comments: