You need to sign in to do that
Don't have an account?
neckr
Help with Trigger - Link child id to parent Id with DML Insert outside of loop
Hi,
I have been revising my trigger code as I had DML Insert statements inside my loop. I created a list and brought the DML statements outside of loop, but have ran into another issue as a result. I need to create a contact and link the contact to the Bankruptcy_Judgements_Liens_Report__r custom object through the BJVLAgencyContact.ID lookup field. When I insert outside of the loop, the actual contact ID is not captured since its not inserted.
trigger ConVerificationRecordSetup on Con_Service_Task_Request__c (after insert, after update) { Set<ID> accID = new Set<ID>(); //map to contain Con_Service_Task_Request__c record's id as key and it's master Account record's ID as value Map<ID, ID> mapST_Account = new Map<ID, ID>(); Map<ID, ID> mapPrincipalAcctCont = new Map<ID, ID>(); Map<ID, List<Trade_Reference_Report__c>> mapExistingTRVs = new Map<ID, List<Trade_Reference_Report__c>>(); List <Contact> listBJLVAgencyContact = new List<Contact>(); List <Bankruptcy_Judgements_Liens_Report__c> listBJLV = new for(Con_Service_Task_Request__c ST : trigger.new){ //This "Con_Service_Task_Request__c" should be API Name of Master Detail relationship with the Account accID.add(ST.Account__c); mapST_Account.put(ST.id , ST.Account__c); Contact Principalcontact=[SELECT ID FROM Contact WHERE AccountId = : ST.Account__c AND Contact_Type__c = 'Acct Principal Contact' limit 1]; mapPrincipalAcctCont.put(ST.id, Principalcontact.ID); List<Trade_Reference_Report__c> ExistingTRVs = [SELECT ID FROM Trade_Reference_Report__c WHERE Account__c = : ST.Account__c AND TRV_Work_Category__c = : ST.Category__c limit 3]; mapExistingTRVs.put(ST.id, ExistingTRVs); } Map<Id,List<Account>> records = new Map<Id,List<Account>>(); for(Account a : [SELECT id , (select id from Bankruptcy_Judgements_Liens_Report__r) FROM Account WHERE id in : accID]) { if(!records.containsKey(a.id)) { records.put(a.id , new List<Account>()); } records.get(a.id).add(a); } for (Con_Service_Task_Request__c ST : Trigger.new) { //INSERT NEW BJLV RECORD IF REQUIRED AND DOES NOT YET EXIST if (records.get(mapST_Account.get(ST.id)).get(0).Bankruptcy_Judgements_Liens_Report__r.Size() == 0) Contact BJLVAgencyContact = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'BJLV Agency', LastName = 'Enter Last Name'); listBJLVAgencyContact.add(BJLVAgencyContact); Bankruptcy_Judgements_Liens_Report__c BJLV = new Bankruptcy_Judgements_Liens_Report__c(Account__c = ST.Account__c , BJLV_Agency_Contact__c = BJLVAgencyContact.ID ); //BJVLAgencyContact.ID does not exist at this stage since Insert //outside of the loop now listBJLV.add(BJLV); } } insert listBJLVAgencyContact; insert listBJLV; //Does not link to Contact on Insert. How would I get it to link? }
This is not right way to use DML statements. You should not put DMLs under a loop.
Use this instead
This will solve your issue.
All Answers
Try this
I tried to expand your code to my other custom object that is very similiar, only difference is it has a Master Detail Lookup field to Con_Service_Task_Request__c. I tried to use Trigger.new[i].id to link the objects and I receive an error:
ConVerificationRecordSetup: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ConVerificationRecordSetup: maximum trigger depth exceeded Con_Service_Task_Request trigger event AfterUpdate for [a0TT0000002jnZQ]
Is there something I am missing?
Something is there in customer_reference_report__c which is causing your this trigger to fire again and again. Reason might be:
- Con_Service_Task_Request__c has a rollup summary field on customer_reference_report__c.
- customer_reference_report__c has an insert trigger which again calls the trigger of Con_Service_Task_Request__c.
Check out whether you have above given scenario.
Yes I have two roll up summary fields in Con_Service_Task_Request__c that summarize customer_reference_report__c. The roll up field is used to check the the total number of references and the total number of passed reference checks
I do not have another Trigger in customer_reference_report__c
My Roll Up Summaries in Con_Service_Task_Request__c are below.
Do I need to remove these for my trigger to work?
Roll-Up Summary (COUNT Customer Reference Report)
Declare a class
Now use this static variable in your trigger
Just to clarify, I have to wrap this around the insert listCRV1; ?
Thank you for your help!
Your Class and condition allows me to process with no errors. However for some reason when I try to insert multiple records at once, only the first ST creates the desired output of creating CRVContact1, CRVContact2, and CRVContact3 and its associated CRV1, CRV2 and CRV3. I copied my entire Trigger code because I have no clue why this is happening and can't tell you if the Class and condition I added is the cause since it wasn't processing at all before.
Can you tell me the way, how you are inserting multiple records at once?
Through method in controller from VF page. I am selecting multiple Service Tasks and processing all at once.
This is not right way to use DML statements. You should not put DMLs under a loop.
Use this instead
This will solve your issue.