Hi,
I'm trying to create a plugin which will take set an entity to a dynamic value based on the primary entity's Id. Essentially, when I create a new 'opportunity' entity, I want to update a text field containing a related url. I think this should be quite simple, and I can get this to work when creating a new 'opportunity', but for some reason running the workflow on an existing 'opportunity' gives the following error: "The given key was not present in the dictionary".
I have seen similar issues online, but nothing that quite solved this, can anyone help?
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); OrganizationServiceContext ctx = new OrganizationServiceContext(service); Entity targetEntity = (Entity)context.InputParameters["Target"]; { //retrieve Record GUID Guid recordId = (Guid)context.PrimaryEntityId; //Set Field Value = url + recordId targetEntity["web_url"] = "https://website.com/" + recordId; service.Update(targetEntity); }
*This post is locked for comments
The reason is simple. Because it's not a plugin InputParameters will not contain Target that is caused exception you experienced. Instead of getting all the target you can use data from Activity context to grab entity name and id to update it.
Actually Andrew, sorry this solution (once I included that line) does work, it just needed that page to be saved for the field to populate. Thank you!
Would you be able to explain briefly why this worked and the other didn't?
Thanks again.
Hi Andrew, thanks for that. I assume you forgot to add in the line:
Guid recordId = (Guid)context.PrimaryEntityId;
Otherwise recordId is never declared, so I have added this in.
This has gotten rid of the 'given key not present' error, but the field is still not populating.
Hello,
Following code should work fine:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Crm.Sdk.Messages; using System.Activities; using System.Collections.ObjectModel; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk.Client; namespace WebURLWorkflow { public class WebURL : CodeActivity { protected override void Execute(CodeActivityContext executionContext) { IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); var targetEntity = new Entity(context.PrimaryEntityName, context.PrimaryEntityId); targetEntity["web_url"] = "https://website.com/" + recordId; service.Update(targetEntity); } } }
Hi, thanks for the help, but that has not fixed it. I don't understand why it would pass to the field on create but not update.
Hey,
Could you please verify if you're passing value to the field "web_url" in update operation because in InputParameters["Target"] we get the fields which are modified only during the update operation.
Could you try declaring the new instance of Entity object before setting the value to the field "web_url" like below:
Entity targetEntity = (Entity)context.InputParameters["Target"];
//retrieve RecordGUID
Guid recordId = (Guid)context.PrimaryEntityId;
//Set Field Value = url + recordId
var targetEntity1=new Entity();
targetEntity1.Id=recordId;
targetEntity1["web_url"] = "https://website.com/" + recordId;
service.Update(targetEntity1);
Hope this helps you.
Yes, sorry you are correct, it is a custom workflow I am building. 'web_url' is an attribute of the 'targetEntity'.
Here is the full code, but there is not much more to add.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Crm.Sdk.Messages; using System.Activities; using System.Collections.ObjectModel; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk.Client; namespace WebURLWorkflow { public class WebURL : CodeActivity { protected override void Execute(CodeActivityContext executionContext) { //Create the context IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); OrganizationServiceContext ctx = new OrganizationServiceContext(service); Entity targetEntity = (Entity)context.InputParameters["Target"]; { //retrieve Record GUID Guid recordId = (Guid)context.PrimaryEntityId; //Set Field Value = url + recordId targetEntity["web_url"] = "https://website.com/" + recordId; service.Update(targetEntity); } } } }
Nope, it's late bound.
My concern is what is the type of outcome "targetEntity " here below code if I consider as plugin perspective -it is early bound object right?
Entity targetEntity = (Entity)context.InputParameters["Target"];
For early bound there should be some class mentioned other then "Entity" like
var target = (Account)context.InputParameters["Target"];
So for me it's still not clear - where did you see Early Bound?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156