0

I am new to apex and I have created an apex trigger that will create new residents whenever a city census(custom object) is approved. The created residents is based on the contacts object's details which are only a few fields and the resident record will depend on the salutation of the contact object (mr for male record type and ms for female record type).

There is also a master-detail relationship in the resident object, it is a lookup for the city census record. I am wondering if it is possible to get the specific id of that city census record and assign it to its child record which is the resident.

Here is the code:

trigger ResidentTrigger on City_Census__c (After update, Before insert) {
    List<Resident__c> residentData = new List<Resident__c>();
    
    Id maleRecordTypeId = Schema.SObjectType.Resident__c.getRecordTypeInfosByName().get('Male Resident').getRecordTypeId();
    Id femaleRecordTypeId = Schema.SObjectType.Resident__c.getRecordTypeInfosByName().get('Female Resident').getRecordTypeId();
    
    for(City_Census__c cityCensus: Trigger.new) {
        Resident__c newResident = new Resident__c();
        
        for(Contact con : [SELECT Id, Name, FirstName, LastName, City__c, Salutation FROM Contact WHERE City__c != NULL]) {
            
            if(cityCensus.Approval_Status__c == 'Approved') {
                if(cityCensus.City__c == con.City__c) {
                    if(con.Salutation == 'Mr.' || con.Salutation == 'Mr') {
                        newResident.Name = con.Name;
                        newResident.First_Name__c = con.FirstName;
                        newResident.Last_Name__c = con.LastName;
                        newResident.City__c = con.City__c;
                        newResident.City_Census__c = cityCensus.Id; //Lookup, master-detail relationship
                        newResident.RecordTypeId = maleRecordTypeId;
                    }
                    else if(con.Salutation == 'Ms.' || con.Salutation == 'Ms') {
                        newResident.Name = con.Name;
                        newResident.First_Name__c = con.FirstName;
                        newResident.Last_Name__c = con.LastName;
                        newResident.City__c = con.City__c;
                        newResident.City_Census__c = cityCensus.Id; //Lookup, master-detail relationship
                        newResident.RecordTypeId = femaleRecordTypeId;
                    }
                    
                    system.debug(newResident.Name);
                    system.debug(newResident.First_Name__c);
                    system.debug(newResident.Last_Name__c);
                    system.debug(newResident.City__c);
                    system.debug(newResident.RecordTypeId);
                    system.debug(newResident.City_Census__c);
                }
            }
        }
        insert residentData;
    }
}
1
  • 1
    What part of this isn't working? Are you getting any errors? Please edit your question and narrow down to the exact problem.
    – Nick Cook
    Feb 11, 2022 at 4:26

1 Answer 1

0

As far as I can see, your code works 'fine' when a City_Census__c is updated, but will fail when a City_Census__c is inserted.

The reason is the newly inserted City_Census__c does not have an Id yet, but will have it 'after insert'.

It is advisable to only create child records when an insert of the parent succeeds, which is 'after insert'.

Changing 'Before insert' to 'after insert' will fix your issue.

However, there are two other 'issues' with the trigger.

First is the trigger performs a select within a loop, so when more than 100 City_Census__c records are inserted or updated, this trigger will fail.

Second is the trigger selects ALL Contacts with a City__c. Potentially this will fail when the number of Contacts grows.

Advice:

  • Change the trigger from 'before' to 'after'
  • Filter the trigger records with Approval_Status__c == 'Approved', and put those in a Map (City => City Id)
  • Select Contact records to process
  • Update Contact records a current and set parent id to 'Map.get(City__c)'

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.