set integer value to specific stagenames - salesforce

How, in SOQL, would I assign a 0 or 1 integer value to specific string values.
Something like this, but taken out of Java pseudo-code and turned into SOQL:
if (stageName.equals(review) || stageName.equals(declined) {
return 0;
} else if (!stageName.equals(review) || !stageName.equals(declined) {
return 1;
}

If you exactly know that integer value can be only 0 or 1, why you don't return and assign string representation of these values - '0' and '1'? If you don't know exact value of integer variable you always can use special function
String.valueof(integer_variable_name);
And if you want to use this assignment in SOQL query, for instaince in WHERE condition, you can use colon symbol. For example:
String review= 'review';
String declined= 'declined';
String stageName = 'declined';
List<Opportunity> opps = [SELECT Id, StageName FROM Opportunity WHERE StageName= :(stageName.equals(review) || stageName.equals(declined) ? '0':'1')];

Related

how to compare the query value from url to id using react?

i want to check if the url contains ?query_param if so then get its value and compare that value to an id.
consider the url /path/20?query_parm=2234
and i have to get the param_id and compare it with the item id.
so i do something like below,
handle_location = (path) => {
let opened_item, param_id;
param_id = new
URLSearchParams(this.props.location.search).get('query_param');
if (this.state.items) {
opened_item = this.state.items.find(item => item.id ===
param_id);
}
};
the data structure for items is below,
items = [{
id: 2244;
attributes: something;
}
{
id: 33;
attributes: nothing;
}]
But this gives the opened_item value undefined since item.id is never equal to param_id... because of type being different.
How can i fix this...or is there a better way to find the query_param from url and get its value and use it accordingly to find the item that matches with the query_param value.
Given you understand that both data types are different, you could use avoid using strict equality and leverage type coercion which would work
item.id == param_id
The most efficient way though would be to convert param_id to the appropriate type before comparing e.g.
param_id = parseInt(param_id, 10);
It means one conversion and you can keep the strict equality
You will need to either cast both of the values to the same type(either Number or String) and then perform the comparison or you could use == operator which will try to coerce the types automatically(not recommended). You can also always fall back to some default value if none of the items matched the id.
if (this.state.items) {
opened_item = this.state.items.find(item => item.id ===
param_id) || 'some default value'
}
try this:
const param_id = this.props.match.params.id

Grouping in store with values in extjs 6.2

I am trying to group my store on department name . Department name contains some null values also . when i am trying grouping along with sort function its result in multiple group from same name.
see this fiddel for details. I am not getting what i am doing wrong. Kindly advise.
Your sorterFn is wrong.
The sorterFn has to return three different values:
1 if the second argument is strictly greater than the first.
-1 if the second argument is strictly smaller than the first.
0 if both arguments are the same group.
Your sorterFn never returns 0. Try this one:
sorterFn: function(a, b) {
if(a.get('department')=="Management" && b.get('department')=="Management") return 0;
if(a.get('department')=="Management") return 1;
if(b.get('department')=="Management") return -1;
if(a.get('department') < b.get('department')) return 1;
if(a.get('department') > b.get('department')) return -1;
return 0;
},
Furthermore, your transform function is useless. It is called only from the original sorterFn, which you overwrite. You would have to account for null values in your sorterFn, if you wish so. (However, usually one would put fallback categories like "Others" in the end, not between "IT" and "Sales".)
Also, to write the department in the header line, you have to override the groupHeaderTpl template, e.g.
groupHeaderTpl: [
'<tpl if=\'name\'>{name}<tpl else>Others</tpl>'
]

How to access to column that is returned SOQL relationship query?

Following code is not working.
The error message is "Compile Error: Initial term of field expression must be a concrete SObject: LIST at line 8 column 16" (return line is line 8)
public String getX(){
List<Oppoinvoice__c> o = [SELECT Opportunity__r.Account.Name FROM Oppoinvoice__c];
return o.Opportunity__r.Account.Name;
}
You must specify what entry number of the list must be shown. With your query you will get back a list. And your method must return a string.
public String getX(){
List<Oppoinvoice__c> o = [SELECT Opportunity__r.Account.Name FROM Oppoinvoice__c];
return o[0].Opportunity__r.Account.Name;
}
Or like this:
public String getX(){
String o = [SELECT Opportunity__r.Account.Name FROM Oppoinvoice__c Where Id = 'XXXXXXX'].Opportunity__r.Account.Name;
return o;
}

Build dynamic Expression for search

I'm in trouble, can't figure out seems a very simple thing that in plain SQL can be done within 1 minute, it's been several hours so far. Here is the situation:
I have single field where user may enter as many words as he/she likes
I need to build Expression to find match
Let's say there are 3 fields in database: firstname, middlename, lastname
I need to split the search entry and compare against those 3 fields
I'm dealing with Silverlight RIA, EF
Once again search entry contains UNKNOWN number of words
Here under what I'm trying to accomplish, return type is mandatory:
public Expression<Func<MyEntity, bool>> GetSearchExpression(string text)
{
Expression<Func<MyEntity, bool>> result;
var keywords = text.Trim().Split(" ");
foreach(var keyword in keywords)
{
// TODO:
// check whether 'OR' is required (i.e. after second loop)
// (firstname = 'keyword'
// AND
// middlename = 'keyword'
// AND
// lastname = 'keyword')
// OR
// (firstname like '%keyword%'
// AND
// middlename like '%keyword%'
// AND
// lastname like '%keyword%')
}
return result;
}
Thanks in advance!
The simplest thing would be to use Joe Albahari's PredicateBuilder to do something like this:
var predicate = PredicateBuilder.False<MyEntity>();
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or (
p => p.FirstName.Contains (temp) &&
p.LastName.Contains (temp) &&
p.MiddleName.Contains (temp));
}
return predicate;
I left out the equality-checks because "Contains" (i.e. like '%...%') will cover that possibility anyway.
I do have to point out, though, that your conditions don't make any sense, from a business logic standpoint. Under what circumstances do you want to find someone whose first, last, and middle name all contain "John"? I suspect what you really want is something more like this:
var predicate = PredicateBuilder.True<MyEntity>();
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.And (
p => p.FirstName.Contains (temp) ||
p.LastName.Contains (temp) ||
p.MiddleName.Contains (temp));
}
return predicate;
One final note: Because PredicateBuilder requires you to call .AsExpandable() when you are creating your query, I don't know whether this will work for you. You might have to resort to building your own expressions, which can be somewhat tedious. This can get you started, though:
var pParam = Expression.Parameter(typeof(MyEntity), "p");
var predicate = Expression.Constant(true);
foreach (string keyword in keywords)
{
var keywordExpr = Expression.Constant(keyword);
// TODO: create an expression to invoke .FirstName getter
// TODO: create an expression to invoke string.Contains() method
//TODO: do the same for lastname and middlename
predicate = Expression.And(predicate,
Expression.Or(
Expression.Or(firstNameContainsKeyword,
middleNameContainsKeyword),
lastNameContainsKeyword));
}
return Expression.Lambda<Func<MyEntity, bool>>(predicate, pParam);

JDO querys with OR in GAE

Got an entity A with like this
public class A
private String a;
private String b;
..
..
..
How do I write a JDO query that select all the A objects that matches either keyword==a OR keyword ==b
Full code looks something like this:
Query q = pm.newQuery(SELECT FROM A WHERE a == keyword || b == keyword ORDER BY date DESC");
List<A> results = (List<A>)q.execute(keyword);
This code does not give any error but does not give any results. Removing the || part gives results.
Thanks
You can't do an OR on two different fields. Here is a snippet from the docs:
In the JDOQL string syntax, you can
separate multiple filters with ||
(logical "or") and && (logical "and"),
although keep in mind that || can only
be employed when the filters it
separates all have the same field
name. In other words, || is only legal
in situations where the filters it
separates can be combined into a
single contains() filter:
// legal, all filters separated by || are on the same field
Query query = pm.newQuery(Employee.class,
"(lastName == 'Smith' || lastName == 'Jones')" +
" && firstName == 'Harold'");
// not legal, filters separated by || are on different fields
Query query = pm.newQuery(Employee.class,
"lastName == 'Smith' || firstName == 'Harold'");
One way around this might be to store your a and b in a list. If you have a list of items called foo, you could do a query where foo = keyword, and if any item in foo matches, you will get that object back in your results. You don't say much about what a and b are, so I don't know if this will work for you or not :)
Update:
public class Example {
String firstName;
String lastName;
List<String> allNames;
public Example(String first, String last){
firstName = first;
lastName = last;
allNames = new ArrayList<String>();
allNames.add(first);
allNames.add(last);
}
}
With something like that, you could then do a query where "allNames == 'Smith' || allNames=='Jones'".

Resources