web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

The given key was not present in the dictionary

(0) ShareShare
ReportReport
Posted on by 5

i have created a plugin for updation of credit limit and creation of new credit card

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace AnnualIncome
{
public class AnnIncomePlugin : IPlugin
{

public void Execute(IServiceProvider serviceProvider)
{

ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));


IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));


if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{

Entity entity = (Entity)context.InputParameters["Target"];


if (entity.LogicalName != "contact")
{
return;
}

// Obtain the organization service reference which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);


if (context.Stage == 40)
{


if (entity.Contains("annualincome") && entity["annualincome"] != null && entity.Contains("dxc_country") && entity["dxc_country"] != null)
{
//annual income field
Money revenue = (Money)entity["annualincome"];
decimal revenue_value = revenue.Value;
double AnnIncome = (double)revenue.Value;
//country field
OptionSetValue country = (OptionSetValue)entity["dxc_country"];
int selectedOptionValue = country.Value;
String text = entity.FormattedValues["dxc_country"].ToString();


if (AnnIncome < 300000)
{
throw new InvalidPluginExecutionException("annual income must be more than 3 lakh");

}

else if (AnnIncome > 300000 && text == "India")
{

var Val1 = revenue_value * 0.35m;
string CreditcardLimit = Convert.ToString(Val1);
entity["dxc_creditlimit"] = CreditcardLimit;
service.Update(entity);

Entity contact = service.Retrieve(entity.LogicalName, entity.Id,new ColumnSet (true));
// creating a new credit card entity record

Entity NewCreditcard = new Entity("dxc_creditcard");
NewCreditcard["dxc_contact"] = new EntityReference("contact", entity.Id);
NewCreditcard["dxc_creditcardno"] = entity.Attributes["dxc_creditcard"];
NewCreditcard["dxc_creditcardtype"] = entity.Attributes["dxc_creditcardtype"];
NewCreditcard["dxc_status"] = entity.Attributes["dxc_status"];

service.Create(NewCreditcard);
}


}


}
}

}
}
}

error was coming for line (NewCreditcard["dxc_creditcardno"] = entity.Attributes["dxc_creditcard"];)  

the credit card field in the contact entity was created by using a java script and its not assaigning its value to the credit card entity and alsoo in debugging i found out that there is no dxc_creditcard field why is that??

I have the same question (0)
  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

    Hi  vinay9849,

    Please clear on below points:

    1. Are you getting this error on create of record or updating or record ?

    - If are getting error on update of record, please retrieve this value using post image and try

    2. Do you have this field in "credit card" entity ?

    - If you are having it already it then try to publish all once

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    You should first check the attribute if it is available in Entity Target object then use the value. So your code should be as below.

    if(entity.Attributes.Contains("dxc_creditcard"))

    NewCreditcard["dxc_creditcardno"] = entity.Attributes["dxc_creditcard"];

    You should add this check condition for other fields too.

    This is a best practice to developer any plugin code/custom c# code in CRM Dynamics.

    Please follow suggestion from above post regarding field missing while debugging.

    Please mark my answer verified if i were helpful

  • Suggested answer
    faheem123 Profile Picture
    107 on at

    Hi,

    var Val1 = revenue_value * 0.35m;
    string CreditcardLimit = Convert.ToString(Val1);
    entity["dxc_creditlimit"] = CreditcardLimit;
    service.Update(entity);

    Find above lines in your code and then replace those lines in your code with below lines

    var Val1 = revenue_value * 0.35m;
    string CreditcardLimit = Convert.ToString(Val1);

    Entity UpdatedEntity=new Entity('contact');

    UpdatedEntity.id=entity.id;

    UpdatedEntity["dxc_creditlimit"]=CreditcardLimit;

    service.Update(UpdatedEntity);

    Rather than using the same entity variable, It is always better to create a new in-memory entity variable and then perform our update operation.

    You are getting the error becoz, the value of entity["dxc_creditlimit"] is not available during plugin call and we are referring it.

    Please try above steps and let me know if it resolves your issue.

  • vinay9849 Profile Picture
    5 on at

    its not working.... bro same  error i guess the "dxc_creditlimit" field is not available in the entity

  • vinay9849 Profile Picture
    5 on at

    i have debugged but that field is not showing in the contact entity i guess because of that field(creditcard) gets updated on save of java script.

  • vinay9849 Profile Picture
    5 on at

    how to create a post image?

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    HI,

    Check below article to create post image.

    community.dynamics.com/.../pre-image-and-post-image-in-dynamics-crm

    Please mark my answer verified if i were helpful

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans