0

while creating child record need to get data from Grand parent. i wrote a trigger but not updating fields. here my working.

trigger Registration on Opportunity (after Update) {

    list<Registration__c> NewReg = new list<Registration__c>();
    list<Registration__c> oldReg = new list<Registration__c>();

    for(opportunity o : Trigger.new){
        if(o.Registration_Raised__c==true) {
            oldReg = [select id,Name from Registration__c where Opportunity__c=:o.id];
            if(oldReg.size()==0){
                registration__C reg = new registration__c();
                reg.Opportunity__c = o.id;
                reg.GHMC_Approved_Date__c = O.Unit_Number__r.Tower__r.GHMC_Approved_Date__C;
                NewReg.add(reg);
            }
        }       
    }
    if(NewReg.size()>0){
        insert NewReg;
    } 
}

i wrote the above trigger on opportunity and registration__C is the child object. when registraion raised check box is true the child record will create and need some inform Gran parent object. those are, Unit__C and tower__C.

  • It is not very clear what your problem is and more specifically what assistance you need from the community. Can you update your question to include both? – TSmith May 14 at 9:33
  • i am unable to get grandpartent data to the trigger. – Narender Reddy Singareddy May 14 at 9:55
  • What is the custom relationship type from Opportunity to Unit Number and from Unit Number to Tower? – TSmith May 14 at 10:00
  • We cannot access relationship fields in trigger, only parentID is accessible. You have to query for other relationship fields. – Navii May 14 at 10:00
  • 1
2

You need to prepare the data outside of the loop. Because:

  1. You mustn't perform a SOQL query inside a loop.
  2. You don't have access to related parents from the records in the trigger.

You'll also want to run the trigger only when Registration Raised has actually changed to true, not anytime an opportunity is edited and its checkbox happens to already be true.

Fixed code:

trigger Registration on Opportunity (after Update) {

    list<Registration__c> NewReg = new list<Registration__c>();
    Map<Id, Opportunity> map_Opps = new Map<Id, Oppportunity>([SELECT Id, Unit_Number__r.Tower__r.GHMC_Approved_Date__C,
                                                                      (Select Id
                                                                       From Registrations__r)
                                                               FROM Opportunity
                                                               WHERE Id in :Trigger.new]);

    for(opportunity o : Trigger.new){
        if(o.Registration_Raised__c==true && Trigger.oldMap.get(o.Id).Registration_Raised__c==false) {
            if(map_Opps.get(o.Id).Registrations__r.isEmpty()){
                registration__C reg = new registration__c();
                reg.Opportunity__c = o.id;
                reg.GHMC_Approved_Date__c = map_Opps.get(o.Id).Unit_Number__r.Tower__r.GHMC_Approved_Date__C;
                NewReg.add(reg);
            }
        }       
    }
    insert NewReg;
}
| improve this answer | |
  • if(NewReg.size()>0){ is not necessary in modern code. – sfdcfox May 14 at 16:29
  • Thanks, updated. – EranV May 14 at 16:37
  • Thanks ErnaV. Its working. – Narender Reddy Singareddy May 15 at 5:22

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