2

我有一个插件,可以将潜在客户的活动转移到合格的机会。我已经在机会的“创建”上注册了插件,以下是代码

public void Execute(IServiceProvider serviceProvider)   
{
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        Entity entity;

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            entity = (Entity)context.InputParameters["Target"];

            if (entity.Attributes.Contains("originatingleadid") == false) return;
        }
        else
        {
            return;
        }

        try
        {
          //.....

            EntityReference Lead = (EntityReference)entity.Attributes["originatingleadid"];
            Guid LeadGuid = Lead.Id;
            MoveActivitiesFromLeadToOpportunity(service, LeadGuid, Opportunityid);
        }
        catch (FaultException<OrganizationServiceFault> ex)
        {
            throw new InvalidPluginExecutionException(
            "An error occurred in the plug-in.", ex);
        }
    }
 private void MoveActivitiesFromLeadToOpportunity(IOrganizationService service, Guid LeadID, Guid OpportunityID)
    {    
        ConditionExpression condition = new ConditionExpression();
        condition.AttributeName = "regardingobjectid";
        condition.Operator = ConditionOperator.Equal;
        condition.Values.Add(LeadID.ToString());
        var query = new QueryExpression("activitypointer");
        query.Criteria.AddCondition(condition);
        //query.Conditions.Add("reagrdingobjectid", ConditionOperator.Equal, theIdOfTheRelatedRecord);
        query.ColumnSet = new ColumnSet(true);

        var activities = service.RetrieveMultiple(query).Entities;
        foreach (var activity in activities)
        {
            var castedActivity = (ActivityPointer)activity;
           if (castedActivity.ActivityTypeCode == "email")
            {
                castedActivity.Id = Guid.NewGuid();
                castedActivity["regardingobjectid"] = new EntityReference("opportunity", OpportunityID);
                //service.Create(castedActivity);--->Exception thrown
                //service.Update(castedActivity);---->Tried this one too.Exception is thrown stating method not supported on "ActivityPointer"
            }


        }

有人可以解释一下吗?我在这里错过了什么吗?谢谢

4

2 回答 2

4

您需要查询确切的实体类型,因为您无法更新activitypointer

private void MoveEmailsFromLeadToOpportunity(IOrganizationService service, Guid LeadID, Guid OpportunityID)
    {    
        ConditionExpression condition = new ConditionExpression();
        condition.AttributeName = "regardingobjectid";
        condition.Operator = ConditionOperator.Equal;
        condition.Values.Add(LeadID.ToString());
        var query = new QueryExpression("email");
        query.Criteria.AddCondition(condition);
        query.ColumnSet = new ColumnSet(true);

        var emails = service.RetrieveMultiple(query).Entities;
        foreach (var email in emails)
        {
            email["regardingobjectid"] = new EntityReference("opportunity", OpportunityID);
            service.Update(email);
        }
    }

您还可以编写一个方法,首先检索所有活动(就像您已经做过的那样)检查ActivityTypeCode后检索并根据活动类型(电子邮件,传真,...)更新每条记录

于 2013-06-04T07:39:01.763 回答
0

尝试注释掉这一行:

castedActivity.Id = Guid.NewGuid();

然后做你的Update

service.Update(castedActivity)

您只是在更新RegardingObjectId,而不是创建新活动,因此您不应该更改 Id 并且不应该使用Create.

于 2013-06-04T01:53:21.050 回答