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
Blake TanonBlake Tanon 

Bulk trigger to update date question

I have a trigger that updates the number of calls logged against a contact, this part works fine in batch processes.  The second part of the trigger I need to update a date field with the first call date(in red), but it doesn't seem to be working in dataloads.  Any suggestions?

 

 

 

 

trigger CallCount on Contact (before update) {


  for(Contact c:Trigger.new)
    c.Calls__c = 0;
     
  for(Task t:[select id,WhoId from Task where Whoid in :Trigger.new AND (Type = 'Cold Call' OR Type = 'Inbound Call' OR Type = 'Outbound Call') AND Status = 'Completed' AND isDeleted = False AND ActivityDate > 2009-12-31 LIMIT 250 ALL ROWS])

    Trigger.newMap.get(t.WhoId).Calls__c++;
    
   
    for(Contact c:Trigger.new)
    c.First_Call_Date__c = null;
    
    for(Task t:[select activitydate, whoid from task where whoid in:trigger.new AND (Type = 'Cold Call' OR Type = 'Inbound Call' OR Type = 'Outbound Call') AND Status = 'Completed' AND isDeleted = False Order By ActivityDate asc LIMIT 1 ALL ROWS])
        
        trigger.newMap.get(t.whoid).First_Call_Date__c = t.ActivityDate;


}

 

Best Answer chosen by Admin (Salesforce Developers) 
logontokartiklogontokartik

Can you try the below code. It does the same thing your code is doing, I just changed to make it more structured. Also when you say you are using Data Loader for updating contacts, are you including Ids in the csv file?

 

trigger CallCount on Contact (before update) {
	
	// Build the maps for the Count and ActivityDate First
	
	Map<String,Integer> callCountMap = new Map<String,Integer>();
	Map<String,Date>  callActivityMap = new Map<String,Date>();
	
	for(AggregateResult r:[select COUNT(id), WhoId from Task where Whoid in :Trigger.new AND (Type = 'Cold Call' OR Type = 'Inbound Call' OR Type = 'Outbound Call') AND Status = 'Completed' AND isDeleted = False AND ActivityDate > 2009-12-31 LIMIT 250 ALL ROWS GROUP BY WhoId])
		callCountMap.put(String.valueof(r.get('WhoId'),Integer.valueof(r.get('expr0'));


	for(Task t:[select activitydate, whoid from task where whoid in:trigger.new AND (Type = 'Cold Call' OR Type = 'Inbound Call' OR Type = 'Outbound Call') AND Status = 'Completed' AND isDeleted = False Order By ActivityDate asc LIMIT 1 ALL ROWS])
		callActivityMap.put(t.WhoId,t.activitydate);

	// Iterate over the Trigger.new List and check if the Id exists in  the Map and assign value from Map		
  for(Contact c:Trigger.new) {   
    c.Calls__c = 0;   
    c.First_Call_Date__c = null;
		
	if(callCountMap.get(c.Id) != null)
		c.Calls__c = callCountMap.get(c.Id);
	
	if(callActivityMap.get(c.Id) != null)
		c.First_Call_Date__c = callActivityMap.get(c.Id);     
   }

}

 

All Answers

logontokartiklogontokartik

Can you try the below code. It does the same thing your code is doing, I just changed to make it more structured. Also when you say you are using Data Loader for updating contacts, are you including Ids in the csv file?

 

trigger CallCount on Contact (before update) {
	
	// Build the maps for the Count and ActivityDate First
	
	Map<String,Integer> callCountMap = new Map<String,Integer>();
	Map<String,Date>  callActivityMap = new Map<String,Date>();
	
	for(AggregateResult r:[select COUNT(id), WhoId from Task where Whoid in :Trigger.new AND (Type = 'Cold Call' OR Type = 'Inbound Call' OR Type = 'Outbound Call') AND Status = 'Completed' AND isDeleted = False AND ActivityDate > 2009-12-31 LIMIT 250 ALL ROWS GROUP BY WhoId])
		callCountMap.put(String.valueof(r.get('WhoId'),Integer.valueof(r.get('expr0'));


	for(Task t:[select activitydate, whoid from task where whoid in:trigger.new AND (Type = 'Cold Call' OR Type = 'Inbound Call' OR Type = 'Outbound Call') AND Status = 'Completed' AND isDeleted = False Order By ActivityDate asc LIMIT 1 ALL ROWS])
		callActivityMap.put(t.WhoId,t.activitydate);

	// Iterate over the Trigger.new List and check if the Id exists in  the Map and assign value from Map		
  for(Contact c:Trigger.new) {   
    c.Calls__c = 0;   
    c.First_Call_Date__c = null;
		
	if(callCountMap.get(c.Id) != null)
		c.Calls__c = callCountMap.get(c.Id);
	
	if(callActivityMap.get(c.Id) != null)
		c.First_Call_Date__c = callActivityMap.get(c.Id);     
   }

}

 

This was selected as the best answer
Blake TanonBlake Tanon

Yes I am including the ID.  I moved my code to production adn the numbers are not updating after a data load, but they did in sandbox.  I will give yours a try and let you know what I get.  Thank you.

Blake TanonBlake Tanon

I'm getting error: expecting a right parentheses, found ';' at line 9 column 86 - But at line9col86 there is a right parentheses?

 

Never mind I found the missing one.

Blake TanonBlake Tanon

I tried this with a single update of a record and teh call count works, however the date field still is not updating.  Did I write the query wrong?

logontokartiklogontokartik

I tested this by adding custom fields to Contact Object, and I am able to update both the Calls__c and First_Call_Date__c fields on Contact when a update happens. 

 

Can you please confirm if First_Call_Date__c is defined as Date field? Also it would be helpful if you can paste the debug log to see where its going wrong.

Blake TanonBlake Tanon

This worked, I was looking at the wrong field in sandbox.  Thank you so much for your help!