+ Start a Discussion
DanielBProbertDanielBProbert 

Trigger to Update a Master Record

Hi All,

 

Another little trigger query(I am learning at least :))

 

So I have a requirement to update a master record based on a critera.

 

Master Object = Loan__c

Child Object = Loan_Repayments__c

 

The trigger needs to work like this,

 

IF Child Object is updated then after that update the trigger should look at Loan__c and then verify that number_of_repayments__c and payments_received__c are =, if they are equal then within Loan__c the field Loan_Status__c needs to be updated to show as Completed.

 

I found some code on here that I have tried to get in place but I actually think it's working the wrong way and would appreciate some pointers about the right way to deal with this.

 

I tried with a workflow but a workflow only works once the loan__c is edited/updated which won't be often.

 

trigger LoanCompletedUpdate on Loan_Repayments__c (after insert, after update) {
 
    
    list<Loan__c> lstLoan = new list<Loan__c>();  
    set<Id> Ids = new Set <Id>();           
    for (Loan_Repayments__c lrp: Trigger.new) {             
        Ids.add(lrp.Loan__c); 
    }
    
    Loan__c loan = [SELECT Id, Number_Of_Repayments__c, Payments_Received__c, Loan_Status__c FROM Loan__c WHERE Id IN :Ids];  
    
    if(trigger.isInsert || trigger.isUpdate)
    {   
        for(Loan_Repayments__c objC : trigger.new) 
        {
            IF (loan.Number_Of_Repayments__c!=loan.Payments_Received__c) 
            {
                
                loan.Loan_Status__c = 'Completed';
                lstLoan.add(loan);
            } 
        }
   }
   IF(lstLoan.size()>0)
         Update loan;
}

 

that codes doesn't seem to do anything when I make a change to the loan_repayments__c object.

 

cheers

dan

hitesh90hitesh90

Hi Daniel,

 

try below updated trigger code.

 

trigger LoanCompletedUpdate on Loan_Repayments__c (after insert, after update) {
 
    
    list<Loan__c> lstLoan = new list<Loan__c>();  
    set<Id> Ids = new Set <Id>();           
    for (Loan_Repayments__c lrp: Trigger.new) {             
        Ids.add(lrp.Loan__c); 
    }
    
    Loan__c loan = [SELECT Id, Number_Of_Repayments__c, Payments_Received__c, Loan_Status__c FROM Loan__c WHERE Id IN :Ids];  
    
    if(trigger.isInsert || trigger.isUpdate)
    {   
        for(Loan_Repayments__c objC : trigger.new) 
        {
            IF (loan.Number_Of_Repayments__c==loan.Payments_Received__c) 
            {
                
                loan.Loan_Status__c = 'Completed';
                lstLoan.add(loan);
            } 
        }
   }
   IF(lstLoan.size()>0)
         Update lstLoan;
}

 
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

DanielBProbertDanielBProbert

hi this seems to work but only when I do this.

 

I have a loan with 5 repayments.

 

when I make all 5 repayments as received then nothing happens, but as soon as I edit and resave the 5th payment then it makes itself as completed.

 

would this be anything related to the fact that the payments_received__c is a rollup summary of the number of payments that are marked as received?

 

if so any ideas how i could get round this as the trigger seems to work well.

 

thanks for your quick response.

 

dan

 

hitesh90hitesh90

Hi Dan,

 

try below code..

 

trigger LoanCompletedUpdate on Loan_Repayments__c (after insert, after update) {
    list<Loan__c> lstLoan = new list<Loan__c>();  
	list<Loan__c> lstLoanToUpdate = new list<Loan__c>();  
    set<Id> Ids = new Set <Id>();           
    for (Loan_Repayments__c lrp: Trigger.new) {             
        Ids.add(lrp.Loan__c); 
    }    
    lstLoan = [SELECT Id, Number_Of_Repayments__c, Payments_Received__c, Loan_Status__c FROM Loan__c WHERE Id IN :Ids];  
     
	for(Loan__c objC : lstLoan) {
		IF (loan.Number_Of_Repayments__c==loan.Payments_Received__c){		
			loan.Loan_Status__c = 'Completed';
			lstLoanToUpdate.add(loan);
		}
	}
	 IF(lstLoanToUpdate.size()>0)
         Update lstLoanToUpdate;	
    
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

DanielBProbertDanielBProbert

thanks for the quick response, i now get an error when saving this trigger:

 

Compile Error: Variable does not exist: loan.Loan_Status__c at line 12 column 13

 

I can see the variable doesn't exist for loan anymore - it used to be on line 8.

 

i tried updating to be lstLoan which is what the variable on line 8 is but have got the following error:

 

Compile Error: Initial term of field expression must be a concrete SObject: LIST<Loan__c> at line 12 column 13

 

thanks dan

 

 

 

 

hitesh90hitesh90

Hi,

 

try below code.

 

trigger LoanCompletedUpdate on Loan_Repayments__c (after insert, after update) {
    list<Loan__c> lstLoan = new list<Loan__c>();  
	list<Loan__c> lstLoanToUpdate = new list<Loan__c>();  
    set<Id> Ids = new Set <Id>();           
    for (Loan_Repayments__c lrp: Trigger.new) {             
        Ids.add(lrp.Loan__c); 
    }    
    lstLoan = [SELECT Id, Number_Of_Repayments__c, Payments_Received__c, Loan_Status__c FROM Loan__c WHERE Id IN :Ids];  
     
	for(Loan__c objC : lstLoan) {
		IF (objC.Number_Of_Repayments__c==objC.Payments_Received__c){		
			objC.Loan_Status__c = 'Completed';
			lstLoanToUpdate.add(objC);
		}
	}
	 IF(lstLoanToUpdate.size()>0)
         Update lstLoanToUpdate;	
    
}

 

 

Thank You,

Hitesh Patel

DanielBProbertDanielBProbert

hi with this I still need to go in and edit the last record again before it marked the loan as completed.

 

i've found a crude workaround though.

 

just by making sure something is updated on the loan__c object after I update - i've then put in place a workflow on the object that works now that the loan__c is being updated.

 

so although not entirely managed by the trigger this has sorted out my issue for now.

 

would love to see if working purely from the trigger but this works.

 

trigger LoanCompletedUpdate on Loan_Repayments__c (after update) {
    list<Loan__c> lstLoan = new list<Loan__c>();  
    list<Loan__c> lstLoanToUpdate = new list<Loan__c>();  
    set<Id> Ids = new Set <Id>();           
    for (Loan_Repayments__c lrp: Trigger.new) {             
        Ids.add(lrp.Loan__c); 
    }    
    lstLoan = [SELECT Id, Payments_Received__c, Hidden_Payments_Received__c FROM Loan__c WHERE Id IN :Ids];  
     
    for(Loan__c objC : lstLoan) {
        {       
            objC.Hidden_Payments_Received__c = objC.Payments_Received__c;
            lstLoanToUpdate.add(objC);
        }
    }
     IF(lstLoanToUpdate.size()>0)
         Update lstLoanToUpdate;    
    
}

 i've then got a workflow that verifies that number of payments = payments received.

 

thanks for all your help.

 

dan