How to use Nested Ternary Operator in Apex - salesforce

String greeting ='';
Integer Hour = 10;
greeting = ((Hour<12) ?'Good Morning':'Good Afternoon') : ((Hour>12) ?'Good Morning':'Good Afternoon');
system.debug('Ternary Operator-->'+greeting);
I want to display Good Morning, if Hour is less than 12,
I want to display Good Afternoon, if Hour is greater than 12,
But I want to use in single nested ternary operator in the apex but getting an error.

The ternary operator is just a condensed if. You don't need two of them here, since you have only one condition. Just do:
greeting = (Hour <= 12) ? 'Good Morning' : 'Good Afternoon';

Related

What is wrong with this expressions? =CountRows(ReportItems!Textbox58.Value = "Intervene"). I want to count each row which says Intervene

What is wrong with this expression? =CountRows(ReportItems!Textbox58.Value = "Intervene")
I want to count each row which says Intervene.
As Larnu has commented, you cannot use CountRows against the ReportItems collection.
Probably what you need to do is
Look at the expression in Textbox58 and see where it gets it's data from. In this exmaple let's say it comes from Fields!myFieldName.Value.
Now we need to count the rows where Fields!myFieldName.Value = "Intervene" but rather than using count, we can convert these matches to return 1 or 0 where the field is not "Intervene"
So the expression would look something like this
=SUM(IIF(Fields!myFieldName.Value = "Intervene", 1, 0))
This will sum the rows withing the current scope, so if this is contained in a row group for example, then it will only sum those rows in that row group.
If you need to count based on a a different scope (e.g. the entire dataset) then you can specify that in the SUM() function like this
=SUM(IIF(Fields!myFieldName.Value = "Intervene", 1, 0), "DataSet1")
Here we are summing across the entire dataset where the dataset name is DataSet1
Update based on OP comment
As your expression is
=SUM(IIF(Fields!Actual_Duration.Value >= 10, "Intervene", "No Intervention Needed"))
What we actually need to count is instances where Actual_Duration is >= 10.
So the final expression should be
=SUM(IIF(Fields!Actual_Duration.Value >= 10, 1, 0))

Why is my code returning all records when I am trying to subset inside for loop - R?

for (i in 1:length(data$name)){
if (!is.na(data$years[i]) >= 34 & !is.na(data$gender[i]) == "male" & !is.na(data$classification[i]) == "mid"){
print(data$name)
}
}
There's a few problems in your code. I am assuming this is a class exercise or similar, so I'll add a bit extra detail to illustrate where you've missed a step.
First of all you loop works fine, but your if condition is not completely correct.
!is.na(data$years[i]) >= 34
All of your conditions look (somewhat) like this. The idea is obvious, you want to "check that data$years[i] is not null, and above 34". But in R (and most languages) you have to check these seperately.
!is.na(data$years[i]) && data$years[i] >= 34
Similar for the rest of your conditions.
Next your print statement is printing out everything, because this is what you're asking it to:
print(data$name)
is "ignorant" of anything else you've done up till now. It seems you want to print the specific record, eg:
print(data$name[i])
And this is the way to go about it.
Now R has a thing called "vectorization", so we could wrap this entire loop up in one go:
data$name[!is.na(data$years) & !is.na(data$gender) & !is.na(data$classification) & data$year > 34 & data$gender == "male" & data$classification == "mid"]
But I am assuming that is not part of your current exercise. Note the slight (but important) difference that for vectorized (eg. more than 1) condition I use single & but for single conditions I use 2 &&. The latter is optimized to be "lazy" for single inputs (thus faster).
Perhaps you can try subset + complete.cases like below
subset(
data,
years >= 34 & gender == "male" & classification == "mid" & complete.cases(data[c("years", "gender", "classification")])
)$name

SSRS: IIF statement to change fill color erroring out

In every example I could find to facilitate this, the logic is precisely what I'm using, but I can't get it to do what I need it to. It appears that I definitely can't use an AND condition inside the IIF expression, but even trying to account for that I don't get the correct results.
Attempt #1:
=iif(Fields!Days_to_Bill.Value >= 5, "Yellow", iif(Fields!Days_to_Bill.Value >= 10, "Red", "Transparent"))
This results in all numbers, including 10 and greater, being Yellow.
Attempt 2:
=iif(Fields!Days_to_Bill.Value >= 5 and < 10, "Yellow", iif(Fields!Days_to_Bill.Value >= 10, "Red", "Transparent"))
This results in the following error:
I really thought this was going to be a super simple expression, but I must be missing something.
For starters, your second IIF() needs to repeat Fields!Days_to_Bill.Value for each condition you're checking.
So you need =iif(Fields!Days_to_Bill.Value >= 5 and Fields!Days_to_Bill.Value < 10...
Second, the first equation is correct and you are getting expected results. In an IIF() statement it will stop at the first 'True' value. Everything in your result set is >= 5, therefore they will all be yellow.
You can avoid the and if you simply change the order of your expression. Simply check for values that are >= 10 first. Also I made a guess at the desired highlighting logic, as I suspect you made some mistakes in your example.
=iif(Fields!Days_to_Bill.Value >= 10, "Transparent", iif(Fields!Days_to_Bill.Value >= 5 "Yellow", "Red"))

Solr FunctionQuery: gte, lte, eq functions

Solr FunctionQuery has a DIV(x,y) function. I have such a need if y=0, then y should be equal to x.
In other words, I need to represent the following logic with FunctionQuery:
if y == 0, return 1 /* i.e. DIV(x,x) */
else, return DIV(x,y)
Somehow, from the Solr doc, I cannot find any comparison function, e.g. EQ(x, value), etc. for me to use.
Will anyone be able to give me a hint to construct my desired logic using FunctionQuery?
Thanks!
To clean up this question and log what is my final solution, thanks to Srikanth Venugopalan comment:
actually you need to switch the arguments. exposure_count = 0 is interpreted as false. So your condition would be {!boost b=if(exposure_count,div(1,exposure_count),1)}"
As it seems, Lucid works documentation does have a mistake. The FunctionQuery parser does not take comparison operators such as ==, at least this is what I found by looking into the sourcecode. Also, the field separator for IF() function should be ,(comma) and not ;(semi-colon).
The official Solr wiki is correct.
For string terms this works:
if(termfreq(fieldname,"value"),2,1)
which yields 2 if "value" is contained in "fieldname" (termfreq will be >0)
you can use == for equals and if conditional statement as below :-
e.g. if(y == 0; 1; DIV(x,y))
Check for the example # Documentation
if(color=="red"; 100; if(color=="green"; 50; 25)) :
This function checks the document field "color", and if it is "red" returns 100, if it is "green" returns 50, else returns 25.
Edit :-
Solr if wiki mentions , (comma) as seperator
e.g. if(exists(myField),100,0)

SSRS: How to count true rows

I have a report with a dataset that has a column with booleans. In the table footer I want to display x / y where x is how many rows that were true and y is how many rows there was total.
Currently I have this:
=Count(Fields!Transfered.Value).ToString() + " / " + CountRows().ToString()
But the first becomes same as the last part. I then tried this:
=Sum(Fields!Transfered.Value).ToString() + " / " + CountRows().ToString()
But that generates an error, which I guess I can understand. I thought that if I converted the booleans into numbers where true is 1 and false is 0, then it could work out nicely. But how can I do that? Or is it a smarter way to do this all together?
Update: Have now also tried
=Sum(CInt(Fields!Transfered.Value)).ToString() + " / " + CountRows().ToString()
And got a negative result... O.o
Also found a way that worked, which I posted as an answer. But I won't accept it as an answer yet incase someone has a better way to do this =)
I can tell you why things went wrong...
Count(Fields!Transfered.Value) is simply the number of rows. aka CountRows()
Sum(Fields!Transfered.Value) is trying to aggregate "true" or "false" = error
Sum(CInt(Fields!Transfered.Value)) will sum -1 and 0 because VB.NET true = -1
Sum(IIF(Fields!Transfered.Value, 1, 0)) fixes the sign issue = your solution
To avoid the extra IIF, you could use negate the sum of all the -1s
= -Sum(CInt(Fields!Transfered.Value)).ToString() + " / " + CountRows().ToString()
In the end, either solution would be OK and both are equally kludgy
Figured out a way to do it, although there is probably a better more clear and logical way...
=Sum(IIF(Fields!Transfered.Value, 1, 0)).ToString() + " / " + CountRows().ToString()
I came across this today and at least verified that I wasn't the only one that got a negative sum value for bools out of SSRS. Thanks.
My solution was to use ABS for absolute value. I like that better than just negating the expression solely because it's easier to see visually where the '-' might be missed if you're not careful in reading the expression.
but it's essentially the exact same thing
Here is a way where you count on a grouped value
=CountDistinct(IIF(Fields!TrueOrFalseField.Value,
Fields!GroupedField.Value,
"BadValueToBeRemovedFromCount"))
-IIF(CountDistinct(Fields!TrueOrFalseField.Value)=1
and First(Fields!TrueOrFalseField.Value)
, 0, 1)
The second part
-IIF(CountDistinct(Fields!TrueOrFalseField.Value)=1
and First(Fields!TrueOrFalseField.Value)
, 0, 1)
remove 1 from the count if it has True and False value or if there are only one distinct false value.

Resources