You need to sign in to do that
Don't have an account?
Qasim
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.
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.
All Answers
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
Hope it helps you. Let me know you need more help.
Thanks,
Anjit
WhoId is lead/contact.
OwnerId is ID of the User who owns the record. Label is Assigned To ID.
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.: ()