+ Start a Discussion
GasparGaspar 

Trigger Help - After Insert and After Update

Hello, I've written the following trigger to create a Payment__c record when a Deal_Participation__c record is created where RecordType = 'Seller Participation.' The trigger is behaving correctly for after insert BUT when an existing Deal_Participation__c record is updated, it creates another new Payment__c record instead of updating the Payment__c record. I think I need to rewrite the payments.add(new.Payments__c)... bit but I'm not sure how to do it. Any help is greatly appreciated!

 

 

trigger createSellerPayments on Deal_Participation__c (after Insert, after Update) {

 

List<Payments__c> payments = new List<Payments__c>();
ID rtId = [SELECT Id FROM RecordType WHERE Name = 'Seller Participation'].Id;
List<RecordType> PmntRecordType = [Select Id from recordType where Name = 'Outgoing Seller Payment' AND SobjectType= 'Payments__c'];

 

for (Deal_Participation__c newPayment: Trigger.New) {

if (newPayment.Deal_Relationship__c != null) {

if(newpayment.RecordTypeId == rtId){

payments.add(new Payments__c(

RecordTypeId = PmntRecordType[0].id,
Deal_Participation_Relationship__c = newPayment.Id));
}
}
}

insert payments;

}

Best Answer chosen by Admin (Salesforce Developers) 
digamber.prasaddigamber.prasad

Hi,

 

Could you please try below:-

 

trigger createSellerPayments on Deal_Participation__c (after Insert, after Update) {
 
	List<Payments__c> insertPayments = new List<Payments__c>();
	List<Payments__c> updatePayments = new List<Payments__c>();
	Set<Id> setDealPartId = new Set<Id>();
	Map<Id, Payments__c> MapPayment = new Map<Id, Payments__c>();
	ID rtId = [SELECT Id FROM RecordType WHERE Name = 'Seller Participation'].Id;
	List<RecordType> PmntRecordType = [Select Id from recordType where Name = 'Outgoing Seller Payment' AND SobjectType= 'Payments__c'];
	 
	for (Deal_Participation__c newPayment: Trigger.New) {
		if (newPayment.Deal_Relationship__c != null) {
	
			if(newpayment.RecordTypeId == rtId){
				if(trigger.isInsert)
					insertPayments.add(new Payments__c(RecordTypeId = PmntRecordType[0].id, Deal_Participation_Relationship__c = newPayment.Id));
			}
			
		}
		
		if(trigger.isUpdate)
			setDealPartId.add(newPayment.Id);
	}
	
	if(setDealPartId.size() > 0){
		List<Payments__c> lstPayments = [Select Id, RecordTypeId, Deal_Participation_Relationship__c from Payments__c where Deal_Participation_Relationship__c in:setDealPartId];
		for(Payments__c pay : lstPayments){
			mapPayment.put(pay.Deal_Participation_Relationship__c, pay);
		}
	}
	
	for (Deal_Participation__c newPayment: Trigger.New) {
		if(trigger.isUpdate){
			if (newPayment.Deal_Relationship__c != null) {
		
				if(newpayment.RecordTypeId == rtId){
					Payment__c payment = mapPayment.get(newPayment.Id);
					payment.RecordTypeId = PmntRecordType[0].id;
					//other fields if you want to set
					updatePayments.add(payment);
				}
			}
		}
	}
	if(insertPayments.size() > 0)
		insert insertPayments;
	if(updatePayments.size() > 0)
		update updatePayments;
}

 You can fix if any syntax error.

 

Let me know if you still have any problem.