marging case statement in salesforce with and statement - salesforce

Please help me to answer this question:
I created a new Formula field called "Car Rating" that will be calculated as follows:
1) In case "Color" field equals "Red" and "Category" field equals "mini", Car Rating value should be 1.
2). In case "Color" field equals “Blue" and "Category" field equals "manager", Car Rating value should be 2.
3). In case "Color" field equals "Yellow" and "Category" field equals "premium", Car Rating value should be 3.
this what I tried to do to answer this question, and it's not working the question if my answer is correct I need your help, please what should I do here
CASE(AND(ISPICKVAL(Color__c, "Red"),( ISPICKVAL( Car_Category__c , "mini")),1 ) CASE(AND(ISPICKVAL(Color__c, "blue"),( ISPICKVAL( Car_Category__c , "manager")),2 ) CASE(AND(ISPICKVAL(Color__c, "Yellow"),( ISPICKVAL( Car_Category__c , "premium")),3,0 )
Thanks, Liraz

You can do this with an nested IF statement:
IF ( AND( ISPICKVAL(Color__c, 'Red'), ISPICKVAL(Car_Category__c,'mini'),1,
IF(AND( ISPICKVAL(Color__c, 'blue'), ISPICKVAL(Car_Category__c,'manager'), 2,
IF(AND( ISPICKVAL(Color__c, 'Yellow'), ISPICKVAL(Car_Category__c,'premium'), 3, null)
)
)

Related

Reference a column in another sheet using column name (heading) in Google Sheets inside an ArrayFormula

I have an example sheet at https://docs.google.com/spreadsheets/d/1i96cjWWGwLQQd2enqQhLylFo31n5GyxbCTcTEPk2I2A/edit#gid=0.
I have two parts to my question.
Part 1
In the names sheet, in C1, I have this formula:
={
"Is Special";
ARRAYFORMULA(
IF(
A2:A <> "",
REGEXMATCH(B2:B, "b|d|e"),
)
)
}
The formula works as expected but I do not like the B2:B reference because it is not easily obvious what column I am checking against. I'd rather refer to the column by it's name (First Name in this case). Is that possible? Can I replace B2:B with some formula or function that lets me lookup the column range by searching for First Name? I know I can use MATCH to get the column number for First Name but I'm not sure how to put that into this formula -- especially since I cannot use INDRECT in an ARRAYFORMULA.
Something like:
={
"Is Special";
ARRAYFORMULA(
IF(
A2:A <> "",
REGEXMATCH([some formula or function that lets me lookup the column range by searching for "First Name"], "b|d|e"),
)
)
}
Part 2
Similar to above, on the names sheet, in D1 I have this formula:
={
"Count of one Properties";
ARRAYFORMULA(
IF(
A2:A <> "",
COUNTIFS(
properties!A:A, A2:A,
properties!B:B, "one"
),
)
)
}
The formula works as expected. However, I do not like the properties!B:B reference. Reading the formula it is not obvious what column from the properties sheet I am checking against. Is it possible to something similar like above where I get the column range by searching for the column name, in this case type?
if you dont like named ranges try:
={"Is Special"; ARRAYFORMULA(IF(A2:A<>"",
REGEXMATCH(FILTER({A2:B, D2:E}, {A1:B1, D1:E1}="First Name"), "b|d|e"), ))}
or like:
={"Count of one Properties"; ARRAYFORMULA(IF(A2:A<>"",
COUNTIFS(properties!A2:A, A2:A,
VLOOKUP(ROW(properties!A2:A),{ROW(properties!A2:A), properties!A2:Z},
MATCH("type", properties!1:1, 0)+1, 0), "one"), ))}
not sure if you are familiar with "named ranges":
={"Is Special"; ARRAYFORMULA(IF(A2:A<>"", REGEXMATCH(FirstName, "b|d|e"), ))}
update:
={"Is Special"; ARRAYFORMULA(IF(A2:A<>"",
REGEXMATCH(QUERY(FirstName, "offset 1", 0), "b|d|e"), ))}

Postgresql update jsonb keys recursively

Having the following datamodel:
create table test
(
id int primary key,
js jsonb
);
insert into test values (1, '{"id": "total", "price": 400, "breakdown": [{"id": "product1", "price": 400}] }');
insert into test values (2, '{"id": "total", "price": 1000, "breakdown": [{"id": "product1", "price": 400}, {"id": "product2", "price": 600}]}');
I need to update all the price keys to a new name cost.
It is easy to do that on the static field, using:
update test
set js = jsonb_set(js #- '{price}', '{cost}', js #> '{price}');
result:
1 {"id": "total", "cost": 1000, "breakdown": [{"id": "product1", "price": 400}]}
2 {"id": "total", "cost": 2000, "breakdown": [{"id": "product1", "price": 400}, {"id": "product2", "price": 600}]}
But I also need to do this inside the breakdown array.
How can I do this without knowing the number of items in the breakdown array?
In other words, how can I apply a function in place on every element from a jsonb array.
Thank you!
SOLUTION 1 : clean but heavy
First you create an aggregate function simlilar to jsonb_set :
CREATE OR REPLACE FUNCTION jsonb_set(x jsonb, y jsonb, _path text[], _key text, _val jsonb, create_missing boolean DEFAULT True)
RETURNS jsonb LANGUAGE sql IMMUTABLE AS
$$
SELECT jsonb_set(COALESCE(x, y), COALESCE(_path, '{}' :: text[]) || _key, COALESCE(_val, 'null' :: jsonb), create_missing) ;
$$ ;
DROP AGGREGATE IF EXISTS jsonb_set_agg (jsonb, text[], text, jsonb, boolean) CASCADE ;
CREATE AGGREGATE jsonb_set_agg (jsonb, text[], text, jsonb, boolean)
(
sfunc = jsonb_set
, stype = jsonb
) ;
Then, you call the aggregate function while iterating on the jsonb array elements :
WITH list AS (
SELECT id, jsonb_set_agg(js #- '{breakdown,' || ind || ',price}', '{breakdown,' || ind || ',cost}', js #> '{breakdown,' || ind || ',price}', true) AS js
FROM test
CROSS JOIN LATERAL generate_series(0, jsonb_array_length(js->'{breakdown}') - 1) AS ind
GROUP BY id)
UPDATE test AS t
SET js = jsonb_set(l.js #- '{price}', '{cost}', l.js #> '{price}')
FROM list AS l
WHERE t.id = l.id ;
SOLUTION 2 : quick and dirty
You simply convert jsonb to string and replace the substring 'price' by 'cost' :
UPDATE test
SET js = replace(js :: text, 'price', 'cost') :: jsonb
In the general case, this solution will replace the substring 'price' even in the jsonb string values and in the jsonb keys which include the substring 'price'. In order to reduce the risk, you can replace the substring '"price" :' by '"cost" :' but the risk still exists.
This query is sample and easy for change field:
You can see my query structure in: dbfiddle
update test u_t
set js = tmp.new_js
from (
select t.id,
(t.js || jsonb_build_object('cost', t.js ->> 'price')) - 'price'
||
jsonb_build_object('breakdown', jsonb_agg(
(b.value || jsonb_build_object('cost', b.value ->> 'price')) - 'price')) as new_js
from test t
cross join jsonb_array_elements(t.js -> 'breakdown') b
group by t.id) tmp
where u_t.id = tmp.id;
Another way to replace jsonb key in all jsonb objets into a jsonb array:
My query disaggregate the jsonb array. For each object, if price key exist, remove the price key from jsonb object, add the new cost key with the old price's value, then create a new jsonb array with the modified objects. Finally replace the old jsonb array with the new one.
WITH cte AS (SELECT id, jsonb_agg(CASE WHEN item ? 'price'
THEN jsonb_set(item - 'price', '{"cost"}', item -> 'price')
ELSE item END) AS cost_array
FROM test
CROSS JOIN jsonb_array_elements(js -> 'breakdown') WITH ORDINALITY arr(item, index)
GROUP BY id)
UPDATE test
SET js = jsonb_set(js, '{breakdown}', cte.cost_array, false)
FROM cte
WHERE cte.id = test.id;

Update/Add Array Value PostgreSQL

My table:
CREATE TABLE public.invoice (
id bigint NOT NULL,
json_data jsonb NOT NULL
);
My Data:
INSERT INTO public.invoice (id,json_data) VALUES
(1,'{"Id": 1, "Items": ["2", "3", "1"], "Invoice": "CR000012"}');
Todo List:
Need to add to "Items" a new value i.e "5". (output of items ["2", "3", "1","5"])
Need to update items value 2 to 9. (output of items ["9", "3", "1","5"])
I have tried below but this will replace the array values not update or add
UPDATE invoice SET json_data = jsonb_set(json_data, '{invoice }', '"4"') where Id ='1'
I recommend you to use this aproach, you should point element with index of array.
In your case, your code should look something like this,
1. Add to Items a new value i.e "5
UPDATE invoice SET json_data = jsonb_set(json_data, {Items,0}, json_data->'Items' || '"5"', TRUE) where Id =1
2. Update Items value 2 to 9.
UPDATE invoice SET json_data = jsonb_set(json_data, {Items,0}, '"9"') where Id =1
You can check PostgreSQL, JSON Functions and Operators from here.

Filter Condition in Business Object BO

I have a problem with a filter condition in BO.
Imagine that I have this database
ID | DESC
0 | None
1 | Company
2 | All
In BO I have a filter that ask where do you want to find the objects and 2 options:
"Company" or "All".
If I choose "All" then I should have all the datas with the "ID" 0,1,2 and if I choose "Company" only the data with the "ID" 1.
So I did something like this:
TABLE_NAME.ID <= (CASE WHEN #Prompt('where do you want to find the objects','A',{'Company', 'All'},mono,constrained,not_persistent,{'Company'}) = 'Company' THEN 1 ELSE 2 END)
This filter is OK when I choose "All" because I have all the "ID" smaller than 2, i.e, 0,1,2.
But It does not work when my option is company, because it also shows the data with the "ID" 0.
I should have some with "=" combined with "<="
If it's really only that simple, the following will work:
TABLE_NAME.ID =
(CASE #Prompt('where do you want to find the objects',
'A',
{'Company', 'All'},
mono,
constrained,
not_persistent,{'Company'}
)
WHEN 'Company'
THEN 1
WHEN 'All'
THEN TABLE_NAME.ID
END)

Solr demote all documents with condition

I want to demote all documents that have inv=0(possible values from 0 to 1000) to the end of the result set. i have got other sorting options like name desc also as part of the query.
For example below are my solr documents
Doc1 : name=apple , Inv=2
Doc2 : name=ball , Inv=1
Doc3 : name=cat , Inv=0
Doc4 : name=dog , Inv=0
Doc5 : name=fish , Inv=4
Doc6 : name=Goat , Inv=5
I want achieve below sorting ...here, i want to push all documents with inv=0 down to bottom and then apply "name asc" sorting.
Doc1
Doc2
Doc5
Doc6
Doc3
Doc4
my solr request is like
bq: "(: AND -inv:"0")^999.0" & defType: "edismax"
here 999 is the rank that i gave to demote results.
this boosting query works fine. it moves all documents with inv=0 down to the bottom.
But when i add &sort=name asc to the solr query, it prioritizes "sort" over bq..i am seeing below results with "name asc".
Doc1 : name=apple , Inv=2
Doc2 : name=ball , Inv=1
Doc3 : name=cat , Inv=0
Doc4 : name=dog , Inv=0
Doc5 : name=fish , Inv=4
Doc6 : name=Goat , Inv=5
can anyone please help me out. ?
The easy solution: You can just sort by inv first, then the other values. This requires that inv only have true (1) or false (0) values. I guess that is not the case, so:
You can sort by a function query - and you can use the function if to return different values based on whether the value is set or not:
sort=if(inv, 1, 0) desc, name desc
If Solr fails to resolve inv by itself, you can use field(inv), but it shouldn't be necessary.
Another option is to use the function min to get either 1 or 0 for the sort field, depending on whether it's in inventory or not:
sort=min(1, inv)

Resources