Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Field value not picking in create plugin

(0) ShareShare
ReportReport
Posted on by 435

Hi Friends, 

I have a plugin on create written on a opportuninty form .

In that I have fields  AnnualValue  and AnnualValue_base of currency types. AnualValue_base is set to type USD dollar because default set of currency .
In form AnnualValue_base is hidden and user can enter Amount in Annual Value by selecting any currency type.

I have requirement is that user enter the value in AnnualValue and after some logic it should take value from AnnualValue_base  and set it another field Revenue.

e.g

Annual value - 100   -- user enter the value in form 

Currency type- Euro  -- -- user enter the value in form 

Annual Value_base($) - 117  -- Hidden(117 is setting by default)

Revenue  =  Annual Value_base($) * (some logic value)

ie = Revenue = 117 * 10 =  $1170

But In plugin I am getting error at 

Money AnnualVal = (Money)(entity.Attributes["annualvalue_base"]);  //

//****************************************************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace OnCreateCalculatesRevenue
{
public class CreateCalculatedRevenue:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//throw new NotImplementedException();

//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
if (context.Depth > 1) { return; }
Entity entity = (Entity)context.InputParameters["Target"];
try
{

Money AnnualVal = (Money)(entity.Attributes["annualvalue_base"]);
decimal AnnualValue = AnnualVal.Value;

// some logic 

  decimal rev = AnnualValue  * 100 * 12 ;

// some logic end 


entity.Attributes["revenue1"] = rev ;
IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred -in.", ex);
}


}

//****************************************************************************************************************

 

*This post is locked for comments

  • windyMill Profile Picture
    windyMill 435 on at
    RE: Field value not picking in create plugin

    Thanks

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Field value not picking in create plugin

    Why are you insisting on doing that in pre-create? Move it to post-create..

  • windyMill Profile Picture
    windyMill 435 on at
    RE: Field value not picking in create plugin

    Problem is that how to retrieve the annual_base value in plugin on pre-create

    Because we need to calculate the revenue based on annual_base

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Field value not picking in create plugin

    Do you need to use the annualvalue_base field, or can you use annualvalue?

    Not sure if the base value is populated for you in the Target entity, which most likely is why you are getting the key not found error.

    You have another choice if you don't want to use Images:

    If you must use the annualvalue_base, you can also call a retrieve method to retrieve that value from the entity record, when the record is created (assuming this is a Post Create Operation).

    Basically after this line of code:

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

    check if you have the annualvalue_base:

    if (entity.contains("annualvalue_base))

    {

      // Do your calculation here or call function to do calculation and pass values to it

    }

    else

    {

      entity = service.Retrieve(entityName, entityId, columns);

      // calculate here or call function to do calculation and pass values to it

    }

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Field value not picking in create plugin

    Should not be a problem if it's a post-create plugin (but you'll need to add "update" call to your code to update that record once you've populated the field.. since you'll be in post-create)

  • windyMill Profile Picture
    windyMill 435 on at
    RE: Field value not picking in create plugin

    Its a create operation

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Field value not picking in create plugin

    Hi,

     I'm pretty sure "_base" values are set through internal plugins - they are not coming from the client, so you can't just pick them up from the "target".

     Either add a PostImage (if it's a post-operation plugin) and include that attribute into the post image, or make a Retrieve call back to the server and retrieve "base" value (and it may have to be a post-operation plugin anyway)

  • windyMill Profile Picture
    windyMill 435 on at
    RE: Field value not picking in create plugin

    Key not found

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Field value not picking in create plugin

    What is the error ??

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans