+ Start a Discussion
cnaranjocnaranjo 

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!

Best Answer chosen by Admin (Salesforce Developers) 
cnaranjocnaranjo

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.

 

for(Object o: Trigger.new) {
            if ((o.Model__c =='Opel')&&(o.Color__c =='Red')){

// etc

 

 

Thanks to all for your help.

 

Regards,

 

Carlos 

All Answers

cropzimcropzim

cnaranjo:

This may seem too obvious but .. (assuming you've defined these variables with declarations as in String variable; String variable2

IF(variable=='text' && variable2=='text2') {
 variable3='value would be this';
 variable4='this';  
 variable5='this';
}

 

cnaranjocnaranjo

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

SFDC-SDSFDC-SD

 

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. 

crop1645crop1645
Map<String,String> keyToValMap = new Map<String>,<String> {
     'redFord' => 'result1',
     'blueFord' => 'result2',
     'redChevy' => 'result3',
     'blueChevy' => 'result4'};

String compositeKey = color+make;
String result = keyToValMap.containsKey(compositeKey) ?
                  keyToValMap.get(compositeKey) : null;

 Not the greatest answer but extensible and doesn't require much in the way of test coverage.

 

cnaranjocnaranjo

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.

 

crop1645crop1645

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

 

cnaranjocnaranjo

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:

 

trigger NewIen on IB_Ex__c (before insert) {
  List <IB_Exs__c> newEx = new List <IB_Ex__c> ();
    
      for(IB_Ex__c o: Trigger.new) {
            if (o.External_Reference_Number_Flow__c =='W045'){
                 o.XXX1__c = '2317';
                 o.XXX2__c ='3112';
                 o.XXX3__c='4606';
                 o.XXX4__c ='1425';
                                 }
             if (o.External_Reference_Number_Flow__c =='W047'){
                 o.YYY1__c = '5817';
                 o.YYY2__c ='3612';
                 o.YYY3__c='4698';
                 o.YYY4__c ='1155';
                 }
                  
          IB_Ex__c v = new IB_Ex__c (id = o.id);
              
               newEx.add(o);
            }
        }

 

 

Apologies if I didn't explain better what it is what I was looking for.

Maybe this is a better explanation? 

 

 

Regards.

 

Carlos 

crop1645crop1645

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:

 

public without sharing class IB_Ex_Wrapper {
  
  //	Constructor
  public IB_Ex_Wrapper () {}

	// inner class	
  public class IB_Ex_Result {
     public String XXX1;
     public String XXX2;
     public String XXX3;
     public String XXX4;
  
  // inner class constructor
  public Ib_Ex_Result(String x1, String x2, String x3, String x4) {
  	this.XXX1 = x1;
  	this.XXX2 = x2;
  	this.XXX3 = x3;
  	this.XXX4 = x4;
  }
  //	alternate constructor, all results set to null
  public IB_Ex_Result() {}
  }
  // result map
  private Map<String,IB_Ex_Result> keyToResultMap = new Map<String,IB_Ex_Result> {
  							'W045' => new IB_Ex_Result('2317','3112','4606','1425'),
  							'W047' => new IB_Ex_Result('5817','3612','4698','1155')
  							// add more rows as needed
  	};
  
  // getResult
  public IB_EX_Result getResult(String key) {
  	if (this.keyToResultMap.containsKey(key)) return this.keyToResultMap.get(key);
  	else return new Ib_Ex_Result();
  }
  
}

 then your trigger gets simpler and looks something like this:

trigger NewIen on IB_Ex__c (before insert) {
  List <IB_Exs__c> newEx = new List <IB_Ex__c> ();
    
      for(IB_Ex__c o: Trigger.new) {
          IB_Ex_Wrapper ibexW = new IB_Ex_Wrapper();
          IB_Ex_Wrapper.IB_Ex_Result ibexRes = ibexW.getResult(o.External_Reference_Number_Flow__c);
          o.xxx1__c = ibexRes.xxx1;
          o.xxx2__c = ibexRes.xxx2;
          o.xxx3__c = ibexRes.xxx3;
          o.xxx4__c = ibexRes.xxx4;
                  
          IB_Ex__c v = new IB_Ex__c (id = o.id);
              
               newEx.add(o);
            }
        }

 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

cnaranjocnaranjo

Thanks!

Sorry for the dealy but I have been out for few days. 

Ok, I will try to work whit that.

 

 

Regards,

 

 

Carlos 

cnaranjocnaranjo

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.

 

for(Object o: Trigger.new) {
            if ((o.Model__c =='Opel')&&(o.Color__c =='Red')){

// etc

 

 

Thanks to all for your help.

 

Regards,

 

Carlos 

This was selected as the best answer