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
harish reddy 39harish reddy 39 

Updateing field in before Insert Trigger..?

Hi,
    I have 2 Custom objects(Reservations__c And HotelGusts__c) both objects have custom fields Email__c  and have lookup from reservationsc to Hotelguests__c .Now i need to write a trigger which updates Reservations__c's Email Field with hotelguests__c's Email Field.Can any one help me with that.
Thanks in Advance.

My trigger Code:
trigger UpdateemailTrigger on Reservations__c (After insert) {

         Reservations__c  resv = New Reservations__c (); 
List<Id> ids = New List<Id>();
         for (Reservations__c hrs:trigger.new)
            {
                 ids.add(hrs.HotelGuest__c);
            }
            
            List<Reservations__c>  Revs1 = [Select Id ,Email__c from Reservations__c Where Id IN : ids];
         HotelGuest__c Gust1 = New HotelGuest__c();
       for (Reservations__c hrs:Revs1 )
         {
            
     
            hrs.Email__c = Gust1.Email__c;
             
          Update Revs1 ;
         }     
      
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Harish,

You can do this via process builder.

Let's say one Reservations__c record has 5 HotelGusts__c records and all of these HotelGusts__c records has Email addres. Now. which email should updates Reservations__c's Email Field with hotelguests__c's Email Field?
harish reddy 39harish reddy 39
Hi harshil,
Thanks for your reply
when ever hotelguest make a reservation email field in reservation__c should be updated with hotelguest  email.
 
HARSHIL U PARIKHHARSHIL U PARIKH
Alright Harish. Here we go,

Trigger Code:
 
Trigger UpdateemailTrigger On HotelGusts__c(After Insert, After Update){
    
    List<Reservations__c> parentIds = New List<Reservations__c>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate){
        For(HotelGusts__c guest : Trigger.New){
           If(guest.Reservations__c != Null && guest.Email__c != Null)
           {
               parentIds.add(guest.Reservations__c);
           }
        }
    }
    
    List<Reservations__c> finalParentList = New List<Reservations__c>();
    
    For(Reservations__c EveryReser : [Select Id, Email__c, 
                                        (Select Id, Email__c FROM HotelGusts__r ORDER BY CreatedDate DESC LIMIT 1)
                                                                FROM Reservations__c WHERE Id =: parentIds ])
    {
        String email = '';
        email = String.ValueOf(EveryReser.HotelGusts__r.Email__c);
        
        EveryReser.Email__c = email;
        
        finalParentList.add(EveryReser);
    }
    try{
        If(!finalParentList.IsEmpty()){
            update finalParentList;
        }
    }
    Catch(Exception e){
        system.debug('Thrown Error For UpdateemailTrigger Is:: ' + e.getMessage());
    }
     
}

If it solves the query then please mark it as Best Answer since it will help other users as well!