You need to sign in to do that
Don't have an account?
cnaranjo
IF variable && variable
I was wondering how can I do something like this with Apex Code:
IF(variable='text' && variable2='text2') variable3='value would be this', variable4='this', variable5='this', etc, etc
The idea is that if the value of variable and variable2 are correct this will change values on variable3, 4, 5, 6, 7, ...
Is this possible?
Thanks in advanced!
Hello again,
I have been trying a couple of things and finally I found a very simple solution.
It is possible to use IF statement with concatenation and I did it just like this.
Thanks to all for your help.
Regards,
Carlos
All Answers
cnaranjo:
This may seem too obvious but .. (assuming you've defined these variables with declarations as in String variable; String variable2
Hello cropzim,
Thank you for your respond.
Well, I know that I can do that but I was talking more about the possibility of assigning a value to a variable based on two other fields, something like this formula:
IF(( Veh__c ="Red" && Model__c = 'Ford ' ), '', "No available ")
When Veh__c && Model__c are 'Red' && 'Ford' field x='No available'
More than merging I was talking about asigning a value based on two differenet fields.
How can I do this with Apex Code?
Regards and thanks
I think you are looking for something like SWITCH... CASE STATEMENT... which is not available in APEX.
Apex does not support switch case statements, on the other hand they have CASE Statement on formula fields which I feel is funny.
You need to manage with if .. else if .. statements.
Not the greatest answer but extensible and doesn't require much in the way of test coverage.
Hello again guys,
Thanks for your help.
I'm kind of lost with the scrip crop1645.
So I create a collection data type Map.
The first question is the operator '=>', what is the operator's action. Can't find anything regarding that operator.
Could you also please explain to me the 'redFord'=> 'result1' as a all?
I'm trying to understand where do I need to use the fields Veh__c and Model__c.
It looks like'redFord' is containing both of values coming from those two fields.
That is kind of confusing for me. I will appreciate if you could explain the logic of this piece of scrip.
The String compositeKey = color+make; also needs some explanation. If you could please.
I'm in the level of knowing the basic but can fully understand the logic which is so important in order for me to understand.
Hope I'm not asking to much, anyway, thank you so much for helping me.
Regards.
cnaranjo
Maybe we're missing what you want to do here.
The code I posted creates a map where the key is the concatenation of a color and a make. The corresponding value is some result field. Maps provide an easy way to do code-based lookups of key to values when you don't have an SObject (Table) in SFDC to use as a reference table.
The key of a map can be almost anything (ID, String, Integer, ..) (and in Winter 13, an SObject). The corresponding value may be any Sobject, any primitive, an object of any class, lists or sets of objects/primitives/sobjects, or even other Maps. Full details can be seen in the Apex Developers Guide under 'Collections'
If I've missed the boat on what you are trying to do, please repost with more precise psedo-code what you want to achieve
The formula I use in Salesforce is something like this:
IF(( XXXX__c ='YYY' && XXX2__c = 'ZZZ' ' ), ' ', 'Stock ok') This is assign to a field. This field will change depending on the output of those two fields ( XXX__c AND XXX2__c)
This is the same formula but with the OR function.
IF(( Total_Inventory__c <1500 && Name = 'Green' || Total_Inventory__c <1500 && Name = 'Red' || Total_Inventory__c <1500 && Name = 'Yellow' ), 'Product under 1500 in stock', 'Stock ok')
This formula is an example but the one I'm using it is becoming very long and that is why I need to use something that will accommodate all those options.
The all idea is to have a flow, that after going through a number of steps, will create a new record in a custom object containing data from that flow.
After the record have been created a trigger will add values to other fields on that new record.
All this is already in place and working (Flow, Trigger)
So I was looking for an APEX code to modify my trigger that will look into 2 or more fields and depending the value of those fields will populate one or more specify fields on the same page layout on that new record.
I'm using a trigger now but the amount of variables is so big that is not efficient anymore. I need to rebuild the all scrip but I'm not really a guru in APEX :( , at least not yet!
The trigger with some variables it is just fine but that is not the case anymore, we have to many variables and we need to combine them in a different way.
The trigger looks something like this, I have removed most of it for this example:
Apologies if I didn't explain better what it is what I was looking for.
Maybe this is a better explanation?
Regards.
Carlos
cnaranjo
Well, in general, my approach only works for exact lookups; when you have inequalities, there really isn't an alternative short of lots of well-commented if-then else statements
I would suggest you create a class called IB_Ex_Wrapper that looks something like this:
then your trigger gets simpler and looks something like this:
even this could be simplified by moving the assignments into the Wrapper class
Bottom, line, if you have lots of complex conditionals, create a wrapper class, move the conditionals into methods in the wrapper class and keep the trigger simple. This has several advantages:
1. You can write testmethods on your conditionals without having to do any database inserts and trigger invocations
2. You can hide complexity of logic into well-encapsulated methods
3. Trigger stays easy to read and focuses on what should be limited to trigger logic -- processing the trigger lists, delegating work to wrapper classes
I can't keep going on this one as your business logic is obviously complex but I hope these techniques will help you organize your code to be maintainable and readable
Thanks!
Sorry for the dealy but I have been out for few days.
Ok, I will try to work whit that.
Regards,
Carlos
Hello again,
I have been trying a couple of things and finally I found a very simple solution.
It is possible to use IF statement with concatenation and I did it just like this.
Thanks to all for your help.
Regards,
Carlos