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
KevinRussellKevinRussell 

How to create local map list then run query against it.

So, I'm getting the error that I have too many queries running on a trigger when I try to import records into an object, using Informatica.  The trigger works fine when I manually enter records from Salesforce.

 

I have 7 queries in this trigger against the same object.  I would like to refactor this code so I have one query, pulling selected Affiliation records into a local map, then run my 7 queries against the local map.

 

Can someone please guide me how to do this.

 

Thanks,

 

Kevin

 

For reference, I have the complete working trigger below:

trigger AccountContactAffiliations on npe5__Affiliation__c   (after insert, after update, after delete) {

// Update Multiple Contact and Account fields: TBD, when an Affiliation record is inserted, updated or deleted. 

Integer SpeakingRecordCount;
Integer LeadershipBoardCount;
Integer PastLeadershipBoardCount;
Integer EmeritusTrusteeCount;

Boolean PastLeadershipBoard;
Boolean EmeritusTrustee;
Boolean LeadershipBoard;
try {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();

// Store Account record ID
map< id, account > accounts = new map< id, account >(); if (trigger.isinsert || trigger.isupdate) { // Create trigger for new or selected npe5__Affiliation__c record for(npe5__Affiliation__c record:trigger.new) { // Contact Affiliation Speaker count SpeakingRecordCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c = 'Speaker']; // Contact Affiliation Leadership Board count LeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Current']; if(LeadershipBoardCount >= 1) LeadershipBoard = TRUE; Else { LeadershipBoard = FALSE; } // Contact Affiliation Past Leadership Board count PastLeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Former']; if(PastLeadershipBoardCount >= 1) PastLeadershipBoard = TRUE; Else { PastLeadershipBoard = FALSE; } // Contact Affiliation Emeritus Trustee count EmeritusTrusteeCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Role__c LIKE :'%'+'Emeritus'+'%']; if(EmeritusTrusteeCount >= 1) EmeritusTrustee = TRUE; Else { EmeritusTrustee = FALSE; } // Contact data to update contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, AffAASpeakingCnt__c = SpeakingRecordCount, Past_Leadership_Board__c = PastLeadershipBoard, Emeritus_Trustee__c = EmeritusTrustee, Leadership_Board__C = LeadershipBoard )); // Update Contact record update contacts.values(); // Account Affiliation Leadership Board count LeadershipBoardcount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Current']; if(LeadershipBoardCount >= 1) LeadershipBoard = TRUE; Else { LeadershipBoard = FALSE; } // Account Affiliation Past Leadership Board count PastLeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Former']; if(PastLeadershipBoardCount >= 1) PastLeadershipBoard = TRUE; Else { PastLeadershipBoard = FALSE; } // Account Affiliation Emeritus Trustee count EmeritusTrusteeCount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Role__c LIKE :'%'+'Emeritus'+'%']; if(EmeritusTrusteeCount >= 1) EmeritusTrustee = TRUE; Else { EmeritusTrustee = FALSE; } // Account data to update accounts.put(record.npe5__Organization__c, new account(id=record.npe5__Organization__c, Leadership_Board_s__c = LeadershipBoard, Past_Leadership_Board_s__c = PastLeadershipBoard, Emeritus_Trustee__c = EmeritusTrustee )); // Update Account record update accounts.values(); } } if (trigger.isdelete) { // Create trigger for deleted npe5__Affiliation__c record for(npe5__Affiliation__c record:trigger.old) { // Contact Affiliation Speaker count SpeakingRecordCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c = 'Speaker']; // Contact Affiliation Leadership Board count LeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Current']; if(LeadershipBoardCount >= 1) LeadershipBoard = TRUE; Else { LeadershipBoard = FALSE; } // Contact Affiliation Past Leadership Board count PastLeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Former']; if(PastLeadershipBoardCount >= 1) PastLeadershipBoard = TRUE; Else { PastLeadershipBoard = FALSE; } // Contact Affiliation Emeritus Trustee count EmeritusTrusteeCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Role__c LIKE :'%'+'Emeritus'+'%']; if(EmeritusTrusteeCount >= 1) EmeritusTrustee = TRUE; Else { EmeritusTrustee = FALSE; } // Contact data to update contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, AffAASpeakingCnt__c = SpeakingRecordCount, Past_Leadership_Board__c = PastLeadershipBoard, Emeritus_Trustee__c = EmeritusTrustee, Leadership_Board__C = LeadershipBoard )); // Update Contact record update contacts.values(); // Account Affiliation Leadership Board count LeadershipBoardcount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Current']; if(LeadershipBoardCount >= 1) LeadershipBoard = TRUE; Else { LeadershipBoard = FALSE; } // Account Affiliation Past Leadership Board count PastLeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Former']; if(PastLeadershipBoardCount >= 1) PastLeadershipBoard = TRUE; Else { PastLeadershipBoard = FALSE; } // Account Affiliation Emeritus Trustee count EmeritusTrusteeCount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Role__c LIKE :'%'+'Emeritus'+'%']; if(EmeritusTrusteeCount >= 1) EmeritusTrustee = TRUE; Else { EmeritusTrustee = FALSE; } // Account data to update accounts.put(record.npe5__Organization__c, new account(id=record.npe5__Organization__c, Leadership_Board_s__c = LeadershipBoard, Past_Leadership_Board_s__c = PastLeadershipBoard, Emeritus_Trustee__c = EmeritusTrustee )); // Update Account record update accounts.values(); } } } catch (Exception e) { } }