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
QasimQasim 

Trigger on Contact to create a task with a few conditions

Hi

I am very new to Apex and this is my first code so bound to have school boy errors, would appreciate help and direction:

Need to create a trigger on Contact, on create and update, where Number_Field__c is greater than 30 (This is a formula field that is created to accumulate the days passed since a certain form was submitted , which is a date field.

Task need to be created with some data; Subject, Priority, Status, Contact Name, Task to be assigned to Contact Owner

Here is the code which does create a task, but doesnt check the condition and docesnt insert the Contact Name in the Related to field:

1
2
3
4
5
6
7
8
9
10
11
trigger QM on Contact (after insert, after update) {
for (Contact Con : Trigger.New) {
List <Contact> DBSCheck = new List<Contact>();
DBSCheck = [SELECT Id, Name FROM Contact WHERE Number_Field__c > 30];
Task DBStask = New Task ();
DBSTask.Subject = 'Overdue DBS';
DBSTask.Priority = 'Normal';
DBSTask.Status = 'In Pogress';
DBStask.WhoId = (Con.Name);
Insert DBSTask;
}}


Thanks a lot for your help.


 




 
Best Answer chosen by Qasim
AnjithKumarAnjithKumar
Can you try following code.
 
trigger QM on Contact (after insert, after update) {
	List<Task> taskToInsert = new list<Task>();
	for (Contact Con : Trigger.New) {
		//con.recalculateFormulas();
		if(con.Number_Field__c>30){
		Task DBStask = New Task ();
		DBSTask.Subject = 'Overdue DBS';
		DBSTask.Priority = 'Normal';
		DBSTask.Status = 'In Pogress';
		DBStask.WhoId = con.id;
                DBStask.OwnerId=con.ownerId;
		taskToInsert.add(DBSTask);
		}
	}
	if(taskToInsert.size()>0){
		insert taskToInsert;
	}
}


 

All Answers

AnjithKumarAnjithKumar
Dear Qasim,

You no need to query contact again. You need assign whoid and whatid fields

WHOID  is the user
WHATID is the  record id.

You have to bulkify your code and you are not supposed to query inside for loop as it hits the governer limits.

One more best practice is you can implement tiggerhandler class(helper/factory class). For more info on trigger handler classes

https://developer.salesforce.com/forums/?id=906F000000093cKIAQ
 
trigger QM on Contact (after insert, after update) {
	List<Task> taskToInsert = new list<Task>();
	for (Contact Con : Trigger.New) {
		con.recalculateFormulas();
		if(con.Number_Field__c>30){
		Task DBStask = New Task ();
		DBSTask.Subject = 'Overdue DBS';
		DBSTask.Priority = 'Normal';
		DBSTask.Status = 'In Pogress';
		DBStask.WhoId = con.OwnerId;
		DBStask.WhatId=con.Id;
		taskToInsert.add(DBSTask);
		}
	}
	if(taskToInsert.size()>0){
		insert taskToInsert;
	}
}


Hope it helps you. Let me know you need more help.

Thanks,
Anjit 
 
AnjithKumarAnjithKumar
Sorry small mistake,

WhoId is lead/contact.
OwnerId is ID of the User who owns the record. Label is Assigned To ID.
trigger QM on Contact (after insert, after update) {
	List<Task> taskToInsert = new list<Task>();
	for (Contact Con : Trigger.New) {
		con.recalculateFormulas();
		if(con.Number_Field__c>30){
		Task DBStask = New Task ();
		DBSTask.Subject = 'Overdue DBS';
		DBSTask.Priority = 'Normal';
		DBSTask.Status = 'In Pogress';
		DBStask.WhoId = con.id;
                DBStask.OwnerId=con.ownerId;
		taskToInsert.add(DBSTask);
		}
	}
	if(taskToInsert.size()>0){
		insert taskToInsert;
	}
}

 
QasimQasim
Thanks for your help, on running this code I am getting this error on both Create and Update contact:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger QM caused an unexpected exception, contact your administrator: QM: execution of AfterUpdate caused by: System.UnexpectedException: Unable to create/update fields: LastModifiedDate, CreatedById, IsDeleted, IsEmailBounced, CreatedDate, SystemModstamp, LastModifiedById. Please check the security settings of this field and verify that it is read/write for your profile or permission set.: ()
AnjithKumarAnjithKumar
Can you try following code.
 
trigger QM on Contact (after insert, after update) {
	List<Task> taskToInsert = new list<Task>();
	for (Contact Con : Trigger.New) {
		//con.recalculateFormulas();
		if(con.Number_Field__c>30){
		Task DBStask = New Task ();
		DBSTask.Subject = 'Overdue DBS';
		DBSTask.Priority = 'Normal';
		DBSTask.Status = 'In Pogress';
		DBStask.WhoId = con.id;
                DBStask.OwnerId=con.ownerId;
		taskToInsert.add(DBSTask);
		}
	}
	if(taskToInsert.size()>0){
		insert taskToInsert;
	}
}


 
This was selected as the best answer
QasimQasim
Briliant stuff, thanks a lot.