+ Start a Discussion
jcaldjcald 

Can IF statement change id in MyController side apex code?

Im kinda in a pickle of sorts....and I don't know how to go about this but here it goes....

I have an amount field, depending on the range amount (for ex. 3000-9999, 10000-14999, and 15000-20000). I would like to change the id in MyController

CONTROLLER:
public Price_Range__c getRate4() {
    return [SELECT Range_Rate_Month24__c FROM Price_Range__c WHERE Id =:  'a67K00000004Emt'  LIMIT 1];
    }

VF PAGE:
<td><apex:outputtext value="{0, number, $000.00}">
    <apex:param value="{!Worksheet__c.Points__c*Worksheet__c.Finance_Amount__c*Rate4.Range_Rate_Month24__c}"/>
    </apex:outputtext>
</td>


Would it better to create an IF statement on the Amount field (Formula/Number), or create a hidden field based on the range and do a call out like:

IF( Finance_Amount__c >149999, "WHERE Id =:'a67K00000004Emt'",
IF(Finance_Amount__c >99999, "WHERE Id =:'a67K00000004Emy'",NULL
))
 
This compiles but I don't know if this is the right way to do this. Can someone share their knowledge on how to go about this?

Thanks
Ramu_SFDCRamu_SFDC
Hi, Assuming that the id's you are referring to in SOQL statement are static you can go with defining a public Id variable and apply the value using if else statement. Something like the below code


Public Id;
public Price_Range__c getRate4() {
if(Finance_Amount__c >149999){
id='a67K00000004Emt'; 
}
else if(Finance_Amount__c >99999 && Finance_Amount__c <149999){
id='a67K00000004Emy';  
}
return [SELECT Range_Rate_Month24__c FROM Price_Range__c WHERE Id =:id  LIMIT 1];
}
jcaldjcald
This is great! thanks for this. Although, I am getting a "Compile Error: unexpected token: ';' " on the Public Id. I am not familiar defining a public id variable. Do I define the variable in my VF page?
jcaldjcald
ok. now I need to call my field (Finance_Amount__c) a variable?  I'm confused now

            Worksheet id;

            public Price_Range__c getRate4_1() {
                if(Finance_Amount__c >149999){
                    id='a67K00000004Emt';
            }
                if(Finance_Amount__c >99999){
                    id='a67K00000004Emy';
            }
            return [SELECT Range_Rate_Month24__c FROM Price_Range__c WHERE Id =:id  LIMIT 1];
            }


ERROR: Variable does not exist" Finance_Amount__c

can someone show me how to create a variable from a field? is it something like this....

Finance_Amount__c Amount;
Amount = [SELECT Finance_Amount__c FROM Worksheet__c WHERE Id =: '(worksheet.id)'  LIMIT 1];
jcaldjcald
Can someone help? im getting this error:

Error: WorksheetLender Compile Error: Illegal assignment from LIST<Worksheet__c> to worksheet at line 34 column 1


Controller:
public Price_Range__c getRate4_1() {
Worksheet amount = [SELECT Finance_Amount__c FROM Worksheet__c WHERE Id =:  '01IK0000000ECGv'  LIMIT 1];
if(amount >149999){
mylender='a67K00000004Emt';
}
if(amount >99999){
mylender='a67K00000004Emy'; 
}
return [SELECT Range_Rate_Month24__c FROM Price_Range__c WHERE Id =:mylender  LIMIT 1];
}
Ramu_SFDCRamu_SFDC
The error is due to improper assignment of list of worksheet__c objects to unknown datatype worksheet. 

Change the 2nd line as below

Decimal amount=[SELECT Finance_Amount__c FROM Worksheet__c WHERE Id =:  '01IK0000000ECGv'  LIMIT 1].Finance_Amount__c;
jcaldjcald
ok im still in a rut. I'd like to try to make this work.

Error: WorksheetLender Compile Error: Illegal assignment from String to worksheet at line 41 column 1


public Price_Range__c getRate4() {
Worksheet A;
Decimal amount=[SELECT Finance_Amount__c FROM Worksheet__c WHERE Id =:  '01IK0000000ECGv'  LIMIT 1].Finance_Amount__c;
if(amount >149999){
A='a67K00000004Emt'; (line 41)
}
else if(amount >99999 && a <149999){
A='a67K00000004Emy';
}
return [SELECT Range_Rate_Month24__c FROM Price_Range__c WHERE Id =:A.id  LIMIT 1];
}


please help Thanks