Answered by:
I need help for plug-in

Question
-
hi friends,
I want to rate the opportunity as cold,warm or hot regarding the value in est.revenue. I've written the following code,
public
void Execute(IServiceProvider
serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext
));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity
)
{
// Obtain the target business entity from the input parmameters.
Entity targetEntity = (Entity)context.InputParameters["Target"
];
try
{
decimal rev = (decimal)( targetEntity["estimatedvalue"
]);
if
(rev < 100000)
targetEntity.Attributes.Add(
"opportunityratingcode", new OptionSetValue
(1));
else if
(rev > 100000 && rev < 200000)
targetEntity.Attributes.Add(
"opportunityratingcode", new OptionSetValue
(2));
else
targetEntity.Attributes.Add(
"opportunityratingcode", new OptionSetValue
(3));
}
catch (Exception
ex)
{
throw
ex;
}
}
}
but its not working properly. The error that I got is 'type casting problem'. Can anyone help me?
Monday, February 6, 2012 6:38 AM
Answers
-
hi,
it could be because target entity does not have estimatedvalue attribute, i have changed the code little bit to throw an exception if the estimatedvalue doesn't exists in the target entity. also which plugin Message (update, create) you are registered with also the stage. if it is create and then user didn't key in any value on this column then the target entity will have this attribute, if it is update and then the user didn't key in anything, you will have to read from pre image (you need to add image step with your plugin registration step).
try { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target business entity from the input parmameters. Entity targetEntity = (Entity)context.InputParameters["Target"]; if (targetEntity.Attributes.Contains("estimatedvalue")) { decimal rev = targetEntity.GetAttributeValue<decimal>("estimatedvalue"); if (rev < 100000) targetEntity.Attributes.Add("opportunityratingcode", new OptionSetValue(1)); else if (rev > 100000 && rev < 200000) targetEntity.Attributes.Add("opportunityratingcode", new OptionSetValue(2)); else targetEntity.Attributes.Add("opportunityratingcode", new OptionSetValue(3)); } else throw new InvalidPluginExecutionException("Target Opportunity entity doesnot have estimatedvalue"); } } catch (InvalidPluginExecutionException) { throw; } catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException("An FaultException occurred in the Opportunity plug-in.", ex); } catch (Exception ex) { throw new InvalidPluginExecutionException("An Exception occurred in the Opportunity plug-in.", ex); }
Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Thomas Thankachan Monday, February 6, 2012 7:04 AM
- Marked as answer by Naveen kumar J Monday, February 6, 2012 11:00 AM
Monday, February 6, 2012 7:03 AM
All replies
-
hi,
it could be because target entity does not have estimatedvalue attribute, i have changed the code little bit to throw an exception if the estimatedvalue doesn't exists in the target entity. also which plugin Message (update, create) you are registered with also the stage. if it is create and then user didn't key in any value on this column then the target entity will have this attribute, if it is update and then the user didn't key in anything, you will have to read from pre image (you need to add image step with your plugin registration step).
try { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target business entity from the input parmameters. Entity targetEntity = (Entity)context.InputParameters["Target"]; if (targetEntity.Attributes.Contains("estimatedvalue")) { decimal rev = targetEntity.GetAttributeValue<decimal>("estimatedvalue"); if (rev < 100000) targetEntity.Attributes.Add("opportunityratingcode", new OptionSetValue(1)); else if (rev > 100000 && rev < 200000) targetEntity.Attributes.Add("opportunityratingcode", new OptionSetValue(2)); else targetEntity.Attributes.Add("opportunityratingcode", new OptionSetValue(3)); } else throw new InvalidPluginExecutionException("Target Opportunity entity doesnot have estimatedvalue"); } } catch (InvalidPluginExecutionException) { throw; } catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException("An FaultException occurred in the Opportunity plug-in.", ex); } catch (Exception ex) { throw new InvalidPluginExecutionException("An Exception occurred in the Opportunity plug-in.", ex); }
Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Thomas Thankachan Monday, February 6, 2012 7:04 AM
- Marked as answer by Naveen kumar J Monday, February 6, 2012 11:00 AM
Monday, February 6, 2012 7:03 AM -
Thanks a lot for replying my friend. But still it is not working. It throws the exception ,
catch (Exception ex)
{
throw new InvalidPluginExecutionException("An Exception occurred in the Opportunity plug-in.", ex);
}
Any idea??Monday, February 6, 2012 9:02 AM -
did you check innerexception details??
Mahain : Check My Blog
Follow me on Twitter
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Monday, February 6, 2012 9:04 AMModerator -
hi mahender pal, thank u for replying.
i am copying the error details,
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An Exception occurred in the Opportunity plug-in.Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
<ErrorCode>-2147220891</ErrorCode>
<ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<KeyValuePairOfstringanyType>
<d2p1:key>OperationStatus</d2p1:key>
<d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>An Exception occurred in the Opportunity plug-in.</Message>
<Timestamp>2012-02-06T08:04:49.4026919Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>[myplugin: myplugin.Class1]
[47242300-9350-e111-91aa-d8d3855be525: myplugin.Class1: Create of opportunity]
</TraceText>
</OrganizationServiceFault>any idea??
Monday, February 6, 2012 9:13 AM -
hi, change the following line to
decimal rev = targetEntity.GetAttributeValue<decimal>("estimatedvalue");
change to
decimal rev = targetEntity.GetAttributeValue<Money>("estimatedvalue").Value;
Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Thomas Thankachan Monday, February 6, 2012 9:27 AM
Monday, February 6, 2012 9:21 AM -
hi thomas,
I've changed it, but plugin is in same stage as i mentioned before.
any suggestion??
Monday, February 6, 2012 9:40 AM -
hi friends its working, i made some mistake with the plug-in registration tool. Thanks a lot to everyone who replied to me.Monday, February 6, 2012 10:59 AM