Spark engine conditional test - spark-view-engine

We use spark to generate HTML-mails. When merging our data into the template I noticed a difference between
<if condition="Equals(#context.UserAccount.Country,'BE')">
<p>You live in Belgium</p>
</if>
<else>
<p>You don't live in Belgium</p>
</else>
and
<if condition="#context.UserAccount.Country == 'BE'">
<p>You live in Belgium</p>
</if>
<else>
<p>You don't live in Belgium</p>
</else>
When I pass in a UserAccount with country set to 'BE' the first one prints correctly 'You live in Belgium', the second one produces the incorrect result.
Can someone tell me why? Can you test on equality of strings without using Equals()?

So the code you posted throws a compiler error when I try to run it. However, the error I get explains why you're seeing different behavior. The error I get his this:
CS0252: Warning as Error: Possible unintended reference comparison;
to get a value comparison, cast the left hand side to type 'string'
Looking at the code that Spark generates, the error and behavior you're seeing makes a lot more sense. First piece of code generates the following comparison:
if (Equals(Eval("context.UserAccount.Country"),"BE"))
I'm pretty sure Eval returns something of type object (regardless of what the actual type of the variable is). Then the call to Equals is probably equivalent to doing this:
Eval("context.UserAccount.Country").Equals("BE")
which then uses the overloaded equals method on the string class (thank you polymorphism) which would return true. Where as the second case:
if (Eval("context.UserAccount.Country") == "BE")
probably just does a reference comparison between two objects, which returns false.
If you don't use the # before context.UserAccount.Country, Spark will generate the following code (notice the lack of call to Eval):
if (context.UserAccount.Country == "BE")
Assuming context has a UserAccount property, that has a Country property of type string then the expression should correctly evaluate to true when Country has the value of "BE".

Related

My flow fails for no reason: Invalid Template Language? This is what I do

Team,
Occasionally my flow fails and its enough test it manually to running again. However, I want to avoid that this error ocurrs again to stay in calm.
The error that appears is this:
Unable to process template language expressions in action 'Periodo' inputs at line '0' and column '0': 'The template language function 'split' expects its first parameter to be of type string. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#split for usage details.'.
And it appears in 2 of the 4 variables that I create:
Client and Periodo
The variable Clientlooks this:
The same scenario to "Periodo".
The variables are build in the same way:
His formula:
trim(first(split(first(skip(split(outputs('Compos'),'client = '),1)),'indicator')))
His formula:
trim(first(split(first(skip(split(outputs('Compos'),'period = '),1)),'DATA_REPORT_DELIVERY')))
The same scenario to the 4 variables. 4 of them strings (numbers).
Also I attached email example where I extract the info:
CO NIV ICE REFRESCOS DE SOYA has finished successfully.CO NIV ICE REFRESCOS DE SOYA
User
binary.struggle#mail.com
Parameters
output = 7
country = 170
period = 202204012
DATA_REPORT_DELIVERY = NO
read_persistance = YES
write_persistance = YES
client = 18277
indicator_group = SALES
Could you give some help? I reach some attepmpts succeded but it fails for no apparent reason:
Thank you.
I'm not sure if you're interested but I'd do it a slightly different way. It's a little more verbose but it will work and it makes your expressions a lot simpler.
I've just taken two of your desired outputs and provided a solution for those, one being client and the other being country. You can apply the other two as need be given it's the same pattern.
If I take client for example, this is the concept.
Initialize Data
This is your string that you provided in your question.
Initialize Split Lines
This will split up your string for each new line. The expression for this step is ...
split(variables('Data'), '\n')
However, you can't just enter that expression into the editor, you need to do it and then edit in in code view and change it from \\n to \n.
Filter For 'client'
This will filter the array created from the split line step and find the item that contains the word client.
`contains(item(), 'client')`
On the other parallel branches, you'd change out the word to whatever you're searching for, e.g. country.
This should give us a single item array with a string.
Initialize 'client'
Finally, we want to extract the value on the right hand side of the equals sign. The expression for this is ...
trim(split(body('Filter_For_''client''')[0], '=')[1])
Again, just change out the body name for the other action in each case.
I need to put body('Filter_For_''client''')[0] and specify the first item in an array because the filter step returns an array. We're going to assume the length is always 1.
Result
You can see from all of that, you have the value as need be. Like I said, it's a little more verbose but (I think) easier to follow and troubleshoot if something goes wrong.

Visualforce Syntax Error for IF statement inside a Apex Repeat

I am receiving a syntax error and having a hard time identifying where I am going wrong.
I have researched several different alternatives such as wrapping <apex outputText> as well as using multiple version of {! leading into my variable calls. Unfortunately, I'm just having a hard time grasping which series of errors I am making to cause this syntax error.
The Crux of the code is:
<td>
{! IF(isTenant, ${woli.Repair_Product__r.Bill_Rate_Retail__c}, ${woli.Repair_Product__r.Bill_Rate__c})}
</td>
For variable References. This is located in a where woli is defined as:
<apex:repeat value="{!woliList}" var="woli">
and isTenant is simply hardcoded in my controller to be false for testing purposes:
public Boolean isTenant = False;
I expected my Apex Repeat command to populate different bill rates depending on the value of isTenant, but of course with the Syntax error I cannot tell if I am on the right track. Helpful tips on when multiple {!} are or are not required in Visual Force references would be helpful. I am also curious when, if ever, I should be using single or double quotation marks to define the output of the if condition.
UPDATE:
I thought perhaps I it was as simple as me not binding the condition statement to a variable in the controller class so I modified to this:
<td>
{! IF({!isTenant}, ${woli.Repair_Product__r.Bill_Rate_Retail__c}, ${woli.Repair_Product__r.Bill_Rate__c})}
</td>
Adding appropriate get/set in the controller. But alas this was not the problem the developer console still gives a mystery Syntax Error.
You don't need $ signs. Dollar is used for special fields that "depends who's looking" and other stuff not really related to data in database. You can have $CurrentPage, $User.Name, $Profile.Name, $Label.someText (that one counts as "depends who's looking" because if your preferred language is French it can display translated version.
And second thing - if you're already inside the {! some merge field syntax } you don't need more curly braces.
Try
{! IF(!isTenant,
woli.Repair_Product__r.Bill_Rate_Retail__c,
woli.Repair_Product__r.Bill_Rate__c
)}

Formatting Dates in Freemarker Where Date Type is Unknown

I have a Freemarker function whose aim is to print any value passed to it and am having difficulty handling dates in particular.
I understand that when Freemarker cannot determine what portion of a date is in use, that it will error when attempting to print the value directly, and so some special-casing is required for dates, but I've been unable to find a reliable workaround of this feature.
My function looks something like this:
<#function format value=''>
<#if value?is_date>
<#-- code to attempt to handle all types of date -->
<#else>
<#-- handle non-date values -->
</#if>
</#function>
So far, I have tried the following:
First attempt: just always print date and time; e.g. value?datetime
Problem: bombs if the value has already 'been told' it's date-only (e.g. format(value?date) - a usage I want to support)
Second attempt: attempt to print raw value using attempt/recover directives to handle problem cases; e.g.
<#attempt>
<#return value>
<#recover>
<#return value?datetime>
</#attempt>
Problem: the attempt/recover directives don't successful catch the exception - instead it's propagated out as before
I've tried many other things but the above approaches were the more sensible, and unfortunately neither were successful. There seems to be a catch-22: if the date-type is unknown I can only print by choosing an arbitrary type to apply to all date values, but if I attempt to apply that type to a known-type date, it will fail where the types don't match.
Is there any way to determine whether the date type of a value is known before trying to print the value? If so, I could use the ?datetime built in only when necessary.
Ideally, I could tell Freemarker to just print the full date where it's unable to determine the exact type, instead of bombing - but I'm not sure this is currently possible.
Update: In FreeMarker 2.3.21 you can use <#if value?is_date_like>${value?datetime_if_unknown}<#else>...
Yeah, there should exist something like ?is_unknown_type_date, but it doesn't... I'm an FM maintainer so I will add that in 2.3.21 (but don't hold your breath until that's released). Meanwhile, you can write a TemplateMethodModelEx that does just that. Implementing it trivial as you will see, how to make them accessible to templates is a bit underdocumented... One way is just doping the TemplateMethodModelEx into the data-model or into the "shared variable" set of the Configuration. Another is putting this into some of your commonly #import-ed or #included template like <#assign isUnknownTypeDate='com.example.IsUnknownTypeDateMethod'?new()>.
BTW, #recover works for me for this (using a nightly 2.3.21, but I don't remember that it was ever broken). But don't use it for this anyway, as it will log the error. #recover is for emergency situations only, not for normal program flow.
As of providing a default format for unknowns-type dates... I feel uneasy about it as then these issues won't be caught during development, and very few will care to use a different FM configuration for production than for development.

Type mismatch error while reading lotus notes document in vb6

Am trying to read the lotus notes document using VB6.I can able to read the values of the but suddenly type mismatch error is throwed.When i reintialise the vb6 variable it works but stops after certain point.
ex; address field in lotus notes
lsaddress=ImsField(doc.address)
private function ImsField(pValue)
ImsField=pValue(0)
end function
Like this I am reading the remaining fields but at certain point the runtime error "13" type mismatch error throwed.
I have to manually reintialize by
set doc=view.getdocumentbykey(doclist)
The type mismatch error occurs for a certain field. The issue should be a data type incompatibility. Try to figure out which field causes the error.
Use GetItemValue() instead of short notation for accessing fields and don't use ImsField():
lsaddress=doc.GetItemValue("address")(0)
The type mismatch is occurring because you are encountering a case where pValue is not an array. That will occur when you attempt to reference a NotesItem that does not exist. I.e., doc.MissingItem.
You should not use the shorthand notation doc.itemName. It is convenient, but it leads to sloppy coding. You should use getItemValue as everyone else is suggesting, and also you should check to see if the NotesItem exists. I.e.,
if doc.hasItem("myItem") then
lsaddress=doc.getItemValue("myItem")(0)
end if
Notes and Domino are schema-less. There are no data integrity checks other than what you write yourself. You may think that the item always has to be there, but the truth is that there is nothing that will ever guarantee that, so it is always up to you to write your code so that it doesn't assume anything.
BTW: There are other checks that you might want to perform besides just whether or not the field exists. You might want to check the field's type as well, but to do that requires going one more level up the object chain and using getFirstItem instead of getItemValue, which I'm not going to get into here. And the reason, once again, is that Notes and Domino are schema-less. You might think that a given item must always be a text list, but all it takes is someone writing sloppy code in an one-time fix-it agent and you could end up having a document in which that item is numeric!
Checking your fields is actually a good reason (sometimes) to encapsulate your field access in a function, much like the way you have attempted to do. The reason I added "sometimes" above is that your code's behavior for a missing field isn't necessarily always going to be the same, but for cases where you just want to return a default value when the field doesn't exist you can use something like this:
lsaddress ImsField("address","")
private function ImsField(fieldName,defaultValue)
if doc.hasItem(fieldName) then
lsaddress=doc.getItemValue(fieldName)(0)
else
lsaddress=defaultValue
end if
end function
Type mismatch comes,
When you try to set values from one kind of datatype variable to different datatype of another variable.
Eg:-
dim x as String
Dim z as variant
z= Split("Test:XXX",":")
x=z
The above will through the error what you mentioned.
So check the below code...
lsaddress = ImsField(doc.address)
What is the datatype of lsaddress?
What is the return type of ImsField(doc.address)?
If the above function parameter is a string, then you should pass the parameter like (doc.address(0))

bpws:getVariableData() causes fault if no xpath match is found

I wanted to use "bpws:getVariableData()" to assign a value only if the xpath expression find a match. If not, nothing should happen. Unfortunately the bpel processing stops with a fault, if the xpath expression finds no match. Is there a way to achieve this behavior?
Thanks for your help.
I found that the oracle BPEL engine provides a feature to ignore missing from data. This Flag can be added to the copy element as follows:
<copy bpelx:ignoreMissingFromData="yes|no"/>
More info on how to set it in the JDeveloper: http://download.oracle.com/docs/cd/E17904_01/integration.1111/e10224/bp_manipdoc.htm#SOASE87087
This solves the problem with the fault message that is thrown. However it still does not show the wanted behavior. My intension was that no assignment is done, if the xpath expression cannot be evaluated. Using the bpelx:ignoreMissingFromData flag however assigns the empty string "" to the target.
In my use case I want to merge tow XML documents. I want to assign a new value to an element in document1 only if the element shows up in document2. If not, leave the element in document1 unchanged.
I solved the problem using a transformation instead of a BPEL assign. In the xsl I use the following statement. The transformation gets two XML documents a input. Document1 is referenced via the parameter $parameter_referenceDocument1.
<elementName>
<xsl:if test="xpathInDocument2">
<xsl:value-of select="xpathInDocument2"/>
</xsl:if>
<xsl:if test="not(xpathInDocument2)">
<xsl:value-of select="$parameter_referenceDocument1.xpathInDocument1"/>
</xsl:if>
</elementName>
I know its ugly, but solves the problem. If anyone has a better solution, please let me know.
No, the BPEL standard requires the engine to throw a selectionFailure in this case. To avoid such situations, make sure you have properly initialized variables and/or validate variable against a schema. Also you may guard an assign activity with an if/switch activity to check for the presence of the element before accessing it. You may also consider writing an custom XPath function that returns a default value in case the demanded element does not exist in the variable. However, I'm not sure if the Oracle BPEL engine supports that.
You can create a scope around the assign activity and using an exception handler on the scope catch the selectionFailure, the item which will then carry on processing.
In the exception handler you could then assign a default value if required.
To clarify Vanto's statement, the Oracle BPEL engine does support custom XPath functions which would allow you to do that.

Resources