+ Start a Discussion
nandacnandac 

Trigger to update the balance amount on a bill

Dear Folks,

 

I have been struggling with trying to write a trigger to update a balance on a bill when a payment is made on it. The progress I have made is below:

 

trigger UpdateBalanceOnPayment on Payment__c (after insert) {
    for (Payment__c payment : Trigger.new) {
        Bill__c bill = [SELECT Id, Balance__c FROM Bill__c
            WHERE Id = :payment.Bill__c.Id];
    }
}

For some reason the Force.com IDE says invalid foreign key relationship and does not synchronize with the server.

Each payment will have a master detail relationship with the associated bill when it is saved and I want to reduce the balance on the bill by the amount made by the payment after the payment is saved.

 

However I have not even got to that stage yet because it looks like I have made a mistake in the query. I would appreciate any pointers you can lend on this problem.

 

Many Thanks

nandac

Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

Hi,

 

It is not a good practise to include a SOQL inside a trigger. Hope the below code helps

 

trigger UpdateBalanceOnPayment on Payment__c (after insert) {
	//get all the bill Id's in a set 
	set<Id> billId = new set<Id>();
	for(payment__c p : trigger.new){
		billId.add(p.Bill__c);
	}
	
	//Query for the bill records by using the set.
	map<id, bill__c> billMap = new map<id, bill__c>([select id, balance__c from bill__c where Id IN : billId]);
	
	//this is the map to be updated.
	map<id, bill__c> updateBillMap = new map<id ,bill__c>();
	
	//I am assuming that amount__c is a field on the payment object. Replace it with the correct api name
	for(payment__c p : trigger.new){
		if(updateBillMap.containsKey(p.bill__c))
			bill__c b = updateBillMap.get(p.bill__c);
			b.balance__c = b.balance__c - p.amount__c;
			updateBillMap.put(b.id, b);
		}else{
			bill__c b = billMap.get(p.bill__c);
			b.balance__c = b.balance__c - p.amount__c;
			updateBillMap.put(b.id, b);
		}
	}
	
	update updateBillMap.values();
}

 

-ಸಮಿರ್

All Answers

jungleeejungleee

Hi,

 

It is not a good practise to include a SOQL inside a trigger. Hope the below code helps

 

trigger UpdateBalanceOnPayment on Payment__c (after insert) {
	//get all the bill Id's in a set 
	set<Id> billId = new set<Id>();
	for(payment__c p : trigger.new){
		billId.add(p.Bill__c);
	}
	
	//Query for the bill records by using the set.
	map<id, bill__c> billMap = new map<id, bill__c>([select id, balance__c from bill__c where Id IN : billId]);
	
	//this is the map to be updated.
	map<id, bill__c> updateBillMap = new map<id ,bill__c>();
	
	//I am assuming that amount__c is a field on the payment object. Replace it with the correct api name
	for(payment__c p : trigger.new){
		if(updateBillMap.containsKey(p.bill__c))
			bill__c b = updateBillMap.get(p.bill__c);
			b.balance__c = b.balance__c - p.amount__c;
			updateBillMap.put(b.id, b);
		}else{
			bill__c b = billMap.get(p.bill__c);
			b.balance__c = b.balance__c - p.amount__c;
			updateBillMap.put(b.id, b);
		}
	}
	
	update updateBillMap.values();
}

 

-ಸಮಿರ್

This was selected as the best answer
nandacnandac

Thanks that works but I think you meant to have the if statement like this without the "!". Do let me know if I am wrong in this case but the trigger does what I envisaged now. :-)

 

if (updateBillMap.containsKey(p.bill__c))
jungleeejungleee

updated the code. Thanks!