You need to sign in to do that
Don't have an account?
Kiran Pandiyan
Is it a right usage of ternary operator ?
I'm trying to remove as many if loops as possible to improve code efficiency , so I'm planning to change the below code
If no what is wrong here . Explain me elaborately
if(!mapProduct.containsKey(aobj.bexample__c)){ mapProduct.put(aobj.bexample__c), new List<cobject__c>()); }into the below using ternary operator
mapProductToPLI = (!mapProductToPLI.containsKey(oPriceListItem.Apttus_Config2__ProductId__c)) ? mapProductToPLI.put(oPriceListItem.Apttus_Config2__ProductId__c, new List<Apttus_Config2__PriceListItem> ) : nul ;
If no what is wrong here . Explain me elaborately
Your statement will probably will result in an Illegal Assignment error because the return type of the put() method is an object(the object might get dynamically casted to string type with null value). But you're assigning this to a map type variable.
You can try something like this:
string s=(!mapProductToPLI.containsKey(oPriceListItem.Apttus_Config2__ProductId__c)) ?mapProductToPLI.put(oPriceListItem.Apttus_Config2__ProductId__c, new List<Apttus_Config2__PriceListItem> ) : null ;
You should be getting a null value in variable s. But you should not be bothered about the value in 's' because your map will get updated according to your needs.
Note that you can't use the rhs statement without assigning it to some variable. Doing so will result in an error saying expression cannot be statement.
This is not a optimal solution, but it will get your work done.
Let me know if it helps.
I have written an alternative. Let me know if it is right. I'm first getting the product id and then putting it into the map
I think you will probably get an error saying: Invalid type: productid__c
Your code should look something like this:
But the problem which you might face with this code is that every time
(!mapProductToPLI.containsKey(oPriceListItem.ProductId__c)) this condition returns false, you will end up inserting a NULL in the keyset of your map, which can be disastrous.
And since key values in a map are unique so for the second time if the condition returns false, the value will be updated instead of being inserted against the NULL key.