Answered by:
Plugin The given key was not present in the dictionary

Question
-
hi i want if we upadte topic and owner opportunity that update show in custom entity. but we get error
Plugin The given key was not present in the dictionary
i create a custom entity optyaudit trail with two feild newtopic and new ownermy code isusing System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Crm.Sdk;using Microsoft.Crm.SdkTypeProxy;using Microsoft.Crm.Sdk.Query;namespace OptyAuditTrail{public class optyaudit : IPlugin{public void Execute(IPluginExecutionContext context){if ((context.InputParameters.Properties.Contains("target")) && (context.InputParameters.Properties["target"] is DynamicEntity)){DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["target"];if (entity.Name == EntityName.opportunity.ToString()){Key id = (Key)entity.Properties["opportunityid"];Guid opportunityid = (Guid)id.Value;ICrmService service = context.CreateCrmService(true);TargetRetrieveDynamic retrievetarget = new TargetRetrieveDynamic();retrievetarget.EntityId = opportunityid;retrievetarget.EntityName = EntityName.opportunity.ToString();RetrieveRequest request = new RetrieveRequest();request.ColumnSet = new AllColumns();request.ReturnDynamicEntities = true;request.Target = retrievetarget;RetrieveResponse responce = (RetrieveResponse)service.Execute(request);entity = responce.BusinessEntity as DynamicEntity;audittrail(entity, context, opportunityid);}}}public void audittrail(DynamicEntity entity,IPluginExecutionContext context,Guid opportunityid){DynamicEntity audit= new DynamicEntity();Key id1= (Key)audit.Properties["new_opportunityauditid"];Guid auditid = (Guid)id1.Value;ICrmService service = context.CreateCrmService(true);TargetRetrieveDynamic retrievetarget = new TargetRetrieveDynamic();retrievetarget.EntityId= auditid;//retrievetarget.EntityName = EntityName.audit.ToString();RetrieveRequest request = new RetrieveRequest();request.ColumnSet = new AllColumns();request.ReturnDynamicEntities = true;request.Target = retrievetarget;RetrieveResponse responce = (RetrieveResponse)service.Execute(request);audit= responce.BusinessEntity as DynamicEntity;if(entity.Properties.Contains("name")){string name = entity.Properties["name"].ToString();audit.Properties["new_newtopic"] = name;}if(entity.Properties.Contains("ownerid")){string name = entity.Properties["ownerid"].ToString();audit.Properties["new_newowner"] = name;}ICrmService crmService = context.CreateCrmService(true);Guid id = crmService.Create(audit);}}}Friday, February 12, 2010 7:15 AM
Answers
-
Hi kamal,
First, I will suggest you to register post and pre images and then compare values of owner,topic and write in your custom entity if there is difference between them.
Second it's better to debug your code and find where you are getting this error and check for the proper schema name.
Hope it will help you !!
Mahain- Marked as answer by DavidJennawayMVP, Moderator Wednesday, March 10, 2010 4:56 PM
Friday, February 12, 2010 7:44 AMModerator -
Hi, your function audittrail() has some mistakes, here i have correct it below.
public void audittrail(DynamicEntity entity,IPluginExecutionContext context,Guid opportunityid){DynamicEntity audit= new DynamicEntity("new_audit");
// the below three lines are required only if you are updating the new_audit entity so i will comment tehm since you are creating a new entity.
/* KeyProperty p1 = new KeyProperty("new_opportunityauditid", new Key());
p1.Value = ((Lookup)entity.Properties["new_opportunityauditid"]).Value;
audit.Properties.Add(p1);
*/
StringProperty p2 = new StringProperty("new_new_topic", entity.properties["name"]).ToString());
audit.Properties.Add(p2);ICrmService service = context.CreateCrmService(true);
TargetCreateDynamic targetCreate = new TargetCreateDynamic();
targetCreate.Entity = audit;
// Create the request object.
CreateRequest create = new CreateRequest();
create.Target = audit;
// Execute the request.
ICrmService service = context.CreateCrmService(true);
CreateResponse created = (CreateResponse)service.Execute(create);}- Edited by Muhammad Ali Khan Friday, February 12, 2010 7:52 AM error fixed.
- Proposed as answer by Muhammad Ali Khan Friday, February 12, 2010 1:27 PM
- Marked as answer by DavidJennawayMVP, Moderator Wednesday, March 10, 2010 4:56 PM
Friday, February 12, 2010 7:46 AM
All replies
-
Hi kamal,
First, I will suggest you to register post and pre images and then compare values of owner,topic and write in your custom entity if there is difference between them.
Second it's better to debug your code and find where you are getting this error and check for the proper schema name.
Hope it will help you !!
Mahain- Marked as answer by DavidJennawayMVP, Moderator Wednesday, March 10, 2010 4:56 PM
Friday, February 12, 2010 7:44 AMModerator -
Hi, your function audittrail() has some mistakes, here i have correct it below.
public void audittrail(DynamicEntity entity,IPluginExecutionContext context,Guid opportunityid){DynamicEntity audit= new DynamicEntity("new_audit");
// the below three lines are required only if you are updating the new_audit entity so i will comment tehm since you are creating a new entity.
/* KeyProperty p1 = new KeyProperty("new_opportunityauditid", new Key());
p1.Value = ((Lookup)entity.Properties["new_opportunityauditid"]).Value;
audit.Properties.Add(p1);
*/
StringProperty p2 = new StringProperty("new_new_topic", entity.properties["name"]).ToString());
audit.Properties.Add(p2);ICrmService service = context.CreateCrmService(true);
TargetCreateDynamic targetCreate = new TargetCreateDynamic();
targetCreate.Entity = audit;
// Create the request object.
CreateRequest create = new CreateRequest();
create.Target = audit;
// Execute the request.
ICrmService service = context.CreateCrmService(true);
CreateResponse created = (CreateResponse)service.Execute(create);}- Edited by Muhammad Ali Khan Friday, February 12, 2010 7:52 AM error fixed.
- Proposed as answer by Muhammad Ali Khan Friday, February 12, 2010 1:27 PM
- Marked as answer by DavidJennawayMVP, Moderator Wednesday, March 10, 2010 4:56 PM
Friday, February 12, 2010 7:46 AM -
and for assigning the owner, use the AssignRequest, here is on how to use it.
// Create the SecurityPrincipal object.
SecurityPrincipal assignee = new SecurityPrincipal();
assignee.Type = SecurityPrincipalType.User;// PrincipalId is some known Guid belonging to the user or team that will own this record.
assignee.PrincipalId = new Guid("326A0053-71CB-465E-9BEB-633E2E0851A9");// Create the target object for the request.
TargetOwnedDynamic target = new TargetOwnedDynamic ();// Set the properties of the target object.
// EntityId is some known Guid belonging to the account that is being assigned to the user.
target.EntityId = auditObjectID;
target.EntityName = "new_audit";// Create the request object.
AssignRequest assign = new AssignRequest();// Set the properties of the request object.
assign.Assignee = assignee;
assign.Target = target;
// Execute the request.
AssignResponse assignResponse = (AssignResponse)service.Execute(assign);- Proposed as answer by Muhammad Ali Khan Friday, February 12, 2010 1:27 PM
Friday, February 12, 2010 7:58 AM