ImanKazemi
ImanKazemi

Reputation: 183

Dynamics CRM 2011 Plugin : Create New Phone Call After Complete An Old Phone Call

I want to create a new plugin to create new phone call when last phone call status is going to completed.

I'm writing below code but it's create duplicate key exception...

if (entity.Attributes.ContainsKey("statuscode") &&
   ((OptionSetValue)entity.Attributes["statuscode"]).Value == 2)
    {
        if (preTarget != null)
        {
            try
            {

                 var newCall = new Entity("phonecall");

                 newCall.Attributes.Add("to", preTarget.Attributes["to"]); 

                 newCall.Attributes.Add("from", preTarget.Attributes["from"]);

                 newCall.Attributes.Add("subject", preTarget.Attributes["subject"]);

                 newCall.Attributes.Add("ownerid", preTarget.Attributes["ownerid"]);

                 organizationService.Create(newCall);

        }
        catch (Exception exception)
        {

           throw new InvalidPlugiExecutionException(exception.Message, exception);
        }
    }
}

Upvotes: 0

Views: 546

Answers (1)

Andrew Butenko
Andrew Butenko

Reputation: 5446

You will have to change approach for to and from fields. Try to use following code for to and from fields:

if (preTarget.Contains("to"))
{
Entity to1 = new Entity("activityparty");
to1["partyid"] = preTarget.getAttributeValue<EntityCollection>("to").Entities[0]["partyid"];

newCall["to"] = new Entity[] { to1 };
}

Upvotes: 2

Related Questions