Custom entities and attributes in a plug-in

You cannot create a webservice in Visual Studio when you're writing a plugin. Instead you need to use the method context.CreateCrmService(true);. There is only one single issue when using this approach. You cannot leverage the power of IntelliSense in Visual Studio. Visual Studio will not recognize your custom entities and attributes. How to work with this? The only correct answer is "Dynamic Entities".

To retrieve records based on some attribute values you could use a piece of code like this:


// Create the QueryByAttribute object.
QueryByAttribute query = new QueryByAttribute();
query.ColumnSet = new AllColumns();
query.EntityName = "new_customentity";
query.Attributes = new string[] { "new_customentityid" };
query.Values = new string[] { customEntityId };

// Create the request.
RetrieveMultipleRequest request = new RetrieveMultipleRequest();

// Set the request properties.
request.Query = query;
request.ReturnDynamicEntities = true;

// Execute the request.
RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);

One of the most important details in this piece of code is this line:

request.ReturnDynamicEntities = true;

If you forget this line, you'll end up with this error message:

System.InvalidOperationException was unhandled by user code Message="There is an error in XML document (1, 509)." Source="System.Xml" StackTrace: at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle) at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) at Microsoft.Crm.SdkTypeProxy.CrmService.Execute(Request Request) at Microsoft.Crm.Asynchronous.SdkTypeProxyCrmServiceWrapper.Execute(Object request) at ...your assembly information...

So, don't forget the ReturnDynamicEntities attribute and you should be fine to go!

11 comments:

shilpa said...

Hi Ronald,

Thakns for all ur replies.
It is of gr8 help for me.
We are facing an issue in assigning tasks(an activity for that matter).
Could pls let me know apart from assign privilege what are the other privileges on other entites is required?

Thanks in advance

Ronald Lemmen said...

One approach you can use to find that out, is to start a database trace in the SQL profiler. It will perform an sql query which will request specific privileges. You can then check which privilege is missing.

Ronad

shilpa said...

Hi Ronald,

Thanks a lot for your suggestion... I will try it out.

Also, we are facing an issue with the Async plugins. We have async plugins which is used for sharing records based on the users identified on the form and their managerial hierarchy.

Problem is that this Async plugin will not fire sometimes. It will not throw any exceptions also, since we are logging all exceptions....

couls you pls guide me under what circumstances AsyncPlugin will fail??

Also we are periodically cleaning our AsyncOperationBase Table.

Your guidance is appreciated.

Many Thanks

Ronald Lemmen said...

Hi Shilpa,

Plug-ins might not fire when they're executed in the child pipeline whereas your plug-in is registered against the parent plug-in pipeline. If that's not the case and you see no error messages, then create a support request and ask Microsoft to assist you.

Hope this helps,
Ronald

shilpa said...

Hi Ronald,

As per ur suggestion we have a logged a support case with MS.

Also, we are facing issues with IFD deployment.

How to implement IFD for custom websites.. that will have updations to core entities of MS CRM like opportunity

Many Thanks in advance.

srinivas said...

Hi Ronald,

I had an issue of updating the cutom attributes mtc_clocktime on the email form when the user clicks save as completed to completed, using a plugin's. i am new to plugin, had an little idea. can you please help me how to achieve the task.

Thanks
srinivas
srinvias@mtccrm.com

shilpa said...

Hi Ronald,

Could u please let me know whether we can upate base currency value of any money field.

As far as my knowledge goes it can't be updated.

We are facing issues with multi currency. Since the exchnage rates differ est revnue at opp and its one of child entities are different. This equires updation of base currency.

Awaiting for ur suggestion.

many thanks in advance.

Pls reply

Ronald Lemmen said...

Dear Shilpa,

Please post your comments in complete English words and sentences, that makes it easier for me to read and to avoid misunderstandings.

As you have noticed, you cannot change the value of the base currency. The base currency is set while setting up the crm instance. Whenever you enter a value in the system, it will automatically calculate the base value belonging to that value based on the exchange rates defined in the system. I'm sure that it is possible to solve your issue without changing the value of the base currency.

Ronald

Divya said...

Hi ronald,
I have a requirement that when i select a bit field in a section of atab,that bit field should be moved to other tab in a new section of the same form.How to move the field from one section to other dynamically using javascript and that too the section should be created dynamically.please help me

Thanks
Divya

Ronald Lemmen said...

Hi Divya,

That is a good question. I'm not aware of a solution for your situation yet. There should be possibilities by rewriting the html output by javascript, but I haven't been diving into the details of that approach yet. It would be great if you can find out how to do that and share your findings with the other readers on this blog.

I know it's not the answer you were hoping for, but I'm sure you or one of your peers is able to create this new features!

Good luck,
Ronald

ngocchau said...

Hi Ronald Lemmen

I want the points for the saleman the number of products sold.
If the product sold in the state, I will point to the saleman that area.
Can you guide me catch Sold status of the product, I use custom workflow activity

Forward to help
Thanks