You need to sign in to do that
Don't have an account?
Help with Trigger Code Syntax - Check Size of Multiple Related Objects
Hi I pulled together the code below with some help on the discussion boards and some examples. I am having trouble with the syntax to get my code to compile.
Con_Service_Task_Request__c is a child object of Account and the relationship name is Service_Tasks__r. Account is a master detail lookup field in Con_Service_Task_Request Object, same goes for all my_custom_Object's referenced below.
In short when I enter a new Con_Service_Task_Request__c record, I want to check the size of several of the Account Related Custom Objects. If the size is zero (i.e no records) I want to create a new record in the corresponding Custom Object.
Any suggestions on how I can achieve my objective?
trigger ConVerificationRecordSetup on Con_Service_Task_Request__c (after insert, after update) { Map<Id,List<Account>> records = new Map<Id,Account>(); for(Account a:[SELECT id, Con_Service_Task_Request__c, (select id from My_Custom_Object1__r), (select id from My_Custom_Object2__r), (select id from My_Custom_Object3__r), (select id from My_Custom_Object4__r), (select id from My_Custom_Object5__r), (select id from My_Custom_Object6__r), (select id from My_Custom_Object7__r), (select id from My_Custom_Object8__r), FROM Account WHERE Con_Service_Task_Request__c in :trigger.new]) { if(records.get(a.Con_Service_Task_Request__c)==null) { records.put(a.Con_Service_Task_Request__c, new List<Account>()); } records.get(a.Con_Service_Task_Request__c).add(a); } for (Con_Service_Task_Request__c ST : Trigger.new) { if (records.get(ST.id)[index].My_Custom_Object1__r.Size() == 0 ) { //Insert New Record Code Here } if (records.get(ST.id)[index].My_Custom_Object2__r.Size() == 0 ) { //Insert New Record Code Here } if (records.get(ST.id)[index].My_Custom_Object3__r.Size() == 0 ) { //Insert New Record Code Here } // Continue through to My_Custom_Object8__r }
Thank you very much! Works great! I posted the working code for others .
All Answers
<div>
Hi neckr,
Try this out, if you still have problem please ask.
Also check this for optimizing trigger : http://forceschool.blogspot.com/
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.Con_Service_Task_Request__c); mapST_Account.put(ST.id , ST.Con_Service_Task_Request__c); }
Map<Id,List<Account>> records = new Map<Id,Account>();for(Account a : [SELECT id , (select id from My_Custom_Object1__r), (select id from My_Custom_Object2__r), (select id from My_Custom_Object3__r), (select id from My_Custom_Object4__r), (select id from My_Custom_Object5__r), (select id from My_Custom_Object6__r), (select id from My_Custom_Object7__r), (select id from My_Custom_Object8__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) {
if (records.get(mapST_Account.get(ST.id)).My_Custom_Object1__r.Size() == 0 ) {
//Insert New Record Code Here }
if (records.get(mapST_Account.get(ST.id)).My_Custom_Object2__r.Size() == 0 ) {
//Insert New Record Code Here } if (records.get(mapST_Account.get(ST.id)).My_Custom_Object3__r.Size() == 0 ) {
//Insert New Record Code Here
}// Continue through to My_Custom_Object8__r }
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 valueMap<ID, ID> mapST_Account = new Map<ID, ID>();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.Con_Service_Task_Request__c); mapST_Account.put(ST.id , ST.Con_Service_Task_Request__c); }Map<Id,List<Account>> records = new Map<Id,Account>();for(Account a : [SELECT id , (select id from My_Custom_Object1__r), (select id from My_Custom_Object2__r), (select id from My_Custom_Object3__r), (select id from My_Custom_Object4__r), (select id from My_Custom_Object5__r), (select id from My_Custom_Object6__r), (select id from My_Custom_Object7__r), (select id from My_Custom_Object8__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) {if (records.get(mapST_Account.get(ST.id)).My_Custom_Object1__r.Size() == 0 ) { //Insert New Record Code Here }if (records.get(mapST_Account.get(ST.id)).My_Custom_Object2__r.Size() == 0 ) { //Insert New Record Code Here } if (records.get(mapST_Account.get(ST.id)).My_Custom_Object3__r.Size() == 0 ) { //Insert New Record Code Here }// Continue through to My_Custom_Object8__r }
</div>
Hi neckr,
Sorry, copied trigger twice , take only first part
Also check this for optimizing trigger : http://forceschool.blogspot.com/
Shashikant,
Thank you for your help. I tried to compile your code and received the following error noted in red below, any suggestions?
Hi ,
Sorry for the errors , actualy I wrote this on notepad so could not compile before suggesting you.
Change this
Change is records.get(mapST_Account.get(ST.id)) returns you a list of account so we need to fetch the first item of that
before feting relationship. So we need to use records.get(mapST_Account.get(ST.id)).get(0)
do similar change to all other objects.
Thank you very much! Works great! I posted the working code for others .
I complied the full code for my trigger. It runs fine the first time I add a Con_Service_Task Record and when I attempt to run again it does not insert my Con_Service_Task Record and sends me an email with the following error.
Apex script unhandled trigger exception by user/organization: 005A0000001VzT0/00DT0000000Jq5H Source organization: 00DA0000000Abte (null)
ConVerificationRecordSetup: execution of AfterInsert
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 AfterInsert for [a0TT0000002jmOW]
Here is my full code, not sure where to do next to get this running, and would appreciate any gudiance.
1) Try to avoid queries inside loop : Use map to avoid the same
2) Never use DML statements inside a loop : Collect objects in list and then perform DML statement to that List outside the FOR LOOP
Thanks for the for the feedback. I made some progress by taking the queries outside of the loop. I also created the DML statements outside of loop as well but have an issue as a result. I need to create a contact and link the contact to a custom object. When I insert outside of the loop, the actual contact ID is not captured since its not inserted. Red highlights may make my problem clear. Any suggestions on logic to handle this?
Thanks for the for the feedback. I made some progress by taking the queries outside of the loop. I also created the DML statements outside of loop as well but have an issue as a result. I need to create a contact and link the contact to a custom object. When I insert outside of the loop, the actual contact ID is not captured since its not inserted. Red highlights may make my problem clear. Any suggestions on logic to handle this?