+ Start a Discussion
Hari nadh babu EluruHari nadh babu Eluru 

Trigger Apex - Phone number change

In this Trigger Apex,we have two custom sobjects Train__c and Passanger__c. At in Train__c there are two fields Passanger_Old_Phone_Number__c and Passanger_New_Phone_Number__c. At in Passanger__c there are one field Phone_Number__c. If the Phone number in Phone_Number__c was changed by Passanger in Passanger__c then old phone number  can be store in Passanger_Old_Phone_Number__c and new phone number can be store in Passanger_New_Phone_Number__c.
There are Lookup Relationship between Passanger__c to Train__c

We want functionality in Trigger Apex
Thank you !
 
Abdul KhatriAbdul Khatri
Hi Hari,

Here is the code with the test coverage.

I am little skeptical of the design here because train may have multiple passanger, as per this code the new phone number on train will be of the last passanger added. Can you please little explain the though behind this.

Trigger
trigger PassangerTrigger on Passanger__c (after insert, after update) {

    Map<Id, Passanger__c> idTrainPassangerMap = new Map<Id, Passanger__c>();
    List<Train__c> trainListToUpdate = new List<Train__c>();
    
    switch on Trigger.OperationType {
        
        when AFTER_INSERT
        {
            for(Passanger__c passanger : trigger.new)
            {
				if(passanger.Train__c != null)
                	idTrainPassangerMap.put(passanger.Train__c, passanger);
            }
        }
        When AFTER_UPDATE
        {
           for(Passanger__c passanger : trigger.new)
            {
				if(passanger.Train__c != null && passanger.Phone_Number__c != trigger.oldMap.get(passanger.Id).Phone_Number__c)
                	idTrainPassangerMap.put(passanger.Train__c, passanger);
            }            
        }       
    }
    
    if(!idTrainPassangerMap.isEmpty())
    {
        for(Train__c train : [SELECT Id, Passanger_New_Phone_Number__c, Passanger_Old_Phone_Number__c FROM Train__c WHERE Id = :idTrainPassangerMap.keySet()])
        {
            if(idTrainPassangerMap.get(train.Id) == null) continue;
            
            train.Passanger_Old_Phone_Number__c =  train.Passanger_New_Phone_Number__c;          
            train.Passanger_New_Phone_Number__c = idTrainPassangerMap.get(train.Id).Phone_Number__c;
            
            trainListToUpdate.add(train);
        }
        
        if(!trainListToUpdate.isEmpty())
            update trainListToUpdate;
    }
}

Test Class
@isTest
public class PassangerTrigger_Test {

    private static testMethod void test_AfterInsert() {
        
        Train__c train = new Train__c();
        insert train;
        
        Passanger__c passanger = new Passanger__c();
        passanger.Phone_Number__c = '3334445555';
        passanger.Train__c = train.Id;
        insert passanger;
        
        Train__c trainAfter = [SELECT Id, Passanger_New_Phone_Number__c, Passanger_Old_Phone_Number__c FROM Train__c WHERE Id = :train.Id];
        Passanger__c passangerAfter = [SELECT Id, Phone_Number__c FROM Passanger__c WHERE Id = :passanger.Id];
        system.assert(trainAfter.Passanger_Old_Phone_Number__c == null);
        system.assert(trainAfter.Passanger_New_Phone_Number__c == passangerAfter.Phone_Number__c);
        
    }
    
    private static testMethod void test_AfterUpdate() {
        
        Train__c train = new Train__c();
        insert train;
        
        Passanger__c passanger = new Passanger__c();
        passanger.Phone_Number__c = '3334445555';
        passanger.Train__c = train.Id;
        insert passanger;
        
        passanger.Phone_Number__c = '5554443333';
        update passanger;
        
        Train__c trainAfter = [SELECT Id, Passanger_New_Phone_Number__c, Passanger_Old_Phone_Number__c FROM Train__c WHERE Id = :train.Id];
        Passanger__c passangerAfter = [SELECT Id, Phone_Number__c FROM Passanger__c WHERE Id = :passanger.Id];
        system.assert(trainAfter.Passanger_Old_Phone_Number__c != null);
        system.assert(trainAfter.Passanger_New_Phone_Number__c == passangerAfter.Phone_Number__c);
        
    }    
}

I hope this trigger will help.
CharuDuttCharuDutt
Hii Hari
Try Below Code
trigger passengertrigger on Passenger__c (after insert,after update) {
    map<Id,String>NewPhoneSet = new map<Id,String>();
    map<Id,String>OldPhoneSet = new map<Id,String>();
    if(trigger.isBefore){
        if(Trigger.IsInsert && Trigger.IsUpdate){
            for(Passenger__c p: trigger.new){
                if(p.PhoneNumber != null && p.PhoneNumber != trigger.oldMap.get(p.Id).PhonenNumber){
                    NewPhoneSet.put(p.train__c,p.PhoneNumber);
                    OldPhoneSet.add(p.train__c,trigger.oldMap.get(p.Id).PhonenNumber);
                }
            } 
        }
    }
    list<Train__c>lstTrain = [Select Id,Passanger_Old_Phone_Number__c ,Passanger_New_Phone_Number__c from Train__c where Id IN:NewPhoneSet.keySet()];
    for(Train__c t : lstTrain){
        if(NewPhoneSet.containsKey(t.Id) && OldPhoneSet.containsKey(t.Id)){
            Passanger_Old_Phone_Number__c =OldPhoneSet.get(t.Id);
            Passanger_New_Phone_Number__c = NewPhoneSet.get(t.Id);
        }
    }
    if(lstTrain.size()>0){
        update lstTrain;
    }
    
}
Please Mark It As Best Answer If It Helps
Thank You!
Qadrf qazQadrf qaz
I also faced the same issue when I am trying to access the daily swertres hearing at (https://swertresonline.com/) . Is there anybody help me how to solve this problem.
Tara RootTara Root
Hey guys! “Will you deliver top-quality if I hire you to do my assignment?” Quality is our foremost priority, and our buy essay (https://buyessay.net/) platform have a competent team of editors and quality assurance agents to assure it. They will check your work thoroughly to ensure it has no mistakes, explores the topic conclusively, and meets all your requirements.