XRM-OSS / Xrm-Templating-Language

A domain specific language for Dynamics CRM allowing for easy text template processing
https://xrmadventuretime.com/tag/xtl/
MIT License
22 stars 8 forks source link

Cast error in XTLProcessor.Execute #17

Closed andreaPorini closed 4 years ago

andreaPorini commented 4 years ago

Hi @DigitalFlow,

I found an error during the execution of the plugin, seems like there is a mismatch of types during the configuration retrieve.

This is the StackTrace of the error:

` Unhandled exception: Exception type: System.ServiceModel.FaultException'1[Microsoft.Xrm.Sdk.OrganizationServiceFault] Message: An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute): Xrm.Oss.XTL.Templating.XTLProcessor: System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'.Detail:

650f35b6-f0cf-40bc-8bf1-320aad366d48 -2147220956 An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute): Xrm.Oss.XTL.Templating.XTLProcessor: System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityReference' to type 'System.String'. 2019-11-29T10:07:41.776699Z false PluginExecution System.InvalidCastException at Microsoft.Xrm.Sdk.Entity.GetAttributeValue[T](String attributeLogicalName) at Xrm.Oss.XTL.Templating.XTLProcessor.RetrieveTemplate(String template, String templateField, Entity dataSource, IOrganizationService service, ITracingService tracing) at Xrm.Oss.XTL.Templating.XTLProcessor.HandleNonCustomAction(IPluginExecutionContext context, ITracingService tracing, IOrganizationService service) at Xrm.Oss.XTL.Templating.XTLProcessor.Execute(IServiceProvider serviceProvider) at Microsoft.Crm.Sandbox.SandboxAppDomainHelper.<>c__DisplayClass15_3.<Execute>b__0() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Crm.Sandbox.SandboxAppDomainHelper.Execute(IOrganizationServiceFactory organizationServiceFactory, Dictionary'2 sandboxServices, String pluginTypeName, String pluginConfiguration, String pluginSecureConfig, IPluginExecutionContext requestContext, Boolean enablePluginStackTrace, Boolean chaosFailAppDomain, String crashOnPluginExceptionMessage) at Microsoft.Crm.Sandbox.SandboxWorker.<>c__DisplayClass3_0.<Execute>b__0() Initiating interpreter Processing handler If Processing handler Not Processing handler IsNull Successfully processed handler IsNull Successfully processed handler Not Executing true condition Successfully processed handler If

`

Can you help me identifyng what's wrong?

Best regards, Andrea

DigitalFlow commented 4 years ago

Hi @andreaPorini,

sure, when is this problem occuring? Seems like it is receiving an entity reference when it is expecting a string.

Kind Regards, Florian

andreaPorini commented 4 years ago

Thank you Florian,

i've configured my step like this: step-configuration

It triggers when I update the HTML Template lookup field, writing the rendered template into the "renderedtemplate" field.

The template il very basic, one textbox with ${{Value ("prn_code")}}, nothing more.

When i update and the execution criteria returns "true", i get the exception I wrote before.

Thanks again, Andrea

DigitalFlow commented 4 years ago

Hi @andreaPorini,

found the issue. The template field is for per-record templating, for example when you have a source field in your entity which contains the template text already instead of a global one. If you want to pull the template from a HTML template, please clear the template field input and write in the lower global template input:

${{Value("prn_htmltemplateid.oss_html")}}

Kind Regards, Florian

andreaPorini commented 4 years ago

Hi @DigitalFlow ,

ok with this workaround I do not get the exception but the template is not correctly parsed.

As I said, my sample template now contains only "${{Value ("prn_code")}}", with your workaround i get the template but in my target field I find the template with "${{Value ("prn_code")}}" printed in and not the code I expected to see.

Seems like the template is "parsed" only for the first time, fetching the content.

Do you have any idea to solve this?

Best regards, Andrea

DigitalFlow commented 4 years ago

Hi @andreaPorini,

yes, there's always two steps required for this scenario: one for injecting the template and one for applying it. When you inspect the SDK steps which are shipped with Xrm-Html-Templating, you will see that there are also always two steps: One for inserting and one for applying it.

You would now need a second step on the same message, which runs on rank 2 and which has prn_renderedtemplate as target field and now also as template field (the one which you set in the beginning). On this second iteration, XTL will get the raw template that we inserted, apply it and write the result back to the same field.

Kind Regards, Florian

andreaPorini commented 4 years ago

Ok I understand the process, but, if I remember well, in the example you use in your other repo HTML templating tool (https://xrmadventuretime.com/tutorials/html-e-mails-with-place-holders-in-dynamics-365/) you use the same method I used initially to insert the template in an Email description field, why in this case the process is different?

I have both solutions installed in my CRM instance.

Thank you for your responses and you're patience, Andrea

DigitalFlow commented 4 years ago

Hi @andreaPorini,

good that you ask. I forgot that I created a patch at some point, that you could call functions inside the per record templating as well. Can you try to clear the lower global template field again and use this in the upper per-record template input: Value("prn_htmltemplateid.oss_html")

The plugin should recognize that the per-record template input is not a plain field and try to execute it, so one step should suffice like that.

Kind Regards, Florian

andreaPorini commented 4 years ago

Hi @DigitalFlow,

that's it! Perfect, this way the plugin is working correctly.

As always, thank you for your assistance and for this awesome addon!

Best regards, Andrea

DigitalFlow commented 4 years ago

Hi @andreaPorini,

awesome! :) I'm glad that you like it 👍

Kind Regards, Florian