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
sandy hellosandy hello 

Using before update apex trigger how to retrieve values from other object "B" based on year and service type fields on object "A" and update revenue value on Object "A"

I want to retreive value revenue from Object "B" based on input values year and servicetype fields which or common on both objects "A"  & "B" and there is no relationship between both objects, and update revenue value on Object "A"

eg: Year 2020, servicetype - gold which are common on both objects   
SFDC12SFDC12
Hi sandy ,based on your requirement  you can try below code by modifying ur objects.

trigger updaterecords on Contact (before insert, before update, after insert, after update) {

List<Contact> conList =new List<Contact>();
    Set<Id> setid = new  Set<Id>();
  
    if(trigger.isBefore){
        system.debug('trigger before event');
        conList = trigger.new;
      
    }else if(trigger.isAfter){
         conList=trigger.new;
        for(Contact con:conList){
           setid.add(con.AccountId);
        }
        system.debug('setid ' + setid);
      
        List<Account> accList = [Select Id, Name, email__c From Account  Where Id=:setid];     
     
        if(trigger.isInsert){                   
          
             for(Contact c1:trigger.new){
                 for(Account a1:accList){
                     a1.email__c= c1.email;
                     update a1;
                 }
              }   
          
        }else if(trigger.isupdate){
            for(Contact c2:trigger.new){
                for(Account a2:accList){
                    a2.email__c= c2.email;
                    update a2;
                }
            }           
        }      
       
    }
}

Thanks 
A.Anshi.
AbhinavAbhinav (Salesforce Developers) 
Iterate Object A Trigger.old or Trigger.New Value-------> Store common field value in a list-----> Query on object B with common field in where clause with value stored in list.