Skip to main content

Notifications

Customer Service forum

Create a new case on update of old case.

Posted on by Microsoft Employee

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) 

Categories:
  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Create a new case on update of old case.

    Hi jakkani

    Try below code :

    case_new["customerid"] = new EntityReference("contact", contactid)

    If found useful, please mark answer as verified.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Create a new case on update of old case.

    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.

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Create a new case on update of old case.

    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);

    }

    }

    }

    }

    }

    }

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,802 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,129 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,154

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans