Hi Experts,
I have a task , that i have created a custom button on case form and on click of that custom button my description field in case form is updated to "ABC" and getting saved.Now I am writing a Plugin on which that if the case description is updated to "ABC" then case status should be "cancelled" and new case should be created.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity Case_E = (Entity)context.InputParameters["Target"];
if (Case_E.LogicalName == "incident")
{
if (Case_E.Attributes.Contains("description"))
{
string entityDescription = Case_E["description"].ToString();
if (entityDescription == "ABC")
{
Case_E["statuscode"] = new OptionSetValue(100000000);
}
}
}
}
Entity case_new = new Entity("case creation");
case_new.Attributes["customerid"] = new EntityReference("account", accountid)
case_new["new_caserecordtype"] = new OptionSetValue(12345);
Service.Create(case_new);
Service.Update(Case_E);
}
}
}
I am unable to get how to create a new case with the customer lookup field filled in it(customer lookup field is referring account entity)
Hi jakkani
Try below code :
case_new["customerid"] = new EntityReference("contact", contactid)
If found useful, please mark answer as verified.
Hi,
Because you are running you plugin on update, you will not get all the field. On Update plugin, only the fields which are changed appears. This is why you are not getting the account field. You need to register the image and get the account field from image.
Refer this: community.dynamics.com/.../utilising-pre-post-entity-images-in-a-dynamics-crm-plugin
Also, there are some other issues on your code-
1) You are using "case creation" as the name of entity in this statement "Entity case_new = new Entity("case creation");". This is incorrect cause here you need to pass the schema name of the field. As you are creating case, you should ass the schema name of case entity which is "incident". So this will become Entity case_new = new Entity("incident");
2) It is never a good idea to update the object which you are retrieving from target property i.e. Case_E object in this statement "Entity Case_E = (Entity)context.InputParameters["Target"];". Thsi is because when you update, the update plugin will again trigger and you will run into infite loop. To avoid this, you need to always create a new instance for update.
E.g.
Entity caseToUpdate = new Entity(Case_E.LogicalName, Case_E.Id);
caseToUpdate["statuscode"] = new OptionSetValue(100000000); // This line can still give you error if the statecode is not valid for your status code.
3) You are chaing teh value of description field and if it is ABC then setting the status code. Whereas after the if bloxk, tiu are updating the case_e entity. This means that if teh descripton is not ABC, the update will still call. So, you should always ensure that you update the record when it is actualy needed.
E.g.
if (entityDescription == "ABC")
{
Entity caseToUpdate = new Entity(Case_E.LogicalName, Case_E.Id);
caseToUpdate["statuscode"] = new OptionSetValue(100000000); // This line can still give you error if the statecode is not valid for your status code.
Service.Update(caseToUpdate);
}
** This also applies to your new cae creation logic, you are creating a new case outside of your if block. You may need to inlcude the new case creation logic as well inside your if block.
Hope this helps.
Update your code as below:
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity Case_E = (Entity)context.InputParameters["Target"];
if (Case_E.LogicalName == "incident")
{
if (Case_E.Attributes.Contains("description"))
{
string entityDescription = Case_E["description"].ToString();
if (entityDescription == "ABC")
{
Case_E["statuscode"] = new OptionSetValue(100000000);
accountid=(entityrefrence)Case_E.Attributes["customerid"];
Entity case_new = new Entity("incident");
case_new.Attributes["customerid"] = accountid
case_new["new_caserecordtype"] = new OptionSetValue(12345);
Service.Create(case_new);
Service.Update(Case_E);
}
}
}
}
}
}
}
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... 290,802 Super User 2024 Season 2
Martin Dráb 229,129 Most Valuable Professional
nmaenpaa 101,154