+ Start a Discussion
GYAN ANDRUSGYAN ANDRUS 

Please help for this code,Total value is not calculating

         
             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;
                        
                            if( total_amt != null &&  total_amt >0)
                            {
                               total_amt  +=obj1.Base_Live_Amount_funded_so_far__c+total_amt;
                               total_amt =  obj1.Base_Live_Amount_funded_so_far__c ;
                                system.debug('TTTTTTTTTTTTTTTTTTTTTTT -----------:'+total_amt);
                            
                             }

                       }

              
    
        
           }

 
Best Answer chosen by GYAN ANDRUS
JethaJetha
Hi Gyan,

Please give a try to my code also, It would work for sure and don't forget to mark it as best answer.
 
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 = (Decimal)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;

}

 system.debug('TTTTTTTTTTTTTTTTTTTTTTT -----------:'+total_amt );




 

All Answers

sandeep sankhlasandeep sankhla
H, Gyan,

Can you let me know what exactly you want to do, you just want to do the sum of all peer__Amount_Funded__c realted to accounts right ?

I mean Account is parent and peer__Loan_Application__c  is child and peer__Amount_Funded__c is on child and you just wamt to some all teh amount related to one account and you want to show on parent right ?

Please confirm so I can help you out ..

Thanks
GYAN ANDRUSGYAN ANDRUS
I am taking the peer__Amount_Funded__c (number field)  value  based on the condition where status__c = 'funded' from the object called peer__Loan_Application__c ,
I need to add all the values of peer_amount_funded__c  and save it to total_amt variable.After adding all the values i want to add  Base_value__c in total_amt variable.
And i have created the custom field in custom Setting Base_value__c.no need of parent child value only in
 peer__Loan_Application__c


Please help me
 
sandeep sankhlasandeep sankhla
Hi Gyan,

Map<Id, Decimal> mapAccIdToDecimal = new Map<Id, Decimal>();
           
           In below code you have not stored the sum in map so every time it will be reset ...so you need to create a mapo and then you can store the sum in that map for each account. also we can optimise the code as per your need. I just share the code using map where you can store teh values against each account and then you can get the value for each our from map and use as you want if you want to populate them in account field that also you can do.
 
Map<Id, Decimal> mapAccIdToDecimal = new Map<Id, Decimal>();

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;
                        
                            if( total_amt != null &&  total_amt >0)
                            {
                               total_amt  +=obj1.Base_Live_Amount_funded_so_far__c+total_amt;
                               total_amt =  obj1.Base_Live_Amount_funded_so_far__c ;
                                system.debug('TTTTTTTTTTTTTTTTTTTTTTT -----------:'+total_amt);
                            
                             }
							mapAccIdToDecimal.put(obj.Id, total_amt);
                       }

Please refer the given code and let me know if that helps you.

Thanks,
Sandeep
sandeep sankhlasandeep sankhla
You need to remove line no 16 because if that value will be 0 then it will reset he value.
sandeep sankhlasandeep sankhla
Let me know if that helps else I will change the code for you.
sandeep sankhlasandeep sankhla
Hi
In below code just replace the field name of account on peer__Loan_Application__c. then it will do the job for you.
Map<Id, Decimal> mapAccIdToDecimal = new Map<Id, Decimal>();
		   Base_live_funded_amount__c  obj1 = Base_live_funded_amount__c.getInstance('Base_Live_Amount_funded_so_far__c');
		   
		    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(mapAccIdToDecimal.containskey(objPeerLocation.AccountId))
			   {
			       mapAccIdToDecimal.put(objPeerLocation.AccountId, mapAccIdToDecimal.get(objPeerLocation.AccountId)+objPeerLocation.peer__Amount_Funded__c);
			   }
			   else
			   {
			       mapAccIdToDecimal.put(objPeerLocation.AccountId,objPeerLocation.peer__Amount_Funded__c + obj1.Base_Live_Amount_funded_so_far__c);
			   }
			
			}
			
			now in ablove map you will get the value for each account whith only where status is funded.Please debug the map and you will get teh values.
			system.debug('TTTTTTTTTTTTTTTTTTTTTTT -----------:'+mapAccIdToDecimal);

In above code I am directly querying on peer__Loan_Application__c where status is Funded and its parent type is borrower.

Then I am framing a map where I am storing sum of all peer__Amount_Funded__c as per the account . Please use and let me knwo if that helps you.
Thanks
Sandeep
sandeep sankhlasandeep sankhla
Also let me know above cide is not understanble from you. I will correct your code only so that will only work.. there always you were resetting teh values so it was not updated. if you use map and conatins key then iot will work there as well. but there you used for inside for which is not a good practise so better if you use my above code.

Thanks
GYAN ANDRUSGYAN ANDRUS
The final value will be stored in total_amount variable right..If yes,I put the mapAccIdToDecimal in System debug,But its not calculating

Map<Id, Decimal> mapAccIdToDecimal = new Map<Id, Decimal>();

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;
                        
                            if( total_amt != null &&  total_amt >0)
                            {
                               total_amt  +=obj1.Base_Live_Amount_funded_so_far__c+total_amt;
                               total_amt =  obj1.Base_Live_Amount_funded_so_far__c ;
                                system.debug('TTTTTTTTTTTTTTTTTTTTTTT -----------:'+total_amt);
                            
                             }
                            mapAccIdToDecimal.put(obj.Id, total_amt);
                                                    system.debug('Final value ---------:'+mapAccIdToDecimal);
                       }
}
sandeep sankhlasandeep sankhla
Hi You use belwo code as I already mentioned than it will work becasue in yoru code you used for inside for which is not a good practose also there are few mistakes so it is not caluclating priperly..
 
Map<Id, Decimal> mapAccIdToDecimal = new Map<Id, Decimal>();
		   Base_live_funded_amount__c  obj1 = Base_live_funded_amount__c.getInstance('Base_Live_Amount_funded_so_far__c');
		   
		    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(mapAccIdToDecimal.containskey(objPeerLocation.AccountId))
			   {
			       mapAccIdToDecimal.put(objPeerLocation.AccountId, mapAccIdToDecimal.get(objPeerLocation.AccountId)+objPeerLocation.peer__Amount_Funded__c);
			   }
			   else
			   {
			       mapAccIdToDecimal.put(objPeerLocation.AccountId,objPeerLocation.peer__Amount_Funded__c + obj1.Base_Live_Amount_funded_so_far__c);
			   }
			
			}
			
			now in ablove map you will get the value for each account whith only where status is funded.Please debug the map and you will get teh values.
			system.debug('TTTTTTTTTTTTTTTTTTTTTTT -----------:'+mapAccIdToDecimal);

Please use and let me know if that does the job for you.
sandeep sankhlasandeep sankhla
Also if above code only you want to use then you need to put map outise the for loop so once all child are calculated then only you will be putting the value inside map...

currently the code whichyou shared is putting values everytime and it will bve overiiden and last child value will be there so it is not proper..

you can keep that outide for loop if you want to use same code.
GYAN ANDRUSGYAN ANDRUS
      Thank you So much,But,I dont want the account object here,Only from the  peer__Loan_Application__c is enough,,If i removed that ,Its showing erroe.Please help me
sandeep sankhlasandeep sankhla
so what you want to do just show me one example...

you just want to add base amount in peer__Amount_Funded__c ?

like if you have 5peer__Loan_Application__c  where status is funded  now tell me what you want ?

peer__Loan_Application__c  obj1    ----peer__Amount_Funded__c  is 100
peer__Loan_Application__c  obj2    ----peer__Amount_Funded__c  is 500
peer__Loan_Application__c  obj3    ----peer__Amount_Funded__c  is 400
peer__Loan_Application__c  obj4   ----peer__Amount_Funded__c  is 300
peer__Loan_Application__c  obj5   ----peer__Amount_Funded__c  is 200 

and base amount from custom setting is 100..now tell me what you want so I can help you out.

Thanks
 
GYAN ANDRUSGYAN ANDRUS
 Total values peer__Amount_Funded__c =  1500
 Base_live_funded_amount__c  value= 500
 
 total_amt = 2000
sandeep sankhlasandeep sankhla
Hi
 
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 (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 && objPeerLocation.peer__Amount_Funded__c != '')
                         total_amt = total_amt + objPeerLocation.peer__Amount_Funded__c + obj1.Base_Live_Amount_funded_so_far__c;
			
			}

then this much code will work for this case. Please check and let me kbow if that helps.

Thanks
GYAN ANDRUSGYAN ANDRUS
GOT ERROR:COMPILE ERROR: Comparison arguments must be compatible types: Decimal, String
sandeep sankhlasandeep sankhla
Hi 
 
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 (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 + obj1.Base_Live_Amount_funded_so_far__c;
			
			}
remove that blank check as its decimal you can do if that value is 0 or not like that
 
sandeep sankhlasandeep sankhla
Hi Gyan,

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');
		   
		    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 != 0)
                         total_amt = total_amt + objPeerLocation.peer__Amount_Funded__c + obj1.Base_Live_Amount_funded_so_far__c;
			
			}

Let me know now if that logic works for you.

Thanks
GYAN ANDRUSGYAN ANDRUS
User-added imageMY BAD----the value coming with empty
JethaJetha
Hi Gyan,

Please give a try to my code also, It would work for sure and don't forget to mark it as best answer.
 
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 = (Decimal)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;

}

 system.debug('TTTTTTTTTTTTTTTTTTTTTTT -----------:'+total_amt );




 
This was selected as the best answer