+ Start a Discussion
Todd B.Todd B. 

Need Help cleaning up a trigger

I have two custom objects: 
  • printsf__Collateral_Send_History__c
  • SD_Member__c
The two objects are linked by a lookup field however, on the printsf__Collateral_Send_History__c a back end system populates a text field with SD_Member__c id.  So I created a trigger to take the field value and query the SD_member__c object and return the correct SD_member__c.name field to poplate the lookup field.  
 
trigger CreateSDMemberLinkage on printsf__Collateral_Send_History__c (Before Insert) {

    List<SD_Member__c > recordsToUpdate = new List<SD_Member__c >();
    
    For(printsf__Collateral_Send_History__c CSH: Trigger.new) {
        String strId = csh.printsf__Recipient_ID__c;
        String StrId2 = csh.Name;
        String strId3 ;
    
    // Check to see if it is an SD Member Entry        
    If(CSH.printsf__Recipient_ID__c!=null){  

    
	// If this is a SD Member record, then query the SD Member object with the text based id
	// to get the matching SD Member name field to populate in the lookup field 
    For(SD_Member__c sd001 :  [Select Id, Name from SD_Member__C where id =: strid]){
    
	System.debug('SD Id:  '+ strid);

    strid3 = sd001.id;
    } 
    }
	
     csh.SD_Member_Stakeholders__c = strid3;

Everything is working as expected, but occasionally I receive an error:

Apex script unhandled trigger exception by user/organization: 0055000000118E4/00D50000000784t
CreateSDMemberLinkage: System.LimitException: Too many SOQL queries: 101


Any suggestions are to how I can clean up my code?

Thanks,
Todd B.