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
apexnewbieapexnewbie 

Need Help with Trigger

Stumbling through a Trigger, found a few examples but I'm pretty sure I butchered them. I have a custom junction object called Contact_Notes__c. When a new one is created, I would like the date created field (or now()) to be added to a field on the standard Contact object called Last_Note_Date__c.  Any help is greatly appreciated. If I can at least find out if I'm on the right track anyway.

 

trigger UpdateContactNoteDate on Contact_Notes__c (After Insert) {

 

list<Contact_Notes__c> theContactNotes = new list<Contact_Notes__c>(); //create a list called theContactNotes
map<ID, Today> cnID = new map<ID, Today>(); //add two columns to theContactNotes list, ID and todays date

 

for (Contact_Notes__c nc:trigger.new) {
theContactNotes.add(l);
if (nc.Contact_Name__c != null) { //If the Contact_Name__c is not null in Contact_Notes__c put Contact_Name__c in the cnID column of theContactNotes list
cnID.put(nc.Contact_Name__c);
}

 

// Loop through the main list of Contacts

 

list<Contacts> theContacts = new list<Contacts>(); // List containing Contact records
for (Contact c:theContacts) {
if(cnID.get(Today.put(c.Last_Note_Date__c))) //if a contact matches theContactNotes list cnID column put Today in the Last_Note_Date__c field on Contact record
}
}


Best Answer chosen by Admin (Salesforce Developers) 
apexnewbieapexnewbie

This helped more than you can know. I really appreciate it and have learned a little along the way. Here is my final trigger and test class for anyone else that may be struggling a little.

******************************TRIGGER*******************************************************************************************************************************

trigger UpdateContactNoteDate on Contact_Notes__c (after insert) {

List<Id> contactIds = new List<Id> (); //create a List to hold all of the contact IDs called contactIds

for (Contact_Notes__c cn:trigger.new) {
if (cn.Contact_Name__c != null) //when a contact note is created and the contact_name is not blank
contactIds.add(cn.Contact_Name__c); //add the contact name to the contactIds list
}

List<Contact> theContacts = new List<Contact> (); //create a list of all the fields in the contact object called theContacts
theContacts = [select Id,Last_Note_Date__c from Contact where Id IN: contactIds]; //get the contact Id and Last note date from the contact where
// the Id is found on the contactIds list

if(theContacts.size() > 0) { //this is making sure that theContacts list has something in it
for (Contact c : theContacts) {
c.Last_Note_Date__c = System.Today(); //put the system date in the last note date field on the contact record

}
update theContacts; //update the contact record
}
}

************************************************************************************************************************************************************************

**********************************************TEST CLASS************************************************************************************************************

 

@istest
public class UpdateContactNoteDate {

public static testMethod void myUpdateContactNoteDate(){

//Create an Account
Account a = new Account (Name = 'Amy Company');
insert a;

// Create a Contact
Contact c = new Contact(FirstName = 'Amy', LastName = 'Amy');
insert c;

// Create a Contact Note
Contact_Notes__c cn = new Contact_Notes__c(Contact_Name__c = '003Z0000003RWzu', Note__c = 'a0HZ0000001QfoG');
insert cn;

//test to make sure the Last Note Date on the contact record contains the date that the note was created
system.assertEquals(c.Last_Note_Date__c, cn.CreatedDate);

}
}

All Answers

goabhigogoabhigo

First of all is there any relationship between Contact_Notes__c and Contact?

 

list<Contacts> theContacts = new list<Contacts>(); // List containing Contact records --- will not contain any record because you are just initializing the list !!

apexnewbieapexnewbie

Contact_Notes__c is a custom object that has a lookup to the Contact record.

goabhigogoabhigo

Try this:

 

trigger UpdateContactNoteDate on Contact_Notes__c (After Insert) {
 
 List<Id> contactIds = new List<Id> ();
 
 for (Contact_Notes__c cn:trigger.new) {
  if (cn.Contact_Name__c != null)
   contactIds.add(nc.Contact_Name__c);
 }

 List<Contacts> theContacts = new List<Contacts> ();
 theContacts = [select Id,Last_Note_Date__c from Contact where Id IN:  contactIds];
 if(theContacts.size() > 0) {
  for (Contact c : theContacts) {
   c.Last_Note_Date__c = System.Today();
  }
 }
}

 

 I believe Contact_Name__c is the lookup field.

apexnewbieapexnewbie

This helped more than you can know. I really appreciate it and have learned a little along the way. Here is my final trigger and test class for anyone else that may be struggling a little.

******************************TRIGGER*******************************************************************************************************************************

trigger UpdateContactNoteDate on Contact_Notes__c (after insert) {

List<Id> contactIds = new List<Id> (); //create a List to hold all of the contact IDs called contactIds

for (Contact_Notes__c cn:trigger.new) {
if (cn.Contact_Name__c != null) //when a contact note is created and the contact_name is not blank
contactIds.add(cn.Contact_Name__c); //add the contact name to the contactIds list
}

List<Contact> theContacts = new List<Contact> (); //create a list of all the fields in the contact object called theContacts
theContacts = [select Id,Last_Note_Date__c from Contact where Id IN: contactIds]; //get the contact Id and Last note date from the contact where
// the Id is found on the contactIds list

if(theContacts.size() > 0) { //this is making sure that theContacts list has something in it
for (Contact c : theContacts) {
c.Last_Note_Date__c = System.Today(); //put the system date in the last note date field on the contact record

}
update theContacts; //update the contact record
}
}

************************************************************************************************************************************************************************

**********************************************TEST CLASS************************************************************************************************************

 

@istest
public class UpdateContactNoteDate {

public static testMethod void myUpdateContactNoteDate(){

//Create an Account
Account a = new Account (Name = 'Amy Company');
insert a;

// Create a Contact
Contact c = new Contact(FirstName = 'Amy', LastName = 'Amy');
insert c;

// Create a Contact Note
Contact_Notes__c cn = new Contact_Notes__c(Contact_Name__c = '003Z0000003RWzu', Note__c = 'a0HZ0000001QfoG');
insert cn;

//test to make sure the Last Note Date on the contact record contains the date that the note was created
system.assertEquals(c.Last_Note_Date__c, cn.CreatedDate);

}
}

This was selected as the best answer