function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
neckrneckr 

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
 
}

 

Best Answer chosen by Admin (Salesforce Developers) 
neckrneckr

Thank you very much!  Works great!  I posted the working code for others .

rigger 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>();
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,List<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)).get(0).My_Custom_Object1__r.Size()  == 0 ) { 
 //Insert New Record Code Here     
}
if (records.get(mapST_Account.get(ST.id)).get(0).My_Custom_Object2__r.Size()  == 0 ) {
 //Insert New Record Code Here   
}
 
if (records.get(mapST_Account.get(ST.id)).get(0).My_Custom_Object3__r.Size()  == 0 ) {
 //Insert New Record Code Here   
}
// Continue through to My_Custom_Object8__r
 
}

 

 

 

All Answers

Shashikant SharmaShashikant Sharma

 

<div>

Hi neckr,

 

Try this out, if you still have problem please ask.

 

Also check this for optimizing trigger :  http://forceschool.blogspot.com/

 

 

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>();
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 }

 

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>

Shashikant SharmaShashikant Sharma

 

 

Hi neckr,

Sorry, copied trigger twice , take only first part

 

Also check this for optimizing trigger :  http://forceschool.blogspot.com/

 

neckrneckr

Shashikant,

 

Thank you for your help. I tried to compile your code and received the following error noted in red below, any suggestions?

 

 

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>();
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);
    }
Map<Id,List<Account>> records = new Map<Id,List<Account>>(); // Was getting the following //error so I added List to the second part, which I beleieve triggered my error below
//Compile Error: Illegal assignment from MAP<Id,Account> to MAP<Id,LIST<Account>> at line //19 column 1

 for(Account a : [SELECT id , (select id from Bankruptcy_Judgements_Liens_Report__r), (select id from Business_Filings_Verifications__r), (select id from Identity_Verifications__r), (select id from Criminal_Sex_Offender_Reports__r), (select id from License_Verifications__r), (select id from General_Liability_Insurance_Verification__r), (select id from Eco_Friendly_Accreditation_Verifications__r), (select id from Trade_Reference_Reports__r), (select id from Customer_Reference_Reports__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)).Bankruptcy_Judgements_Liens_Report__r.Size() == 0) //Compile Error: Initial term of field expression must be a concrete SObject: //LIST<Account> { Contact BJLVAgencyContact = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'BJLV Agency', LastName = 'Agency Contact'); insert BJLVAgencyContact; Bankruptcy_Judgements_Liens_Report__c BJLV = new Bankruptcy_Judgements_Liens_Report__c(Account__c = ST.Account__c , BJLV_Agency_Contact__c = BJLVAgencyContact.ID ); insert BJLV; } } }

 

 

Shashikant SharmaShashikant Sharma

Hi ,

 

Sorry for the errors , actualy I wrote this on notepad so could not compile before suggesting you.

 

Change this 

 

 

From : if (records.get(mapST_Account.get(ST.id)).Bankruptcy_Judgements_Liens_Report__r.Size()  == 0)
 
To : if (records.get(mapST_Account.get(ST.id)).get(0).Bankruptcy_Judgements_Liens_Report__r.Size()  == 0)

 

 

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.

 

 

neckrneckr

Thank you very much!  Works great!  I posted the working code for others .

rigger 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>();
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,List<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)).get(0).My_Custom_Object1__r.Size()  == 0 ) { 
 //Insert New Record Code Here     
}
if (records.get(mapST_Account.get(ST.id)).get(0).My_Custom_Object2__r.Size()  == 0 ) {
 //Insert New Record Code Here   
}
 
if (records.get(mapST_Account.get(ST.id)).get(0).My_Custom_Object3__r.Size()  == 0 ) {
 //Insert New Record Code Here   
}
// Continue through to My_Custom_Object8__r
 
}

 

 

 

This was selected as the best answer
neckrneckr

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.

 

 

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>();
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);
    }
Map<Id,List<Account>> records = new Map<Id,List<Account>>();
for(Account a : [SELECT id , 
(select id from Bankruptcy_Judgements_Liens_Report__r), 
(select id from Business_Filings_Verifications__r), 
(select id from Identity_Verifications__r), 
(select id from Criminal_Sex_Offender_Reports__r),
(select id from License_Verifications__r), 
(select id from General_Liability_Insurance_Verification__r), 
(select id from Eco_Friendly_Accreditation_Verifications__r), 
(select id from Trade_Reference_Reports__r),
(select id from Customer_Reference_Reports__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) {

Contact Principalcontact=[SELECT ID FROM Contact WHERE AccountId = : ST.Account__c AND Contact_Type__c = 'Acct Principal Contact' limit 1];

//INSERT NEW BJLV RECORD IF REQUIRED AND DOES NOT YET EXSIST 
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');
 insert BJLVAgencyContact;
 
 Bankruptcy_Judgements_Liens_Report__c BJLV = new Bankruptcy_Judgements_Liens_Report__c(Account__c = ST.Account__c , BJLV_Agency_Contact__c = BJLVAgencyContact.ID );    
 insert BJLV;      
}


//INSERT NEW BFV RECORD IF REQUIRED AND DOES NOT YET EXSIST 
if (records.get(mapST_Account.get(ST.id)).get(0).Business_Filings_Verifications__r.Size()  == 0)  
{ 
 Contact BFVAgencyContact = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'BFV Agency', LastName = 'Enter Last Name');
 insert BFVAgencyContact;
 
 Business_Filings_Verification__c BFV = new Business_Filings_Verification__c(Account__c = ST.Account__c , BFV_Agency_Contact__c = BFVAgencyContact.ID );    
 insert BFV;      
}

//INSERT NEW IDV RECORD IF REQUIRED AND DOES NOT YET EXSIST 
if (records.get(mapST_Account.get(ST.id)).get(0).Identity_Verifications__r.Size()  == 0)  
{ 
 Contact IDVAgencyContact = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'IDV Agency', LastName = 'Enter Last Name');
 insert IDVAgencyContact;
 
 Identity_Verification__c IDV = new Identity_Verification__c(Account__c = ST.Account__c , IDV_Agency_Contact__c = IDVAgencyContact.ID );    
 insert IDV;      
}


//INSERT NEW CSOV RECORD IF REQUIRED AND DOES NOT YET EXSIST 
if (records.get(mapST_Account.get(ST.id)).get(0).Criminal_Sex_Offender_Reports__r.Size()  == 0)  
{ 
 Contact CSOVAgencyContact = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'CSOV Agency', LastName = 'Enter Last Name');
 insert CSOVAgencyContact;
 
 Criminal_Sex_Offender_Report__c CSOV = new Criminal_Sex_Offender_Report__c(Account__c = ST.Account__c , CSOV_Agency_Contact__c = CSOVAgencyContact.ID );    
 insert CSOV;      
}


//INSERT NEW LV RECORD IF REQUIRED AND DOES NOT YET EXSIST 
if (records.get(mapST_Account.get(ST.id)).get(0).License_Verifications__r.Size()  == 0)  
{ 
 Contact LVAgencyContact = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'LV Agency', LastName = 'Enter Last Name');
 insert LVAgencyContact;
 
 License_Verification__c LV = new License_Verification__c(Account__c = ST.Account__c , LV_Agency_Contact__c = LVAgencyContact.ID );    
 insert LV;      
}

//INSERT NEW GLIV RECORD IF REQUIRED AND DOES NOT YET EXSIST 
if (records.get(mapST_Account.get(ST.id)).get(0).General_Liability_Insurance_Verification__r.Size()  == 0) 
{ 
 Contact GLIVAgencyContact = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'GLIV Agency', LastName = 'Enter Last Name');
 insert GLIVAgencyContact;
 
 General_Liability_Insurance_Verification__c GLIV = new General_Liability_Insurance_Verification__c(Account__c = ST.Account__c , GLIV_Agent_Contact__c = GLIVAgencyContact.ID );    
 insert GLIV;      
}

//INSERT NEW EFAV RECORD IF REQUIRED AND DOES NOT YET EXSIST 
if (records.get(mapST_Account.get(ST.id)).get(0).Eco_Friendly_Accreditation_Verifications__r.Size()  == 0) 
{ 
 Contact EFAVAgencyContact = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'EFAV Agency', LastName = 'Enter Last Name');
 insert EFAVAgencyContact;
 
 Eco_Friendly_Accreditation_Verification__c EFAV = new Eco_Friendly_Accreditation_Verification__c(Account__c = ST.Account__c , EFAV_Agency_Contact__c = EFAVAgencyContact.ID );    
 insert EFAV;      
}

//INSERT 3 NEW CRV RECORDS IF REQUIRED
if (!ST.Service__r.Customer_Reference_Reports__c)
{
 
 Contact CRVContact1 = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'CRV Contact', LastName = 'Enter Last Name');
 insert CRVContact1;
 
 Customer_Reference_Report__c CRV1 = new Customer_Reference_Report__c(CRV_Service__c = ST.ID, CRV_Account__c = ST.Account__c , CRV_Contact__c = CRVContact1.ID, CRV_Principal_Contact__c = PrincipalContact.ID );    
 insert CRV1;      

 Contact CRVContact2 = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'CRV Contact', LastName = 'Enter Last Name');
 insert CRVContact2;
 
 Customer_Reference_Report__c CRV2 = new Customer_Reference_Report__c(CRV_Service__c = ST.ID, CRV_Account__c = ST.Account__c , CRV_Contact__c = CRVContact2.ID, CRV_Principal_Contact__c = PrincipalContact.ID );    
 insert CRV2;  
 
  Contact CRVContact3 = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'CRV Contact', LastName = 'Enter Last Name');
 insert CRVContact3;
 
 Customer_Reference_Report__c CRV3 = new Customer_Reference_Report__c(CRV_Service__c = ST.ID, CRV_Account__c = ST.Account__c , CRV_Contact__c = CRVContact3.ID, CRV_Principal_Contact__c = PrincipalContact.ID );    
 insert CRV3;  

}


//CHECK TO SEE IF TRADE REFERENCE FOR PARTICULAR CATEGORY EXISITS, IF SO ASSIGN ID'S TO ST, //IF NOT CREATE 3 NEW TRV RECORDS

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]; if (ExistingTRVs.Size() == 3) { ST.Trade_Reference_Report_1__c = ExistingTRVs[0].ID; ST.Trade_Reference_Report_2__c = ExistingTRVs[1].ID; ST.Trade_Reference_Report_3__c = ExistingTRVs[2].ID; } if (ExistingTRVs.Size() == 0) { Contact TRVCompanyContact1 = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'TRV Company', LastName = 'Enter Last Name'); insert TRVCompanyContact1; Trade_Reference_Report__c TRV1 = new Trade_Reference_Report__c(Account__c = ST.Account__c , TRV_Company_Contact__c = TRVCompanyContact1.ID, TRV_Principal_Contact__c = PrincipalContact.ID ); insert TRV1; ST.Trade_Reference_Report_1__c = TRV1.ID; Contact TRVCompanyContact2 = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'TRV Company', LastName = 'Enter Last Name'); insert TRVCompanyContact2; Trade_Reference_Report__c TRV2 = new Trade_Reference_Report__c(Account__c = ST.Account__c , TRV_Company_Contact__c = TRVCompanyContact2.ID, TRV_Principal_Contact__c = PrincipalContact.ID ); insert TRV2; ST.Trade_Reference_Report_2__c = TRV2.ID; Contact TRVCompanyContact3 = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'TRV Company', LastName = 'Enter Last Name'); insert TRVCompanyContact3; Trade_Reference_Report__c TRV3 = new Trade_Reference_Report__c(Account__c = ST.Account__c , TRV_Company_Contact__c = TRVCompanyContact3.ID, TRV_Principal_Contact__c = PrincipalContact.ID ); insert TRV3; ST.Trade_Reference_Report_3__c = TRV3.ID; } } }

 

 

Rahul S.ax961Rahul S.ax961

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

neckrneckr

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?

 

 

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), 
(select id from Business_Filings_Verifications__r), 
(select id from Identity_Verifications__r), 
(select id from Criminal_Sex_Offender_Reports__r),
(select id from License_Verifications__r), 
(select id from General_Liability_Insurance_Verification__r), 
(select id from Eco_Friendly_Accreditation_Verifications__r), 
(select id from Trade_Reference_Reports__r),
(select id from Customer_Reference_Reports__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 EXSIST - NEED TO AUTO LINK BJLV CONTACT TO VERIFICATION BODY
//BASED ON SERVICE AREA
if (records.get(mapST_Account.get(ST.id)).get(0).Bankruptcy_Judgements_Liens_Report__r.Size()  == 0)  //&& (ST.Service__r.Bankruptcy_Judgements_Liens_Report__c )
{ 
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?
}

 

 

neckrneckr

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?

 

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), 
(select id from Business_Filings_Verifications__r), 
(select id from Identity_Verifications__r), 
(select id from Criminal_Sex_Offender_Reports__r),
(select id from License_Verifications__r), 
(select id from General_Liability_Insurance_Verification__r), 
(select id from Eco_Friendly_Accreditation_Verifications__r), 
(select id from Trade_Reference_Reports__r),
(select id from Customer_Reference_Reports__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 EXSIST - NEED TO AUTO LINK BJLV CONTACT TO VERIFICATION BODY
//BASED ON SERVICE AREA
if (records.get(mapST_Account.get(ST.id)).get(0).Bankruptcy_Judgements_Liens_Report__r.Size()  == 0)  //&& (ST.Service__r.Bankruptcy_Judgements_Liens_Report__c )
{ 
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?
}