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
ishchopraishchopra 

Trigger for Related List on Opportunity

Hello Experts,

I am writing a trigger to meet one of the objectives but couldn't get it working. This is a cross object updation. 

Scenario:

On an opportunity we have a related list called Asperato_Trasaction which is a credit card payment tool, when opportunities is closed won then payment can be collected within salesforce using this add on. What we wanted to do is copy the amount and payment status from this related list to opportunity. 

Opportunity Fields
Asperato Payment Status
Total Amount Paid

Asperato Fields
AP_Status__c
AP_Amount__c

the relation ship is not master detail between both objects.

Here is the code i have tried with my limited knowledge of Apex, currently i have tried to copy only one field i.e. payment status but ultimately i wanted to bring over both fields. Also, we want to fire the trigger when Asperato_Transaction (related list) occurs.

here is the code, this code doesnt give any erros but doesnt work either.. any help is much appreciated.
 
trigger UpdateAmount on Asperato_Transaction__c (after insert,after update)
{

 
set<id> ASSID = new set<id>();


    for(Asperato_Transaction__c ass : Trigger.new){
        if(ass.AP_Status__c == 'Paid'){
            ASSID.add(ass.AP_Opportunity__c);
        }
            
}
//create list of opportunities to iterate on
List<Opportunity> OppUpdateList = [SELECT Id, Asperato_Transaction_Status__c FROM Opportunity WHERE id in: ASSID];
    Map<id,opportunity> opMap = new map<id,opportunity>();

    for(opportunity o : OppUpdateList)
    
            {
                opMap.put(o.id,o);
            }
    for(Asperato_Transaction__c ACID : Trigger.new)
            {
                if(acid.AP_Status__c == 'Paid')
                    {
                        Opportunity thisop = opMap.get(ACID.AP_opportunity__c);
                        thisop.Asperato_Transaction_Status__c = ACID.AP_Status__c;
                    }
                    
               }                                    
            
            
}

 
Shikha AgashiShikha Agashi
Try this:

trigger UpdateAmount on Asperato_Transaction__c (after insert,after update)
{

 
set<id> ASSID = new set<id>();
  List<Opportunity> oppToUpdate=new List<Opportunity>(); 

    for(Asperato_Transaction__c ass : Trigger.new){
        if(ass.AP_Status__c == 'Paid'&& AP_opportunity__c!=null){
            ASSID.add(ass.AP_Opportunity__c);
        }
            
}
//create list of opportunities to iterate on
List<Opportunity> OppUpdateList = [SELECT Id, Asperato_Transaction_Status__c FROM Opportunity WHERE id in: ASSID];
    Map<id,opportunity> opMap = new map<id,opportunity>();

    for(opportunity o : OppUpdateList)
    
            {
                opMap.put(o.id,o);
    
        }
     
    for(Asperato_Transaction__c ACID : Trigger.new)
            {
                if(acid.AP_Status__c == 'Paid')
                    {
                        Opportunity thisop = opMap.get(ACID.AP_opportunity__c);
                        thisop.Asperato_Transaction_Status__c = ACID.AP_Status__c;
                        oppToUpdate.add(thisOp);
                    }
                    
               }               
            if(!oppToUpdate.isEmpty()){    
                update oppToUpdate;
            }
            
}