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
Alex GroagAlex Groag 

Trigger to Update related Recurring Donations when an Opportunity is edited or Created

Hi - I'm trying to write a trigger that will update Recurring Donations when a related opportunity is created or edited.  We need to map encrypted credit card fields on the opportunity to encrypted fields on the Recurring Donation.  I'm a novice when it comes to trigger/apex and have been trying to repurpose from other people's post but I'm not having any luck.  Below is the starting point using test checkbox fields.  If we can get this to work then I can modify it for more fields.  In the developer console I'm not getting any errors - but when I edit an Opportunity the Recurring Donation is not updating. 

Here is the code I was using. 

trigger TestRD1 on Opportunity (after insert, after update) {
    List<Opportunity> opps = new List<Opportunity>(); 
    List<npe03__Recurring_Donation__c> RDs = new List<npe03__Recurring_Donation__c>();
    
    
    for (Opportunity opp : Trigger.new) {
        opps = [SELECT Id, Test_Checkbox__c FROM Opportunity LIMIT 1];
        RDs = [SELECT Id, Test_Checkbox__c FROM npe03__Recurring_Donation__c WHERE Id =: opp.npe03__Recurring_Donation__c];
    }

    for( Opportunity O : opps) {
    for (npe03__Recurring_Donation__c RD: RDs){
        if(O.Test_Checkbox__c== true ){
            RD.Test_Checkbox__c=true;
        }
            
    }
        update RDs;
    }} 
Best Answer chosen by Alex Groag
EldonEldon
Try the below code,
 
trigger TestRD1 on Opportunity (after insert, after update) {

    List<Opportunity> opps = new List<Opportunity>(); 
    List<npe03__Recurring_Donation__c> RDs = new List<npe03__Recurring_Donation__c>();
    List<id> RDIds = new list<id>();
	
    for (Opportunity opp : Trigger.new) {
		 if(Opp.Test_Checkbox__c== true ){
			RDIds.add(opp.npe03__Recurring_Donation__c);
		 }
	}
	
	RDs = [SELECT Id, Test_Checkbox__c FROM npe03__Recurring_Donation__c 
		  WHERE Id in RDids];
		  
	for(npe03__Recurring_Donation__c r : RDs){
		r.Test_Checkbox__c = true;
	}
	
	update RDs;
}

PS: Try to avoid SOQL and DML statements inside loops as it will break when the governor limit reaches.  
Refer here : https://developer.salesforce.com/page/Apex_Code_Best_Practices

Regards

 

All Answers

EldonEldon
Try the below code,
 
trigger TestRD1 on Opportunity (after insert, after update) {

    List<Opportunity> opps = new List<Opportunity>(); 
    List<npe03__Recurring_Donation__c> RDs = new List<npe03__Recurring_Donation__c>();
    List<id> RDIds = new list<id>();
	
    for (Opportunity opp : Trigger.new) {
		 if(Opp.Test_Checkbox__c== true ){
			RDIds.add(opp.npe03__Recurring_Donation__c);
		 }
	}
	
	RDs = [SELECT Id, Test_Checkbox__c FROM npe03__Recurring_Donation__c 
		  WHERE Id in RDids];
		  
	for(npe03__Recurring_Donation__c r : RDs){
		r.Test_Checkbox__c = true;
	}
	
	update RDs;
}

PS: Try to avoid SOQL and DML statements inside loops as it will break when the governor limit reaches.  
Refer here : https://developer.salesforce.com/page/Apex_Code_Best_Practices

Regards

 
This was selected as the best answer
Alex GroagAlex Groag
Hi Thanks for the response - this is great.  How could we adjust the code so instead of Opportunity Test Checkbox = True, if an encrypted credit card field has a value, copy the Opportunity Encrypted field to the Recurring Donation encrypted field?

If Opportunity Encrypted field ChargentSFA__Card_Number__c != Null, then copy field value to Recurring Donation Card_Number__c.

Thanks, really appreciate it.