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
Padmini S 26Padmini S 26 

Apex Class and Trigger in Salesforce

Hi All,

I am new to salesforce triggers.  I have two object Doctor__c and Patient__c objects. One custom field Doctor_Code__c on Docor__c object and another custom field on Patient__C object. Patient object has lookup field Doctor__c. If Patient Doctor_Code__c equal to Doctors Doctor_Code__c then Doctor__C field should populate with Doctor Name on Patient Object. For this I have written the Apex class and calling the Apex Class in trigger. But I am not getting any field Update. Below is the Code.

Apex Class:
------------------
public Class UpdateDoctor
{
  public void updatedoctor1 (List<Doctor__C> doctors)
     {
         map<string, Doctor__C> ObjMap = new map<string, Doctor__C>();
         for(Doctor__C obj: doctors)
         {
              if (obj.Doctor_Code__C!= Null)
        {
            ObjMap.put(obj.Doctor_Code__C, obj);
        }
    }
      List<Patient__c> cases = [SELECT Id, Doctor_Code__c, Doctor__c FROM Patient__c WHERE Doctor_Code__c IN :ObjMap.KeySet()];
    List<Patient__c> caseUpdateList = new List<Patient__c>();
     for(Patient__c c: cases)
    {
        Doctor__C  obj = ObjMap.get(c.Doctor_Code__c);
        c.Doctor__C= obj.Id;
         caseUpdateList.add(c);
    }
     }
}

Trigger
----------
trigger UpdateDoctortrigger  on Doctor__c( before insert,before update) {
    list<Doctor__c> doctors = trigger.new;
    UpdateDoctor my = new UpdateDoctor();
    my.updatedoctor1 (doctors);
  }

Please help on this issue.

Thanks in Advance.
Niraj Kumar 9Niraj Kumar 9
Hi padmini,

Please change your trigger, here you are not calling the class.
trigger UpdateDoctortrigger  on Doctor__c( before insert,before update) {
    list<Doctor__c> doctors = trigger.new;
    UpdateDoctor my = new UpdateDoctor();
    UpdateDoctor.updatedoctor1 (doctors);
  }

Thanks
niraj
Amit Chaudhary 8Amit Chaudhary 8
Please try to update your apex class like below
public Class UpdateDoctor
{
  public void updatedoctor1 (List<Doctor__C> doctors)
     {
			map<string, Doctor__C> ObjMap = new map<string, Doctor__C>();
			for(Doctor__C obj: doctors)
			{
				if (obj.Doctor_Code__C!= Null)
				{
					ObjMap.put(obj.Doctor_Code__C, obj);
				}
			}
			List<Patient__c> cases = [SELECT Id, Doctor_Code__c, Doctor__c FROM Patient__c WHERE Doctor_Code__c IN :ObjMap.KeySet()];
			List<Patient__c> caseUpdateList = new List<Patient__c>();
			
			for(Patient__c c: cases)
			{
				Doctor__C  obj = ObjMap.get(c.Doctor_Code__c);
				c.Doctor__C= obj.Id;
				caseUpdateList.add(c);
			}
			if(caseUpdateList.size() > 0 )
			{
				update caseUpdateList;
			}
     }
}
Let us know if this will help you

Thanks
AMit Chaudhary
 
Padmini S 26Padmini S 26
Hi Amit and Niraj,

Thanks for your reply.

I have tried with both solutions. still, it is not working . 


 
ManojjenaManojjena
HI Padmini,

Try with below code it will help !!
Insert event is not required in this case what I think .

What I understood is you when your doctor code will update in Doctor object then related patinet's doctor code needs to update .
 
trigger UpdateDoctortrigger  on Doctor__c( after update) {
   if( Trigger.isAfter && Trigger.isUpdate){
      UpdateDoctor.updateDoctorCode(Trigger.new,Trigger.oldMap);  
   }
}

public Class UpdateDoctor{ 
    public  static void updateDoctorCode (List<Doctor__c> docList,Map<Id,Doctor__c> docOldMap){
		 Map<id,Doctor__c> modiFiedDocMap=new Map<Id,Doctor__c>();
	    for(Doctor__c doc : docList){
			if(doc.Doctor_Code__c != docOldMap.get(doc.Id).Doctor_Code__c ){
				modiFiedDocMap.put(doc.Id,doc);
			}
		}
		List<Patient__c> patientListToUpdate = new List<Patient__c>();
		if(modiFiedDocMap.isEmpty()){
		   for( Patient__c pat: [SELECT Id, Doctor_Code__c, Doctor__c FROM Patient__c WHERE Doctor_Code__c IN :modiFiedDocMap.keySet() ]){
		        if(modiFiedDocMap.get(pat.Doctor__c) != null){
			      pat.Doctor_Code__c=modiFiedDocMap.get(pat.Doctor__c).Doctor_Code__c;
				   patientListToUpdate.add(pat);
			    }
		    }
		}
		if(patientListToUpdate.isEmpty()){
		   try{
		      update patientListToUpdate;
		   }catch(DmlException de ){
		      System.debug(de);
		   }
		}
    }
}

Let us know still you have issue .
Thanks 
Manoj
Padmini S 26Padmini S 26
Hi Manoj,

Thank you for your reply.

There is no any relationship between Doctor__c and Patient__c Object. If Doctor_c object Doctor_Code_c field equals to Patient_c Object Doctor_Code_c then Doctor_c field which is on Patient_c object should populate with Doctor__c Object Name field.



 
ManojjenaManojjena
HI Padmini,
Try with below code .

Let us know if you need only on update or on creation of doctor record as well ,
 
trigger UpdateDoctortrigger  on Doctor__c( After insert,after update) {
    if( Trigger.isAfter){
		if(Trigger.isUpdate){
			UpdateDoctor.updateDoctorCode(Trigger.new,Trigger.oldMap);  
		}
    }
}

public Class UpdateDoctor{ 
    public  static void updateDoctorCode (List<Doctor__c> docList,Map<Id,Doctor__c> docOldMap){
		 Map<String,Doctor__c> modiFiedDocMap=new Map<String,Doctor__c>();
		 Set<String> doctorCodeSet=new Set<String>();
	    for(Doctor__c doc : docList){
			if(doc.Doctor_Code__c != docOldMap.get(doc.Id).Doctor_Code__c ){
				modiFiedDocMap.put(doc.Doctor_Code__c,doc);
			}
		}
		List<Patient__c> patientListToUpdate = new List<Patient__c>();
		if(modiFiedDocMap.isEmpty()){
		   for( Patient__c pat: [SELECT Id, Doctor_Code__c, Doctor__c FROM Patient__c WHERE Doctor_Code__c IN : modiFiedDocMap.keySet() ]){
		        if(modiFiedDocMap.get(pat.Doctor_Code__c) != null){
			      pat.Doctor__c=modiFiedDocMap.get(pat.Doctor_Code__c).Name;
				   patientListToUpdate.add(pat);
			    }
		    }
		}
		if(patientListToUpdate.isEmpty()){
		   try{
		      update patientListToUpdate;
		   }catch(DmlException de ){
		      System.debug(de);
		   }
		}
    }
}