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
Tanvi KakkarTanvi Kakkar 

trigger for field update on unrelated objects

I have two objects parent__c and Teacher__c . There is no relationship between them .no materdetail no lookup . 
only name fields in both parent__c and teacher__c will be treated as common , because we will manual enter the name field . 
Parent fields: name(text )
, teacher_expertise (text)
Teacher Name (text)
Teacher fields :
- name (text)
expertise (picklist) 
Teacher_name( Text Fields)
I would like to update teacher_expertise and Teacher_name  field on parent objects :
Once we update fields on Teacher__c it should update Parent__c.
how can we use maps in this . 
Please help . Urgent
Thanks 
Best Answer chosen by Tanvi Kakkar
Terence VibanTerence Viban
Hi Tanvi,
I already typed my solution half-way through when I realised you cannot do this directly. You will most definitely need a lookup relationship bewteen Parent__c and Teacher__c. The reason for this is that you have to build maps with a key (that should at best be unique) so you can use the map to get parent records to update when updating Teacher__c records e.g. 

Map<String, Parent__c> parentData = new Map<String, Parent__c>();

So now what will you use as the key to the Map above? You can't use the Teacher Name because it may change when the Teacher record is updated. So you need a lookup on the Teacher record which points to the parent. Or you need to fake this if for some reason you do not or cannot add a lookup on the Teacher object to point to the parent.

So how do you fake it?

You could create a field on the parent object and put the Teacher record Id in this field. What this means is that, i am assuming whenever you create a teacher record, you create a parent and copy the name, expertise and the Id into the relevant fields.

This said you could then do the following;

for(Parent__c p : [SELECT Teacher_Name__c, Teacher_Expertise__c, TeacherId FROM Parent__c]){
     parentData.put(TeacherId, p);
}

2.) Go over the list of Teacher records you are updating and update the parent records, preferable in an after insert/update trigger on the Teacher__c object. Use a list to collect all parent records you will like to update. 
List<Parent__c> parentRecordsToUpdate = new List<Parent__c>();
for(Teacher__c t : teacherRecords){
    if(parentData.get(t.TeacherId) != null){
       parentData.get(t.TeacherId).Teacher_Name__c = t.Teacher_Name__c;
       parentData.get(t.TeacherId).Teacher_Expertise__c = t.Teacher_Expertise__c;
       parentRecordsToUpdate.add(parentData.get(t.TeacherId));
    }
}

try{
    update parentRecordsToUpdate;
} catch(Exception e){
    System.debug('An error occured while updating the parents');
}

Hope this comes close to what you want. If not I hope you get some ideas on how to proceed.

Cheers

All Answers

Sai Ram ASai Ram A
Hi Tanvi Kakkar

Using Lightning Process Builder you can achieve the Expected functionality, its an awesome Feature in Salesforce
Similar post : https://developer.salesforce.com/forums/?id=906F0000000B007IAC

Thank you
BLearn - Sai
Terence VibanTerence Viban
Hi Tanvi,
I already typed my solution half-way through when I realised you cannot do this directly. You will most definitely need a lookup relationship bewteen Parent__c and Teacher__c. The reason for this is that you have to build maps with a key (that should at best be unique) so you can use the map to get parent records to update when updating Teacher__c records e.g. 

Map<String, Parent__c> parentData = new Map<String, Parent__c>();

So now what will you use as the key to the Map above? You can't use the Teacher Name because it may change when the Teacher record is updated. So you need a lookup on the Teacher record which points to the parent. Or you need to fake this if for some reason you do not or cannot add a lookup on the Teacher object to point to the parent.

So how do you fake it?

You could create a field on the parent object and put the Teacher record Id in this field. What this means is that, i am assuming whenever you create a teacher record, you create a parent and copy the name, expertise and the Id into the relevant fields.

This said you could then do the following;

for(Parent__c p : [SELECT Teacher_Name__c, Teacher_Expertise__c, TeacherId FROM Parent__c]){
     parentData.put(TeacherId, p);
}

2.) Go over the list of Teacher records you are updating and update the parent records, preferable in an after insert/update trigger on the Teacher__c object. Use a list to collect all parent records you will like to update. 
List<Parent__c> parentRecordsToUpdate = new List<Parent__c>();
for(Teacher__c t : teacherRecords){
    if(parentData.get(t.TeacherId) != null){
       parentData.get(t.TeacherId).Teacher_Name__c = t.Teacher_Name__c;
       parentData.get(t.TeacherId).Teacher_Expertise__c = t.Teacher_Expertise__c;
       parentRecordsToUpdate.add(parentData.get(t.TeacherId));
    }
}

try{
    update parentRecordsToUpdate;
} catch(Exception e){
    System.debug('An error occured while updating the parents');
}

Hope this comes close to what you want. If not I hope you get some ideas on how to proceed.

Cheers
This was selected as the best answer
Tanvi KakkarTanvi Kakkar
Hi  Terence

Thanks for the logic . It worked for me :) . However , I did it in a slight diff way .. I have one question for you.
why we need list to add these records??
parentRecordsToUpdate.add(parentData.get(t.TeacherId));
We can directly update maps too ... Cant we ?/

Thanks
 
Terence VibanTerence Viban
Hi Tanvi,
you can update a map directly by doing somthing like 
   
try{
    update  parentData.values();
} catch(Exception e){
    System.debug('An error occured while updating the parents');
}

The reason why I personally used a List here, is because I do not need to update all parent values. In my example above, I get all parent records, loop through them, modify some and then put the ones that I have modified in a list. It is this List that i update. Imagine I have 1000 parent records and I am updating a single Teacher record via the UI. I will run my DML update on a list with a single Parent record rather than all 1000 records. I think you see where I am going with this. The trigger should be written in such a way that it will perfom optimal (performant) no matter if you are updating a single record via the UI or loading a bunch of records via the Data Loader or some other tool.

I know you said you already solved your issue, but I just thought of an even better and more efficient way you could tackle this problem without having to add an extra field. You could write your trigger to also use the Trigger.oldMap for the Teacher's object. And use this map to retrieve only Parent objects to be modified. When building the map Map<String, Parent__c>, the key could then be the Id of the Teacher record, which will come from the oldMap. The rest of the solution basically stays the same. This is a way more efficient solution.

Happy I could be of help

Terence