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
Arash TeimoupoorArash Teimoupoor 

update a custom field with last event createddate

Hi Everyone, I'm trying to update a custom field on my sample object with the creation date of any task that is created. I receive the following error when I try to save the trigger:


Error: Compile Error: Initial term of field expression must be a concrete SObject: List<OpenActivity> at line 8 column 2

below is my trigger:

trigger UpdatePresDate on Task(after insert, after delete){
 Set<ID> tWhatIDs = new Set<ID>();
Task t= Trigger.new[0];
List<Sample__c> acclist = new List<Sample__C>();
    List<OpenActivity> openlist = new List<OpenActivity>();
    
 acclist = [Select Score__C, (Select Id, CreatedDate From OpenActivities where isTask =: true ) From Sample__C where Id IN: tWhatIDs];
 openlist.CreatedDate = acclist.Score__C;
update acclist;
}


what should I change in my trigger? Thanks in advance


 
Best Answer chosen by Arash Teimoupoor
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

In that case you should try this:
 
trigger UpdatePresDate on Task(after insert){
	
	Map<Id, Datetime> sampleDates = new Map<Id, Datetime>();
	
	for (Task tsk : Trigger.new) {
		if (tsk.WhatId.getSObjectType() == Sample__c.sObjectType)
			sampleDates.put(tsk.WhatId, tsk.CreatedDate);
	}
	
	// Mass updates records avoiding heap limit.
	// https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm
	for (List<Sample__c> samples : [SELECT Id, Score__c FROM Sample__c WHERE Id =: sampleDates.keySet()]) {
		for (Sample__c sample : samples) {
			sample.Score__c = sampleDates.get(sample.Id);
		}
		
		Database.update(samples);
	}
}

Let me know if worked.


Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello, 

I am afraid you cannot update a CreatedDate field as it is read-only. Moreover, you should bulkify your triggers instead of explicitly access only the first record in the Trigger.new collection. Find out more about here: 

https://developer.salesforce.com/page/Apex_Code_Best_Practices (https://developer.salesforce.com/page/Apex_Code_Best_Practices" target="_blank)


Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Arash TeimoupoorArash Teimoupoor
Hi, I'm not trying to update the CreatedDate, I'm trying to update the custom field(Score__c) with the CreatedDate value when a new task is created.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior

Hello,

So are you trying to get the CreatedDate of a Task an set it in the Score__c of Sample__c. Did I get it right?
Arash TeimoupoorArash Teimoupoor
Exactly
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

In that case you should try this:
 
trigger UpdatePresDate on Task(after insert){
	
	Map<Id, Datetime> sampleDates = new Map<Id, Datetime>();
	
	for (Task tsk : Trigger.new) {
		if (tsk.WhatId.getSObjectType() == Sample__c.sObjectType)
			sampleDates.put(tsk.WhatId, tsk.CreatedDate);
	}
	
	// Mass updates records avoiding heap limit.
	// https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm
	for (List<Sample__c> samples : [SELECT Id, Score__c FROM Sample__c WHERE Id =: sampleDates.keySet()]) {
		for (Sample__c sample : samples) {
			sample.Score__c = sampleDates.get(sample.Id);
		}
		
		Database.update(samples);
	}
}

Let me know if worked.


Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
This was selected as the best answer
Arash TeimoupoorArash Teimoupoor
Thanks Man, it is working perfect
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Glad to have helped!