Answered by:
Unable to retrieve account EntityReference from contact entity on update message.

Question
-
Hello All,
I have a plugin which will be triggered on Create and Update of Contact field. In the Plugin I want to access related (Account) entity. I am able to access this when the plugin is triggered for Create but I get exception for update message at the following step:
EntityReference accountref = (EntityReference)entity["parentcustomerid"];
What I think is I need to use is PreImage and PostImage to compare the values. If this is the case how can I find out weather the plugin is triggered for Create or Update so that I can avoid retrieving PreImage values for Create message?
Any help appreciated.
Thanks
Vikram
- Edited by Vikram96 Sunday, September 29, 2013 8:27 PM
Sunday, September 29, 2013 8:19 PM
Answers
-
You can get this from the IPluginExecutionContext.MessageName property
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Proposed as answer by HIMBAPModerator Monday, September 30, 2013 8:17 AM
- Marked as answer by Vikram96 Monday, September 30, 2013 1:38 PM
Monday, September 30, 2013 5:26 AMModerator -
Hi Vikram,
In case of Update, If you are not updating the parent customer field then you will not get this in to the target. so in this case you need to get this from preimage entity .
Hope this will help you.
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.
- Proposed as answer by Kamalkant Sharma Monday, September 30, 2013 6:37 AM
- Marked as answer by Vikram96 Monday, September 30, 2013 1:39 PM
Monday, September 30, 2013 6:37 AM -
Hi Vikram,
The reason for which you are getting issue is target doesn't contain parentcustomerid field. You will always get only those fields in target those are updated or populated while creation.
You should always check if target contains the field or not before reading.
if(entity.attributes.Contains("parentcustomerid") && entity.attributes["parentcustomerid"]!=null)
{
EntityReference accountref = (EntityReference)entity["parentcustomerid"];
}
You can also check MessageName property of Context that will give you the message for which plugin fired.
HTH!
Thanks!
- Proposed as answer by HIMBAPModerator Monday, September 30, 2013 8:19 AM
- Marked as answer by Vikram96 Monday, September 30, 2013 1:38 PM
Monday, September 30, 2013 7:56 AM -
Hi,
Just check like below
if(context.MessageName=="Create")
{//code for create}
if(context.MessageName=="Update")
{//code for update}
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Proposed as answer by HIMBAPModerator Monday, September 30, 2013 8:19 AM
- Marked as answer by Vikram96 Monday, September 30, 2013 5:34 PM
Monday, September 30, 2013 8:19 AMModerator
All replies
-
You can get this from the IPluginExecutionContext.MessageName property
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Proposed as answer by HIMBAPModerator Monday, September 30, 2013 8:17 AM
- Marked as answer by Vikram96 Monday, September 30, 2013 1:38 PM
Monday, September 30, 2013 5:26 AMModerator -
Hi Vikram,
In case of Update, If you are not updating the parent customer field then you will not get this in to the target. so in this case you need to get this from preimage entity .
Hope this will help you.
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.
- Proposed as answer by Kamalkant Sharma Monday, September 30, 2013 6:37 AM
- Marked as answer by Vikram96 Monday, September 30, 2013 1:39 PM
Monday, September 30, 2013 6:37 AM -
Note even for "Create" you may not always get the parentcustomerid field. If the user does not set the parent customer for the contact while creating the contact you would not get it even in Create.
You should check for exists in the entity attribute collection before reading it.
HTH
Sam
Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"
Monday, September 30, 2013 7:30 AM -
Hi Vikram,
The reason for which you are getting issue is target doesn't contain parentcustomerid field. You will always get only those fields in target those are updated or populated while creation.
You should always check if target contains the field or not before reading.
if(entity.attributes.Contains("parentcustomerid") && entity.attributes["parentcustomerid"]!=null)
{
EntityReference accountref = (EntityReference)entity["parentcustomerid"];
}
You can also check MessageName property of Context that will give you the message for which plugin fired.
HTH!
Thanks!
- Proposed as answer by HIMBAPModerator Monday, September 30, 2013 8:19 AM
- Marked as answer by Vikram96 Monday, September 30, 2013 1:38 PM
Monday, September 30, 2013 7:56 AM -
Hi,
Just check like below
if(context.MessageName=="Create")
{//code for create}
if(context.MessageName=="Update")
{//code for update}
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Proposed as answer by HIMBAPModerator Monday, September 30, 2013 8:19 AM
- Marked as answer by Vikram96 Monday, September 30, 2013 5:34 PM
Monday, September 30, 2013 8:19 AMModerator