0

I have a requirement in which when I create a record in parent object(subject__c) that same record should be created in child(sub_Subject__c).
Also when subject__c is updated, all the changes that have been made in subject__c record should come in subSubject__c record.
I am doing this way. Is there any alternate method for doing this I don't think its a reliable method.

trigger Clone on Subject__c(after insert, after update) {
    if (Trigger.isInsert) {
        List<Subject__c> subjectList = [Select id, name From Subject__c WHere Id IN: trigger.new]
        List<sub_Subject__c> SubSubjectList = new List<sub_Subject__c>()
        for (Subject__c s: subjectList) {
            sub_Subject__c sb = new sub_Subject__c();
            sb.Subject__c = Subject__c.Id;
            sb.name = s.name;
            SubSubjectList.add(sb);
        }
        insert SubSubjectList;
    }
    if (Trigger.isUpdate) {
        List<Subject__c> subjectList = [Select id, name From Subject__c WHere Id IN: trigger.new]
        List<sub_Subject__c> SubSubjectList = new List<sub_Subject__c>()
        for (Subject__c s: subjectList) {
            for (sub_Subject__c sb: Subject__c.sub_Subjects__c) {
                sb.name = s.name;
                SubSubjectList.add(sb);
            }
        }
        update SubSubjectList;
    }
}
  • Why do you think trigger is not reliable ? – MIX DML Jul 7 '16 at 12:35
  • Hai Anzar, My code was help to you? – MSCB Jul 15 '16 at 6:43
2

This code is same as your code but its optimized than your code

/* Here I mention 
**    Parent --> Subject Record
**    Child --> subSubject Record
*/

trigger Clone on Subject__c(after insert, after update) {    // This trigger fires only After Insert and After Update

    Map<Id,Subject__c> subjectIdAndSubjectMap = new Map<Id,Subject__c>();  // Initialize the Map
    List<sub_Subject__c> newSubSubjectList = newList<sub_Subject__c>();    // Initialize the insert Child List
    List<sub_Subject__c> updateSubSubjectList = newList<sub_Subject__c>(); // Initialize the update Child List

    if ((trigger.isInsert || trigger.isUpdate) && trigger.isAfter) {

        for(Subject__c s: trigger.new) {    // trigger.new values

            if(trigger.isInsert) {    // Check isInsert
                sub_Subject__c sb = new sub_Subject__c();    // Initialize new Child Instance
                sb.Subject__c = s.Id;    // Lookup relation to the Parent Record
                sb.name = s.name;
                newSubSubjectList.add(sb);    // Add the insert List
            }

            if(trigger.isUpdate) {    // Check isUpdate
                subjectIdAndSubjectMap.put(s.Id, s);    // Put the Map value
            }
        }

        if(newSubSubjectList.size() > 0) {    // If newSubSubjectList size is '0' in this trigger fires in update Event
            insert newSubSubjectList;    // Insert Child records
        }

        if(subjectIdAndSubjectMap.size() > 0) {    // If subjectIdAndSubjectMap size is '0' in this trigger fires in insert Event

            for(sub_Subject__c sub : [SELECT Id,Name FROM sub_Subject__c WHERE Subject__c IN :subjectIdAndSubjectMap.keySet()]) {    // Query the child record using parent Id set 
                sub.Name = subjectIdAndSubjectMap.get(sub.sub_Subject__c).Name;    // If name changed in parent record its refledted in child record
                updateSubSubjectList.add(sub);    // Add the update list in child records
            }
        }

        if(updateSubSubjectList.size() > 0) {    //If updateSubSubjectList greater than zero to update the child record. 
            update updateSubSubjectList;
        }
    }
}
  • I appreciate your job here but It would be more helpful for every one if you provide comments also in the code and exactly specify what changes you have made.On side note it would be helpful for quickly gaining reputation as well ;) – Mr.Frodo Jul 7 '16 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.