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
Amit Jadhav 13Amit Jadhav 13 

hey How to Calculate Two Custom Number Field In Controller Method And result Store in integer Variable please Help Me

Suraj DemgundeSuraj Demgunde
integer f1 = '';
integer f2 = '';
integer f3 = f1+f2;
integer.valueof(f3 );

LIKE THIS
Amit Jadhav 13Amit Jadhav 13
Hey @suraj calculate the salesforce custom field not a integer and result store in int Variables then problem happen when accessing fields in mrthod
Harsh P.Harsh P.
Hi Amit,

The field created with Number datatype will be decimal value.So you need to first store that fields value to decimal.
Use this logic. Here TotalOpportunityQuantity,ST_NO__c both are the Number fields.
list<Opportunity> oppList = [Select Id, Name,TotalOpportunityQuantity,ST_NO__c from Opportunity];
decimal total;

for(Opportunity o : oppList ){
    if(o.TotalOpportunityQuantity !=null && o.ST_NO__c !=null){

        total = o.TotalOpportunityQuantity + o.ST_NO__c;
        Integer intConv = integer.valueOf(total);
    }
else
{
system.debug('***');
}
}
If above solution helpful Mark as best Answere to help others.........

Regards,
Harsh P.
 
Ajay K DubediAjay K Dubedi
Hi Amit,
Please try the below code and let me know if this works for you. If still need modifications do let me know.
 
public class SumOfTwoCustomfieldValue {
    public SumOfTwoCustomfieldValue () {
        /*Account accObj = new Account (Name='TestSum' , FirstNum__c = 20);
        insert accObj; */
        Account acc = [SELECT FirstNum__c FROM Account WHERE Name Like '%TestSum%' Limit 1];
        Integer custom_val = (acc.FirstNum__c).intValue();
        System.debug(custom_val);
    }
}


 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi