locked
Create Plugin of status from cases RRS feed

  • Question

  • Hi!

    I'm newbie on CRM but I want learn this technology.

    The plugin that I'm working must retrieve the Active cases on an Account and if an account have less than 5 active cases the plugin fill a specific text field with a string. For example if an account have less than 5 active cases fill a text field with "Green". This is my first plugin that I've created but it doesn't work and I don't why if any of you know what is my problem please explain me how to fix them

    The plugin message is:  Update, the primary entity: Cases(Incident), Async, Post-Operation

    the code is:

    protected void ExecutePostCaseUpdate(LocalPluginContext localContext)
    
    {
    
    if (localContext == null)
    
    {
    
    throw new ArgumentNullException("localContext");
    
    }
    
    IPluginExecutionContext context = localContext.PluginExecutionContext;
    
    Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
    
    Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
    
    // TODO: Implement your custom Plug-in business logic.
    
    var orgService = localContext.OrganizationService;
    
    // TODO: Implement your custom Plug-in business logic.
    
    Entity project = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
    
    CrmDataContext dataContext = new CrmDataContext(localContext.OrganizationService);
    
     dataContext.MergeOption = Microsoft.Xrm.Sdk.Client.MergeOption.NoTracking;
    
    Account acccount = new Account();
    
    //Declarative Syntax 
    
    //Linq query to retrieve active cases
    var ActiveCases = (from incidents in dataContext.IncidentSet
    
    join account in dataContext.AccountSet
    
    on incidents.CustomerId.Id equals account.AccountId
    
    orderby incidents.CustomerId
    
    where project.Id == account.Id
    
    select incidents.StateCode.Equals(0)).ToList();
    
     
    
    var SumCases = ActiveCases.Count();
    
    if (SumCases >= 0)
    
    {
    
     acccount.new_CaseStatus = "Green";
    
    orgService.Update(acccount);
    
    
    }

    Thanks in advance


    • Edited by Robert9121 Thursday, February 26, 2015 9:54 PM
    Thursday, February 26, 2015 9:43 PM

All replies

  • Hello,

    Seems that you haven't defined account id. Should be something like:

    account.Id = preentityImage.getAttribute<EntityReference>("customerid").Id;


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Thursday, February 26, 2015 9:55 PM
    Moderator