Answered by:
How to read value from multiple line text field in CRM 2011 plugin

Question
-
I have created a plugin in which I am reading the body (description) of the email. I am using the below code in the plugin to read the email body.
Entity email_entity = service.Retrieve("email", entity.Id, new ColumnSet("subject", "description")); string body = email_entity["description"];
But I am getting NullReferenceException when in the second line. The above code works fine when I try to read for the subject field which is a single line of text.
Please let me know what I am doing wrong here.
Thanks!!
Monday, April 16, 2012 11:36 AM
Answers
-
You may have to use attributes property like
string body = email_entity.Attributes["description"].ToString();
Thanks Anil Chelasani http://exploringxrm.wordpress.com
- Proposed as answer by Anil_Ch Monday, April 16, 2012 11:45 AM
- Marked as answer by Prashant Khandelwal Monday, April 16, 2012 1:15 PM
Monday, April 16, 2012 11:45 AM
All replies
-
You may have to use attributes property like
string body = email_entity.Attributes["description"].ToString();
Thanks Anil Chelasani http://exploringxrm.wordpress.com
- Proposed as answer by Anil_Ch Monday, April 16, 2012 11:45 AM
- Marked as answer by Prashant Khandelwal Monday, April 16, 2012 1:15 PM
Monday, April 16, 2012 11:45 AM -
Thanks for the response Anil!!
Here is a thing I am trying to do:
When an e-mail is received the the plugin will read the e-mail body and then read the XML in the body of the e-mail and then creates a contact. The e-mail is being sent from an application (ASP.NET app).
Here is the code I am using now:
string body = email_entity.Attributes["description"].ToString(); var contact = new Contact() { FirstName = body, LastName = "Khandelwal123", MobilePhone = "12345678", EMailAddress1 = "marykay@contoso.com", OwnerId = new EntityReference("systemuser", context.UserId), }; oContext.AddObject(contact); oContext.SaveChanges();
It is now throwing this error:
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): Microsoft.Crm.Sdk.Samples.RegistrationPlugin: System.Exception: Microsoft.Xrm.Sdk.SaveChangesException: An error occured while processing this request. ---> System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Generic SQL error. Server stack trace: at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) Exception rethrown at [0]: at Microsoft.Crm.Sandbox.SandboxOrganizationService.Execute(String operation, Byte[] serializedRequest) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext) Exception rethrown at [1]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Crm.Sandbox.ISandboxOrganizationService.Execute(String operation, Byte[] serializedRequest) at Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper.ExecuteInternal(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChange(OrganizationRequest request, IList`1 results) --- End of inner exception stack trace --- at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChanges(SaveChangesOptions options) at Microsoft.Crm.Sdk.Samples.RegistrationPlugin.Execute(IServiceProvider serviceProvider) System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Generic SQL error. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).Detail: <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts"> <ErrorCode>-2147220956</ErrorCode> <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /> <Message>Unexpected exception from plug-in (Execute): Microsoft.Crm.Sdk.Samples.RegistrationPlugin: System.Exception: Microsoft.Xrm.Sdk.SaveChangesException: An error occured while processing this request. ---> System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Generic SQL error. Server stack trace: at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) Exception rethrown at [0]: at Microsoft.Crm.Sandbox.SandboxOrganizationService.Execute(String operation, Byte[] serializedRequest) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext) Exception rethrown at [1]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Crm.Sandbox.ISandboxOrganizationService.Execute(String operation, Byte[] serializedRequest) at Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper.ExecuteInternal(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChange(OrganizationRequest request, IList`1 results) --- End of inner exception stack trace --- at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.SaveChanges(SaveChangesOptions options) at Microsoft.Crm.Sdk.Samples.RegistrationPlugin.Execute(IServiceProvider serviceProvider) System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Generic SQL error. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).</Message> <Timestamp>2012-04-16T11:53:59.6939021Z</Timestamp> <InnerFault i:nil="true" /> <TraceText> [Plugin: Microsoft.Crm.Sdk.Samples.RegistrationPlugin] [2b4f9dd3-ba87-e111-b433-d8d3855b0511: Microsoft.Crm.Sdk.Samples.RegistrationPlugin: Create of email] </TraceText> </OrganizationServiceFault>
Monday, April 16, 2012 11:54 AM -
Check the column lenghts for FirstName of Contact and Email body.
Its says Generic SQL Error, so i am just thinking Email body string length is exceeding the capacity of Contact firstname field. Try that once
Thanks Anil Chelasani http://exploringxrm.wordpress.com
Monday, April 16, 2012 12:01 PM -
I am just writing my first name in the body field nothing more than that.Monday, April 16, 2012 12:23 PM
-
Try to enable trace and check for the detailed error description. Here is the link to start with tracing
http://exploringxrm.wordpress.com/2012/01/12/enabling-trace-in-microsoft-dynamics-crm-2011/
Thanks Anil Chelasani http://exploringxrm.wordpress.com
Monday, April 16, 2012 12:32 PM -
I found the problem...it is that the body is not just the text it is HTML!!! and to collect the details from the body we have to parse the HTML and then set the values.
Thanks for the help Anil!
Monday, April 16, 2012 12:32 PM