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
GYAN ANDRUSGYAN ANDRUS 

Can anyone help for this,I am taking the peer__Amount_Funded__c (number field) where status is funded ,I need sum all the values ,but it is not working

I need to sum all the value of peer_amount_funded__c .
And i have created the custom field in custom Setting Base_value__c..I  want to sum all the value of peer_amount__c and add it to base_value__C..Please help me  on this

for (Account obj : [select id,Total_Amount_Funded__c, ( Select Id,name,peer__Amount_Funded__c from peer__Loan_Application__r where  status__c =' Funded' ) from Account Where Type='Borrower'])
            {
                      Decimal total_amt = 0.0;
                      for(peer__Loan_Application__c temp : obj.peer__Loan_Application__r){
                       total_amt = total_amt + temp.peer__Amount_Funded__c;
                       }
           obj.Total_Amount_Funded__c =  total_amt;
           system.debug('LOAN DISBURSED SO FAR -----------:'+total_amt);  
           }
Pankaj_GanwaniPankaj_Ganwani
Hi Gyan,

You can use below mentioned code to achieve this. Please update the custom setting name with the one you created in your org:

Decimal total_amt = 0.0;
for (Account obj : [select id,Total_Amount_Funded__c, ( Select Id,name,peer__Amount_Funded__c from peer__Loan_Application__r where  status__c =' Funded' ) from Account Where Type='Borrower'])
            {
                     
                      for(peer__Loan_Application__c temp : obj.peer__Loan_Application__r){
                       total_amt = total_amt + temp.peer__Amount_Funded__c;
                       }
           system.debug('LOAN DISBURSED SO FAR -----------:'+total_amt);  
           }
insert new CustomSetting__c(Base_value__c = total_amt);
GYAN ANDRUSGYAN ANDRUS
Can you please explain ,,How to  add the basevalue to the total Amount?
Pankaj_GanwaniPankaj_Ganwani
Hi Gyan,

If your intention is to add the basevalue of custom field to the totalAmount field then you can do something like this:

Decimal total_amt = 0.0;
CustomSetting__c obj = CustomSetting__c.getInstance('Name'); //fetch the instance of custom setting that contains the base value
for (Account obj : [select id,Total_Amount_Funded__c, ( Select Id,name,peer__Amount_Funded__c from peer__Loan_Application__r where  status__c =' Funded' AND  peer__Amount_Funded__c!=NULL) from Account Where Type='Borrower'])
            {
                     
                      for(peer__Loan_Application__c temp : obj.peer__Loan_Application__r){
                       total_amt = total_amt + temp.peer__Amount_Funded__c;
                       }
           system.debug('LOAN DISBURSED SO FAR -----------:'+total_amt);  
           }

total_amt  +=obj.BaseValue__c+total_amt;// add base value to total amount.
system.debug('=================='+total_amt);
obj.BaseValue__c = total_amt;//assign total amount field value to base value
update obj;//update the base value in custom setting.


Please refer the above mentioned highlighted statements and let us know in case of any issues.
GYAN ANDRUSGYAN ANDRUS
          
             Decimal total_amt = 0.0;
             Base_live_funded_amount__c  obj1 = Base_live_funded_amount__c.getInstance('Base_Live_Amount_funded_so_far__c');
            for (Account obj : [select id,Total_Amount_Funded__c, ( Select Id,name,peer__Amount_Funded__c from peer__Loan_Application__r where  status__c =' Funded' AND  peer__Amount_Funded__c!=NULL) from Account Where Type='Borrower'])
            {
                       for(peer__Loan_Application__c temp : obj.peer__Loan_Application__r)
                      {
                       total_amt = total_amt + temp.peer__Amount_Funded__c;
                       }
           system.debug('LOAN DISBURSED SO FAR -----------:'+total_amt);  
           }

        total_amt  +=obj1.Base_Live_Amount_funded_so_far__c+total_amt;// add base value to total amount.

       total_amt =  obj1.Base_Live_Amount_funded_so_far__c ; //assign total amount field value to base value
       system.debug('=================='+total_amt);



Got error,,attempt to dereference a null object
JethaJetha
Use Below code :
Decimal total_amt = 0.0;
Base_live_funded_amount__c  obj1 = Base_live_funded_amount__c.getInstance('Base_Live_Amount_funded_so_far__c');

total_amt = obj1;
		   
for (peer__Loan_Application__c objPeerLocation : [ Select Id,AccountId, name,peer__Amount_Funded__c from peer__Loan_Application__c where  status__c =' Funded' AND  peer__Amount_Funded__c!=NULL AND Account.Type='Borrower'])
{
   if(objPeerLocation.peer__Amount_Funded__c != null)
			 total_amt = total_amt + objPeerLocation.peer__Amount_Funded__c;

}