function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KitagawaSan85KitagawaSan85 

If statement help

Pretty new to this and hoping somebody can help... 

 

I want to have a controller do some calculations for me. 

 

for example, I want to return a weighting %  based on age of the opportunity. 

 

I have defined the age as such: 

 public integer getAge() {
        if(opp.isClosed == false) 
return Date.valueof(opp.CreatedDate).daysBetween(date.today());
return Date.valueof(opp.CreatedDate).daysbetween(opp.CloseDate); }

 

However, I now want to build a decimal based on that, but cant seem to be able to access the Age value that I just created even thought it is public. 

 

decimal weight = if(age<10)
{.10;}else{.30;}

 Does not work as it appears I cant use an if statment to define a variable. I get the error Expression cannot be a statement. 

 

What is the proper way to do this? 

 

Jia HuJia Hu
try this,
decimal weight;
integer age = 5;
if(age<10) {
weight = .10; } else {
weight = .30;
}
system.debug(' ------- ' + weight );
KitagawaSan85KitagawaSan85

Thanks, it still says that Age is not a variable. 

 

public integer getAge() {
        if(opp.isClosed == false) return Date.valueof(opp.CreatedDate).daysBetween(date.today()); return Date.valueof(opp.CreatedDate).daysbetween(opp.CloseDate); 
    }  

 public decimal getAgeProb() {
     decimal prob;
        	integer age = age;
        	if(age<1){prob = 0.9;}else{prob = 0.1;}
			
        	return test;

 Do I have to annotate it differently to call a variable defined in another function even if it is public?

Abhishek_NewAbhishek_New

try this...

 

integer age = getAge();