• CW5
  • NEWBIE
  • 25 Points
  • Member since 2010

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 5
    Replies

ERROR ON LINE - 

if(lstcoa3[i].Contact__c == c.id) 

 

I know what the error is, but am not sure how to fix it.

 

count is sometimes higher than the lists (lstcoa1, lstcoa2, lstcoa3, lstcoa4) because they are different sizes.lstcoa1.size() could be 10 and lstcoa2.size() could be 25.

 

I need it to to loop through and only calculate if "i" will return for lstcoa1[i] and not provide an error.

 

I've tried basic if statements that say if(lstcoa1.size() < i) but that didn't  seem to work.

 

Thoughts on how to get this error to go away?

 

 

 

 

for(Integer i = 0; i < count; i++)
  {
                
        if(lstcoa1[i].Contact__c == c.id)
        {
        numSent = numSent + 1;
        }
        
                
        if(lstcoa2[i].Contact__c == c.id) 
        {
        numOpened = numOpened + 1;
        }
        
        if(lstcoa3[i].Contact__c == c.id) 
        {
        numClicked = numClicked + 1;
        }
    
       
        if(lstcoa4[i].Contact__c == c.id) 
        {
        numBounced = numBounced + 1;
        }
  }
  

 

 

  • November 17, 2010
  • Like
  • 0

Essentially, all I'm trying to do is count the number of emails from a custom object that are related to the contact being updated and make it bulk safe (No errors when mass updating from the data loader).

 

The custom object is called "xtma_Individual_Email_Result__c" and contains individual emails that are sent with a related lookup containing the Contact ID.

 

I want to count 4 variables. How many emails related to that contact that were Sent (Has a Sent Date), Opened (Has an Opened Date), Clicked (Has at least 1 or more Unique Clicks), or Bounced (Has a Bounced Date).

 

I then want to put those 4 counts into 4 fields on the contact labeled appropriately.

 

This trigger works perfectly for a one-off update inside of SalesForce.com but provides the error "UpdateEmailCount: System.Exception: Too many script statements: 50001" when updated in mass (5,000+) through the dataloader.

 

My question is... How do I reprogram the FOR loops such as below, so that I don't hit the limits?

   

Integer numSent = 0; 

    for(Integer i = 0; i < lstcoa1.size(); i++) 
    { 
        if(lstcoa1[i].Contact__c == c.id) { 
        numSent = numSent + 1; 
        } 
    }    

 

 Is there a way to query from the Maps so that it counts all of the records in the map that match the Customer ID and the specific criteria (Such as Sent Date)?

 

 

 

Below is the full code:

 

trigger UpdateEmailCount on Contact (before update){

    Map<String, Contact> cobMap = new Map<String, Contact>();
    
    for (Contact cob : System.Trigger.New) {
            cobMap.put(cob.ID, cob);
    }

list<xtma_Individual_Email_Result__c> lstcoa1 = new list<xtma_Individual_Email_Result__c>();
list<xtma_Individual_Email_Result__c> lstcoa2 = new list<xtma_Individual_Email_Result__c> ();
list<xtma_Individual_Email_Result__c> lstcoa3 = new list<xtma_Individual_Email_Result__c> ();
list<xtma_Individual_Email_Result__c> lstcoa4 = new list<xtma_Individual_Email_Result__c> ();
 
 
for(xtma_Individual_Email_Result__c   obj : [SELECT Id, Contact__c,Date_Time_Sent__c,Date_Time_Opened__c,Number_of_Unique_Clicks__c,Date_Bounced__c
FROM xtma_Individual_Email_Result__c WHERE Contact__c IN :cobMap.KeySet()])
{  
  if(obj.Date_Time_Sent__c != null)
  {
  lstcoa1.add(obj);
  }
  if(obj.Date_Time_Opened__c != null)
  {
  lstcoa2.add(obj);
  }
  if(obj.Number_of_Unique_Clicks__c > 0)
  {
  lstcoa3.add(obj);
  }
  if(obj.Date_Bounced__c != null)
  {
  lstcoa4.add(obj);
  }
}

Map<ID, Integer> numSentMap = new Map<ID, Integer>();
Map<ID, Integer> numOpenedMap = new Map<ID, Integer>();
Map<ID, Integer> numClickedMap = new Map<ID, Integer>();
Map<ID, Integer> numBouncedMap = new Map<ID, Integer>();


for(Contact c: Trigger.New)
{
    Integer numSent = 0; 

    for(Integer i = 0; i < lstcoa1.size(); i++) 
    { 
        if(lstcoa1[i].Contact__c == c.id) { 
        numSent = numSent + 1; 
        } 
    }    

numSentMap.put(c.id,numSent);

}

for(Contact c: Trigger.New)
{
    Integer numOpened = 0; 


    for(Integer j = 0; j < lstcoa2.size(); j++) 
    { 
        if(lstcoa2[j].Contact__c == c.id) { 
        numOpened = numOpened + 1; 
        } 
    }
 
numOpenedMap.put(c.id,numOpened);

}

for(Contact c: Trigger.New)
{
    Integer numClicked = 0;  

    for(Integer k = 0; k < lstcoa3.size(); k++) 
    { 
        if(lstcoa3[k].Contact__c == c.id) { 
        numClicked = numClicked + 1; 
        } 
    }   

 
numClickedMap.put(c.id,numClicked);

}

for(Contact c: Trigger.New)
{
    Integer numBounced = 0; 


    for(Integer l = 0; l < lstcoa4.size(); l++) 
    { 
        if(lstcoa4[l].Contact__c == c.id) { 
        numBounced = numBounced + 1; 
        } 
    }
 
numBouncedMap.put(c.id,numBounced);

}
    
    for(Contact c: Trigger.New)
    {
    
         c.Number_Emails_Sent__c = numSentMap.get(c.id);
         c.Number_Emails_Opened__c = numOpenedMap.get(c.id);
         c.Number_Emails_Clicked__c = numClickedMap.get(c.id);
         c.Number_Emails_Bounced__c = numBouncedMap.get(c.id);
         
      }
    
}

 

  • November 17, 2010
  • Like
  • 0

I've been bouncing around the forums and searching online, which I thought would be easy. How do you create a new record on the Account object inside of a trigger?

 

I want the new Account to have a name, record type, owner id, and that's it.

  • May 10, 2010
  • Like
  • 0

I'd like to write a simple trigger that updates a field on account called HPD if any of the contacts have a checkbox called HPD checked.

 

This is what I've come up with so far but it needs a little tweaking... Any thoughts?

 

------------------------------------------------------------------------

 

trigger UpdateAccountFromContacts on Account(before insert, before update) {

Set<Id> accountIds = new Set<Id>();

for (Account a : Trigger.new)

accountIds.add(a.Id);

Map<Id, Account> accounts = new Map<Id, Account>( [ Select a.Id, (Select HPD__c From Contacts) from Account a where HPD__c = True ] );

for (Account a : Trigger.new) {

Account accFromMap = accounts.get(a.Id);

if (accFromMap.Contacts.size() > 0) {

a.HPD__c = True;

}

} }

  • May 06, 2010
  • Like
  • 0

ERROR ON LINE - 

if(lstcoa3[i].Contact__c == c.id) 

 

I know what the error is, but am not sure how to fix it.

 

count is sometimes higher than the lists (lstcoa1, lstcoa2, lstcoa3, lstcoa4) because they are different sizes.lstcoa1.size() could be 10 and lstcoa2.size() could be 25.

 

I need it to to loop through and only calculate if "i" will return for lstcoa1[i] and not provide an error.

 

I've tried basic if statements that say if(lstcoa1.size() < i) but that didn't  seem to work.

 

Thoughts on how to get this error to go away?

 

 

 

 

for(Integer i = 0; i < count; i++)
  {
                
        if(lstcoa1[i].Contact__c == c.id)
        {
        numSent = numSent + 1;
        }
        
                
        if(lstcoa2[i].Contact__c == c.id) 
        {
        numOpened = numOpened + 1;
        }
        
        if(lstcoa3[i].Contact__c == c.id) 
        {
        numClicked = numClicked + 1;
        }
    
       
        if(lstcoa4[i].Contact__c == c.id) 
        {
        numBounced = numBounced + 1;
        }
  }
  

 

 

  • November 17, 2010
  • Like
  • 0

Essentially, all I'm trying to do is count the number of emails from a custom object that are related to the contact being updated and make it bulk safe (No errors when mass updating from the data loader).

 

The custom object is called "xtma_Individual_Email_Result__c" and contains individual emails that are sent with a related lookup containing the Contact ID.

 

I want to count 4 variables. How many emails related to that contact that were Sent (Has a Sent Date), Opened (Has an Opened Date), Clicked (Has at least 1 or more Unique Clicks), or Bounced (Has a Bounced Date).

 

I then want to put those 4 counts into 4 fields on the contact labeled appropriately.

 

This trigger works perfectly for a one-off update inside of SalesForce.com but provides the error "UpdateEmailCount: System.Exception: Too many script statements: 50001" when updated in mass (5,000+) through the dataloader.

 

My question is... How do I reprogram the FOR loops such as below, so that I don't hit the limits?

   

Integer numSent = 0; 

    for(Integer i = 0; i < lstcoa1.size(); i++) 
    { 
        if(lstcoa1[i].Contact__c == c.id) { 
        numSent = numSent + 1; 
        } 
    }    

 

 Is there a way to query from the Maps so that it counts all of the records in the map that match the Customer ID and the specific criteria (Such as Sent Date)?

 

 

 

Below is the full code:

 

trigger UpdateEmailCount on Contact (before update){

    Map<String, Contact> cobMap = new Map<String, Contact>();
    
    for (Contact cob : System.Trigger.New) {
            cobMap.put(cob.ID, cob);
    }

list<xtma_Individual_Email_Result__c> lstcoa1 = new list<xtma_Individual_Email_Result__c>();
list<xtma_Individual_Email_Result__c> lstcoa2 = new list<xtma_Individual_Email_Result__c> ();
list<xtma_Individual_Email_Result__c> lstcoa3 = new list<xtma_Individual_Email_Result__c> ();
list<xtma_Individual_Email_Result__c> lstcoa4 = new list<xtma_Individual_Email_Result__c> ();
 
 
for(xtma_Individual_Email_Result__c   obj : [SELECT Id, Contact__c,Date_Time_Sent__c,Date_Time_Opened__c,Number_of_Unique_Clicks__c,Date_Bounced__c
FROM xtma_Individual_Email_Result__c WHERE Contact__c IN :cobMap.KeySet()])
{  
  if(obj.Date_Time_Sent__c != null)
  {
  lstcoa1.add(obj);
  }
  if(obj.Date_Time_Opened__c != null)
  {
  lstcoa2.add(obj);
  }
  if(obj.Number_of_Unique_Clicks__c > 0)
  {
  lstcoa3.add(obj);
  }
  if(obj.Date_Bounced__c != null)
  {
  lstcoa4.add(obj);
  }
}

Map<ID, Integer> numSentMap = new Map<ID, Integer>();
Map<ID, Integer> numOpenedMap = new Map<ID, Integer>();
Map<ID, Integer> numClickedMap = new Map<ID, Integer>();
Map<ID, Integer> numBouncedMap = new Map<ID, Integer>();


for(Contact c: Trigger.New)
{
    Integer numSent = 0; 

    for(Integer i = 0; i < lstcoa1.size(); i++) 
    { 
        if(lstcoa1[i].Contact__c == c.id) { 
        numSent = numSent + 1; 
        } 
    }    

numSentMap.put(c.id,numSent);

}

for(Contact c: Trigger.New)
{
    Integer numOpened = 0; 


    for(Integer j = 0; j < lstcoa2.size(); j++) 
    { 
        if(lstcoa2[j].Contact__c == c.id) { 
        numOpened = numOpened + 1; 
        } 
    }
 
numOpenedMap.put(c.id,numOpened);

}

for(Contact c: Trigger.New)
{
    Integer numClicked = 0;  

    for(Integer k = 0; k < lstcoa3.size(); k++) 
    { 
        if(lstcoa3[k].Contact__c == c.id) { 
        numClicked = numClicked + 1; 
        } 
    }   

 
numClickedMap.put(c.id,numClicked);

}

for(Contact c: Trigger.New)
{
    Integer numBounced = 0; 


    for(Integer l = 0; l < lstcoa4.size(); l++) 
    { 
        if(lstcoa4[l].Contact__c == c.id) { 
        numBounced = numBounced + 1; 
        } 
    }
 
numBouncedMap.put(c.id,numBounced);

}
    
    for(Contact c: Trigger.New)
    {
    
         c.Number_Emails_Sent__c = numSentMap.get(c.id);
         c.Number_Emails_Opened__c = numOpenedMap.get(c.id);
         c.Number_Emails_Clicked__c = numClickedMap.get(c.id);
         c.Number_Emails_Bounced__c = numBouncedMap.get(c.id);
         
      }
    
}

 

  • November 17, 2010
  • Like
  • 0

I'd like to write a simple trigger that updates a field on account called HPD if any of the contacts have a checkbox called HPD checked.

 

This is what I've come up with so far but it needs a little tweaking... Any thoughts?

 

------------------------------------------------------------------------

 

trigger UpdateAccountFromContacts on Account(before insert, before update) {

Set<Id> accountIds = new Set<Id>();

for (Account a : Trigger.new)

accountIds.add(a.Id);

Map<Id, Account> accounts = new Map<Id, Account>( [ Select a.Id, (Select HPD__c From Contacts) from Account a where HPD__c = True ] );

for (Account a : Trigger.new) {

Account accFromMap = accounts.get(a.Id);

if (accFromMap.Contacts.size() > 0) {

a.HPD__c = True;

}

} }

  • May 06, 2010
  • Like
  • 0