Null value passed as string "null" from listBox with no selection - google-apps-script

I am checking that a user makes a selection from a listbox. It appears that if no selection is made, the value passed to the server handler is "null" NOT null. Is this working as intended?
This code correctly tests for no selection in selectList listBox.
Logger.log("e.parameter.selectList"+e.parameter.selectList)
if (e.parameter.selectList === "null") {
error = true;
errortext = "no valid region selected";
Logger.log('error')
}
I expected
if (!e.parameter.selectList )... to work

Yes, this is known.
All the values in the handler are returned as string (except the dateBox value, I am not sure though). E.g checkbox value is returned "true"/"false" not true/false (a boolean value). You will have to parse these strings first to get the values in the desired form.
I usually use Utilities.jsonParse(stringValue)

Related

Index was outside the bounds of the array in SSRS

I have two parameters , let's say P1 and P2. The sample expression I used for P2 is
IIF(P1.Label="string", "null" ,Split(P1.Label," ").GetValue(0))
When the condition is false, the split expression is working fine. But if the condition is true, I'm getting 'Index was outside the bounds of the array' error. If the condition is true, I need to pass the value "null" as varchar type.
Can someone please advice on this?
The problem with the IIF function is that it is a function not a language construct. This means that it evaluates both parameters before passing the parameters to the function. Consequently, if you are trying to do a Split on a parameter that can't be split, you will still get the 'Index was outside the bounds of the array' error, even when it looks like that code shouldn't be executed due to boolean condition of the IIF statement.
The best way to solve this is to create a safe string splitter function in custom code where you can use real language constructs. Also, check that the string is splittable by checking it contains a space instead of checking for a special string:
Public Function SafeSplit(ByVal SplitThis As String) As String
If InStr(SplitThis, " ") Then
Return Split(SplitThis, " ")(0)
End If
Return "null"
End Function
and then use this in your report for the Value expression instead of IIF:
=Code.SafeSplit(Parameters!P1.Label)

Octave: How to correctly make string as a condition?

I was trying the if test for the first time, well actually so does with function.
Here's the script:
function trial()
I = input("f INPUT > Manually input frequency value? (yes/no):");
if I = "yes";
f = input("Please input the frequency value : \n")
elseif I = "no";
f = randi([100 1000],5,5)
endif
O = 2*pi*f;
fprintf("%.2f \n",O);
plot(f,O);
xlabel("Frequency");
ylabel("Angular Frequency");
end
f INPUT > Manually input frequency value? (yes/no):"no"
Please input the frequency value :
There are 2 things that I don't understand:
Why do I have to write the condition with quotation marks? (i.e "yes" not yes or "no" not no).
Why the 'no' condition ran the input command when it should've been randi?
Can someone show me how it should be done?
I = "yes" assigns the string "yes" to the variable I. To make a comparison, use == like this: I == "yes". But this comparison will only work if the two strings are the same length, and will return an array, not a single equality value. Use strcmp to compare strings: if strcmp(I, "yes").
The input function parses what the user types, so that typing 3 will result in the number 3, not the string "3". If you add a second input argument like this: input("prompt","s") then it will not parse the input, and return a string. The user will then be able to type no. See the docs.

Bind boolean to a label based on user's selection

I am designing a page in which informs a manager whether an applicant requires a work permit. For this I have created a data table called "Countries" that contains a string field "countries" and a boolean "EU_member". If the country is a member of the European Union the boolean is true and vice-versa.
I have set up a dropdown field in the page from which the manager can select the country. I now want to bind a label to the boolean field, so that it shows whether the country is part of the European Union or not. Right now, it is only bound to the country field.
#datasources.Countries.query.filters.country._equals=Dropdown1.value;
My second idea was to include a server script that is triggered by the onValueEdit method of the dropdown field
function checkWorkPermit(widgetValue) {
var query = app.models.Countries.newQuery();
query.filters.country._equals = widgetValue;
var records = query.run();
return records[0].EU_Member;
}
but the console returns
checkWorkPermit is not defined at ....Panel8.Dropdown1.onValueEdit:1:1
Considering, that the dropdown's options property is bound to smth like this:
#datasources.Countries.items
binding for the label could look similar to this:
#pages.MyPage.descendants.CountriesDropDown.value.EU_member ?
'Work permit is required' :
'Work permit is NOT required'

How to display number values with commas in form

In my Access query, I have the query using a VBA function to figure the value that goes in the query field.
In the form, if the stringval textbox has a value, then I want to compute it, but if not, it should remain empty (null).
Function GetValue(stringval, numval)
Dim result
stringval= stringval & ""
result= IIf(stringval<> "", numval* 1.5, Null)
GetValue = Int(result)
End Function
Now, I have a form that uses this query, and on the form is a textbox that displays the query value. I want the value to be formatted with commas in the numbers for easy reading. Everything I've tried so far does not show any commas.
I've tried:
used Standard for the Format > Formatfor the textbox (in properties)
putting #,###.### in the textbox Format value
putting #,##0.0## in the textbox Format value
changing Data > Text Format but it only gives me Plain Text and Rich Text - no option for numbers.
returning a double from the function
Note: if I don't use a custom VBA function, and write the formula directly into the query, then it does display commas. But when I move the formula into the function then the commas are lost.
What do I do?
[update]
I tried Gustav's solutions and since they didn't work for me, I added those as items to my "what I've tried" list above.
Also, if I look at the query in datasheet view, the number values sort alphabetically instead of by the size of the value. When I used the forumulae directly in the query instead of using functions, it sorted by the value of the number. I hope this is a clue.
Numbers carries no format. A format is applied when displayed only.
But be sure to return a Double if not Null:
Function GetValue(stringval, numval)
Dim result
If stringval & "" <> "" Then
result = Int(CDbl(numval) * 1.5)
Else
result = Null
End If
GetValue = result
End Function
Then apply your Format to the textbox
#,##0.0##
Or force a formatted string to be returned:
If stringval & "" <> "" Then
result = Format(Int(CDbl(numval) * 1.5), "#,##0.0##")
Else
result = Null
End If
and skip formatting of the textbox.
The solution is this: the function has to be declared as a double.
That allows the query's datasheet view to know it is displaying numbers - and so you can set the field's format to Standard for the comma to display. This also allows the form to know it has a number and it will display the comma there, too. I knew it had to do with double, but didn't realize before that the function needed to be declared as such.
Function GetValue(stringval, numval) as double '<----THIS!!!!
Dim result
If stringval & "" <> "" Then
result = numval * 1.5
Else
result = 0 `<--can't return null here; use nz function in control source for textbox
End If
GetValue = int(result) 'to remove decimals
End Function
The problem I was having was in some of my functions I need to return double or null, because I wanted textboxes to remain blank if they contained no data. Now, at least I know how to make the numbers generated by functions to display commas.
And here is how to deal with the fact that you can't return null as the value of a double. The function is originally from here.
Put this function in a module so it is public, and then in the control source for the textbox, instead of just putting the field value, put Zn(fieldvalue). This works like a charm (although using functions in the control source seems to have a delay on the form display). This way you can keep the underlying value as a double and still get commas to display in both the form and the query whilst keeping the field blank if necessary.
Public Function Zn(pvar)
' Return null if input is zero or ""
If IsNull(pvar) Then
Zn = Null
ElseIf IsNumeric(pvar) Then
If pvar = 0 Then
Zn = Null
Else
Zn = pvar
End If
Else
If Len(pvar) = 0 Then
Zn = Null
Else
Zn = pvar
End If
End If
End Function

Error "Cannot Implicitly Convert Type" when accessing Publication Metadata Category

I had a similar question before, but this is a few steps beyond that so here we go:
I am trying to access values in a category in Tridion 2011. I am using the Razor TBB and using this code to do it:
#foreach (var keyword in Publication.MetaData.myCategory) {
#: Hello World!
}
I have set up a metadata schema with a field that has an xml name of "myCategory" attached to my publication. If I run this on the publication where myCategory is a Text field, this code works... kind of. It treats EACH character as a separate value of the keyword variable... so if I enter the text "one", what prints is "Hello World! Hello World! Hello World!", and if I just have "o" as the value, it prints "Hello World!".
Bizarre as that is (and I'd like to know why on that too), what I really want the field to be a "Values will be selected from a list" type of field, pointing to my category in Categories and keywords. When I do this, and the value of myCategory changes to the value of the item selected in the dropdown for this type of field instead of direct text entry, the code no longer works and gives this error:
Cannot implicitly convert type 'Tridion.Extensions.Mediators.Razor.Models.KeywordModel' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
Please help me.
If you get your output repeated for every character in a string, you are clearly iterating over the characters in the string and not the other range of values that you expected.
I quickly checked the relevant code of the Razor mediator and its ToString method indeed returns the Title of the underlying RepositoryLocalObject.
http://code.google.com/p/razor-mediator-4-tridion/source/browse/trunk/Tridion.Extensions.Mediators.RazorMediator/Tridion.Extensions.Mediators.RazorMediator/Models/AbstractRepositoryLocalObject.cs
http://code.google.com/p/razor-mediator-4-tridion/source/browse/trunk/Tridion.Extensions.Mediators.RazorMediator/Tridion.Extensions.Mediators.RazorMediator/Models/KeywordModel.cs
http://code.google.com/p/razor-mediator-4-tridion/source/browse/trunk/Tridion.Extensions.Mediators.RazorMediator/Tridion.Extensions.Mediators.RazorMediator/Models/DynamicItemFields.cs
The code that handles KeywordFields in in the DynamicItemsFields.cs file:
else if (itemField is KeywordField)
{
KeywordField keywordField = (KeywordField)itemField;
if (keywordField.Definition.MaxOccurs == 1)
if (keywordField.Value == null)
_dictionary[key] = null;
else
_dictionary[key] = new KeywordModel(_engine, keywordField.Value);
else
{
List<KeywordModel> keywords = new List<KeywordModel>();
int i = 0;
foreach (Keyword k in keywordField.Values)
{
var kw = new KeywordModel(_engine, k);
kw.Index = i++;
kw.IsLast = Index == keywordField.Values.Count - 1;
keywords.Add(kw);
}
_dictionary[key] = keywords;
}
}
So it looks like the myCategory property will either be a KeywordModel object (if the KeywordField is single-value) or a List<KeywordModel> (if the KeywordField is marked as multi-value in the Schema). Is your myCategory field single value? Or multi-value?
If it is single-value, what type of output were your expecting? If you were expecting the list of allowed values (instead of the currently selected value), check if you can access it through myCategory.Definition somehow (which should be a regular TOM.NET KeywordFieldDefinition object).