Groovy 11 – if then else / switch Adding Logic To the Scripts

if-else Sometimes we want  to control the flow of the code we make. So that a block of code executes when certain conditions are met or execute another block of code and ignore other statements. In order to achieve this we utilize something called  if-else . This is the most basic flow control statements in groovy and actually in any most of the programming languages. if-else tell your script to execute certain parts of the code only if a particular condition evaluates to true or false. General Form of if –else
  1. if(condition){  
  2. //block of code to execute if the condition is met  
  3. }else{  
  4. //block of code to execute if the condition in if is not met  
  5. }  
Here is  a nice little trick to save you some typing. If there is  only  1 line of code, following if or else then you do not have to encapsulate it in between { }
  1. if(condition)  
  2. //1 line of code to execute  
  3. else  
  4. //1 line of code to execute 
Example 1:
  1. def var_A = 100  
  2. if(var_A == 100)  
  3. setAttribute(‘FieldName’,var_A)  
  4. else  
  5. setAttribute(‘FieldName’,200)  
Code above can also be written like:
  1. def var_A = 100  
  2. if(var_A == 100){  
  3. setAttribute(‘FieldName’,var_A)  
  4. }  
  5. else{  
  6. setAttribute(‘FieldName’,200)   
  7. }  
Example 2 :
  1. def var_A = 100  
  2. def var_B = 200  
  3. if(var_A <150){  
  4. var_A = var_B + 400  
  5. setAttribute(‘FieldName’,var_A)  
  6. }else{  
  7. var_B ++  
  8. var_A = var_B + 600  
  9. setAttribute(‘FieldName’,var_B)  
  10. }  
  This code can not be written as
  1. if(var_A <150)  
  2. var_A = var_B + 400  
  3. setAttribute(‘FieldName’,var_A)  
  4. else  
  5. var_B ++  
  6. var_A = var_B + 600  
  7. setAttribute(‘FieldName’,var_B)  
This is not correct Of course we can even combine the 2 styles like
  1. if(var_A <150){  
  2. var_A = var_B + 400  
  3. setAttribute(‘FieldName’,var_A)  
  4. }else  
  5. setAttribute(‘FieldName’,800)  
Sometimes we need even more complex logic to our code, so we control the flow depending on multiple conditions. So then we use an else if General  Form
  1. If(condition){  
  2. //block of code to execute if the if condition is met  
  3. }else if(condition){  
  4. //block of code to execute if the else if condition is met  
  5. }else{  
  6. //block of code to execute if none of the conditions above are met  
Example 3:
  1. def var_A = 100  
  2. if(var_A < 100){  
  3. setAttribute(‘FieldName’,10)  
  4. }  
  5. else if (var_A > 100){  
  6. setAttribute(‘FieldName’,300)   
  7. }else {  
  8. setAttribute(‘FieldName’,100)    
  9.   
If you need to add even more condition you can simply add another else if So you can do:
  1. if(condition)  
  2. //1 line of code  
  3.   
  4. else if(condition)  
  5. //1 line of code  
  6. else if(condition)  
  7. //1 line of code  
  8. else  
  9. //1 line of code  
The important thing to remember is that if you only have 1 line of code to execute  you do not have to encapsulate it in  between { } If you have a block of code to execute then use { } to encapsulate it Also as best practice it is recommend that if you start an if statement always end it with else The code could work just like this:
  1. if(condition){  
  2. // some code  
  3. }  
But to respect a standardize syntax end the if with an else, even if nothing needs to execute in the else part.
  1. if(condition){  
  2. //code of block  
  3. }else{  
  4. return false //this line means nothing will be returned, so basically nothing happens  
  5. }  
SWITCH Switch is usually used to replace the if else and to give your code a more compact form and ease to read. Sometimes also helps in making the code more efficient, however in the context of sales cloud I doubt the scripts you write using switch will actually be visibly more efficient, as the groovy engine imposes lots of limitations on the scripts that we can write. Therefor using switch is a matter of preference in the context of sales cloud and depending on your desire to make the code more compact General form of a Switch  
  1. switch(Variable_to_Check){  
  2.   
  3.   
  4. case 1:  
  5. //code to execute  
  6. break;  
  7. case 2:  
  8. //code to execute  
  9. break;  
  10. case 3:  
  11. //code to execute  
  12. break;  
  13. default  
  14. //code to execute  
  15. break;  
  16. }  
Lets take an classic example:
  1. def var_number = 10  
  2. switch(var_number ){  
  3. case 1:  
  4. println(“var_number  = “ + 1)  
  5.   
  6. break;  
  7. case 5:  
  8. println(“var_number  = “ + 5)  
  9.   
  10. break;  
  11. case 10:  
  12. println(“var_number  = “ + 10)  
  13.   
  14. break;  
  15. case 100:  
  16. println(“var_number  = “ + 100)  
  17.   
  18. break;  
  19. default:  
  20. println(“var_number  has a different value”   
  21. break;  
  22. }  
So the code above works like this: we have a variable define, var_number that is equal with 10. switch statement will check the value of this variable. If the value in the variable is in any of the cases then we use a function called println() to  display a message in the run-time messages of Application Composer. So first the switch checks if var_number is equal to 1 (case 1), obviously is not, so it move to case 5 (is var_number  equal to 5), moves on to case 10, here indeed var_number has the value 10 so it displays in run-time message the line var_number = 10 and exits the switch. Another thing to notice is that we use the keyword break. This makes it possible to exit the case the code is in at one time, if we would to remove the break keyword the code will simply execute everything until it encounters break or switch statement is over. So if we would to remove break from case 1, in rune-time message we would have  also the  text var_number = 1 and then var_number = 10

Leave a Reply

Your email address will not be published. Required fields are marked *