2
votes

Am having a plugin that moves the activities of lead to opportunity on qualify. I have registered the plugin on "Create" of opportunity and the following is the code

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


        }

Can somebody shed light on this? Am i missing something here? Thank you

2
Don't the Lead's emails move over to the Opportunity on Qualify anyway? Not sure why you'd need to do this.John Rogerson

2 Answers

4
votes

You need to query for the exact entity type, because you can't update an 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);
        }
    }

you can also write a method that will retrieve first all the activities (as you already did) check the ActivityTypeCode after retrieve and update each single record depending on activity type (email, fax, ...)

0
votes

Try commenting out this line:

castedActivity.Id = Guid.NewGuid();

And then just do your Update:

service.Update(castedActivity)

You are just updating the RegardingObjectId, not creating a new activity, so you shouldn't change the Id and you shouldn't use Create.