Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin exception with certain calculated field formulas

Posted on by 40

I have a calculated decimal field from which I pull the value in a plugin.

If the formula in the calculated field is setted as below, everything works:

8420.pic2.png

If I add two decimal fields to the formula I get an exception from my plugin:

2134.pic1.png

BTW the calculated field works well with this formula, everything is calculated correctly except the error from my plugin.

6758.pic3.png

Responsible is this line in the code

dauerstunden = r.Attributes["fis_dauerstunden"].ToString();

It's not found in the plugin dictionary anymore because I added two values to the calculated field formula.

*This post is locked for comments

  • cosmic-ian Profile Picture
    cosmic-ian 40 on at
    RE: Plugin exception with certain calculated field formulas

    Thank you very much Gopalan Bhuvanesh!

    This line solved my issue:

    dauerstunden = r.Attributes.Contains("fis_dauerstunden")?r.Attributes["fis_dauerstunden"].ToString() : string.Empty;

    Background info why this happened:

    I have a calculated field in appointments and when these are completed/closed and I change the formula of the calculated field. The calculated field(fis_dauerstunden) disappears in closed appointments. Maybe it's a bug or a feature. Apparently I also missed to check for null fields which I should have.

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Plugin exception with certain calculated field formulas

    The error is key not found exception.

    Check whether the attribute fis_dauerstunden is retrieved.

    In the query you must have added this to the columnset.

    Also verify whether the entity (r) has the attribute present in it

    r.Attributes.Contains("fis_dauerstunden")

    Like

    dauerstunden = r.Attributes.Contains("fis_dauerstunden")?r.Attributes["fis_dauerstunden"].ToString() : string.Empty;

    Please try the following, if fis_dauerstunden is a decimal field,

    dauerstunden = ((decimal)r.Attributes["fis_dauerstunden"]).ToString();

    (I typed manually here so check for the right syntax)

  • cosmic-ian Profile Picture
    cosmic-ian 40 on at
    RE: Plugin exception with certain calculated field formulas

    I'm interested to find out if this is a Microsoft Bug or not. I will use a workaround now and do the (calculated field) calculation in the Plugin now.

    I will mark your answer/s in the future, maybe someone knows the solution or finds out that this is a bug.

  • cosmic-ian Profile Picture
    cosmic-ian 40 on at
    RE: Plugin exception with certain calculated field formulas
    EntityCollection result1 = service.RetrieveMultiple(query1);
    
            foreach (var r in result1.Entities){
    
                  dauerstunden = r.Attributes["fis_dauerstunden"].ToString();
    
                  ...


    Again the code works fine, I just change the calculated field formula and the code errors.

  • Suggested answer
    Dynamics_Alok Profile Picture
    Dynamics_Alok 1,746 on at
    RE: Plugin exception with certain calculated field formulas

    if r is a EntityCollection result then to get entity attribute you should use r.Entities[0].Attribute .

  • cosmic-ian Profile Picture
    cosmic-ian 40 on at
    RE: Plugin exception with certain calculated field formulas

    r is a EntityCollection result in a retrieveMultiple Query, "fis_dauerstunden" is in the ColumnSet of this query.

    I don't change anything in the code, just the calculated field formula.

    Maybe the calculation of the field takes more time and is therefore not accesible in the dictionary.

    BTW thx for answering, I will try what you say now.

  • cosmic-ian Profile Picture
    cosmic-ian 40 on at
    RE: Plugin exception with certain calculated field formulas

    It's registered on Update, Post operation, Synchronous

  • Verified answer
    Dynamics_Alok Profile Picture
    Dynamics_Alok 1,746 on at
    RE: Plugin exception with certain calculated field formulas

    Can you share what is the trigger of this Plugin.On Update of any field or etc.

    r has no fis_dauerstunden field and thats why you are getting this error.

    Try to re-analyse the r entity ...and fetch the fis_dauerstunden attribute for it ,then use Convert.ToString in place of ToString .

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans