You need to sign in to do that
Don't have an account?

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.
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.
Please change your trigger, here you are not calling the class.
Thanks
niraj
Thanks
AMit Chaudhary
Thanks for your reply.
I have tried with both solutions. still, it is not working .
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 .
Let us know still you have issue .
Thanks
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.
Try with below code .
Let us know if you need only on update or on creation of doctor record as well ,