• KevinRussell
  • NEWBIE
  • 50 Points
  • Member since 2013

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 12
    Questions
  • 25
    Replies

I manually insert a record inserted into a custom object.  One of the fields is a "Lookup(Contact)" to the Contact object.

This object does not require the Conttact field to be populated when manually entering and saving a record, it saves fine.  However, when the record is saved I fire a trigger to run some code.  if the Contact field is not populated, the trigger throws an error and it's code does not run.  What is the way to solve this?

 

1) I could tell my users that they are required to populate that field, maybe with a dummy Contact.

 

2) I could trap the error and somehow allow the trigger code execution to continue.

 

I'm using Try Catch. 

This is my error:

 

08:35:09.479 (479495000)|EXCEPTION_THROWN|[118]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

 

What could I put here to catch and allow the record to update anyway?

catch (Exception e) {
}

 

Thanks for any help.

 

Kevin

 

I'm trying to loop through a local list a number of times.  It seems like it works when I have a single record but not when I have more.  I suspect that my multiple 'for' loops don't start from the begining of the list each time.

Can anyone tell me where I'm going wrong?

 

Thanks,

 

Kevin

 

Integer SpeakingRecordCount;
Boolean PastLeadershipBoard;
Boolean EmeritusTrustee;
Boolean LeadershipBoard;


try {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();
map< id, account > accounts = new map< id, account >();
       
if (trigger.isinsert || trigger.isupdate) 
    {
     
       // Create trigger for new or selected npe5__Affiliation__c  record
       for(npe5__Affiliation__c  record:trigger.new)        
       {

        // Build local list of Affiliation records 
       List<npe5__Affiliation__c > affl = new List <npe5__Affiliation__c>();
       affl=[Select id, npe5__Contact__c, npe5__Organization__c ,Type__c, npe5__Status__c, npe5__Role__c from npe5__Affiliation__c  
       WHERE npe5__Contact__c = :record.npe5__Contact__c AND npe5__Organization__c = :record.npe5__Organization__c ];

        // Loop through the local Affiliation list to parse results for Contact Record


        for (npe5__Affiliation__c li: affl) {
            if (li.npe5__Contact__c == record.npe5__Contact__c 
                && li.Type__c.contains('Board') && li.npe5__Status__c == 'Current' )
                LeadershipBoard = TRUE;
            else {
                LeadershipBoard = FALSE;
            }
        }
        
        for (npe5__Affiliation__c li: affl) {
            if (li.npe5__Contact__c == record.npe5__Contact__c 
                && li.Type__c.contains('Board') && li.npe5__Status__c == 'Former' )
                PastLeadershipBoard = TRUE;
            else {
                PastLeadershipBoard = FALSE;
            }
        }
        
     // Construct Contact record update with results  
        contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Leadership_Board__C = LeadershipBoard, Past_Leadership_Board__c = PastLeadershipBoard ));               

     // Update Contact record
        update contacts.values(); 


        }   
    }

 

 

I'm trying to query against a map of local records I populated and getting the error:

Error: sObject type 'Affiliations' is not supported.

 

 // Contact Affiliation count
RecordCount = [select count() from Affiliations where npe5__Contact__c = :record.npe5__Contact__c ];

 

What might I be doing incorrectly?

 

Thanks,

 

Kevin

 

Partial code listing:

 // Create trigger for new or selected npe5__Affiliation__c  record
       for(npe5__Affiliation__c  record:trigger.new)        
       {
        
        // Store temporary Affiliation records
        map< id, npe5__Affiliation__c > Affiliations = new map< id, npe5__Affiliation__c >();
     
        // Building local list
        for (npe5__Affiliation__c afl : [Select Id, npe5__Contact__c, npe5__Organization__c, Type__c, npe5__Status__c, npe5__Role__c from npe5__Affiliation__c  
            WHERE npe5__Contact__c = :record.npe5__Contact__c AND npe5__Organization__c = :record.npe5__Organization__c ] ){
            Affiliations.put(afl.id, afl);
        }
      

           // Contact Affiliation Speaker count
           RecordCount = [select count() from Affiliations where npe5__Contact__c = :record.npe5__Contact__c ]; 

        

 

I'm trying to populate a local map.  I'm getting the error:

 

Compile Error: Initial term of field expression must be a concrete SObject: MAP<Id,npe5__Affiliation__c> at line 33 column 13 

 

What am I doing incorrectly?

 

Thanks,

 

Kevin

        // Store temporary Affiliation records
        map< id, npe5__Affiliation__c > Affiliations = new map< id, npe5__Affiliation__c >();
     
        // Building local list
        for (npe5__Affiliation__c afl : [Select npe5__Contact__c, npe5__Organization__c, Type__c, npe5__Status__c, npe5__Role__c from npe5__Affiliation__c  
            WHERE npe5__Contact__c = :record.npe5__Contact__c AND npe5__Organization__c = :record.npe5__Organization__c ] ){
            Affiliations.npe5__Contact__c = afl.npe5__Contact__c;
            Affiliations.npe5__Organization__c = afl.npe5__Organization__c;
            Affiliations.Type__c = afl.Type__c;
            Affiliations.npe5__Status__c = afl.npe5__Status__c;
            Affiliations.npe5__Role__c = afl.npe5__Role__c;
            // Add Affiliation records
           Affiliations.add(Affiliations);
        }

 

So, I'm getting the error that I have too many queries running on a trigger when I try to import records into an object, using Informatica.  The trigger works fine when I manually enter records from Salesforce.

 

I have 7 queries in this trigger against the same object.  I would like to refactor this code so I have one query, pulling selected Affiliation records into a local map, then run my 7 queries against the local map.

 

Can someone please guide me how to do this.

 

Thanks,

 

Kevin

 

For reference, I have the complete working trigger below:

trigger AccountContactAffiliations on npe5__Affiliation__c   (after insert, after update, after delete) {

// Update Multiple Contact and Account fields: TBD, when an Affiliation record is inserted, updated or deleted. 

Integer SpeakingRecordCount;
Integer LeadershipBoardCount;
Integer PastLeadershipBoardCount;
Integer EmeritusTrusteeCount;

Boolean PastLeadershipBoard;
Boolean EmeritusTrustee;
Boolean LeadershipBoard;
try {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();

// Store Account record ID
map< id, account > accounts = new map< id, account >(); if (trigger.isinsert || trigger.isupdate) { // Create trigger for new or selected npe5__Affiliation__c record for(npe5__Affiliation__c record:trigger.new) { // Contact Affiliation Speaker count SpeakingRecordCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c = 'Speaker']; // Contact Affiliation Leadership Board count LeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Current']; if(LeadershipBoardCount >= 1) LeadershipBoard = TRUE; Else { LeadershipBoard = FALSE; } // Contact Affiliation Past Leadership Board count PastLeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Former']; if(PastLeadershipBoardCount >= 1) PastLeadershipBoard = TRUE; Else { PastLeadershipBoard = FALSE; } // Contact Affiliation Emeritus Trustee count EmeritusTrusteeCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Role__c LIKE :'%'+'Emeritus'+'%']; if(EmeritusTrusteeCount >= 1) EmeritusTrustee = TRUE; Else { EmeritusTrustee = FALSE; } // Contact data to update contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, AffAASpeakingCnt__c = SpeakingRecordCount, Past_Leadership_Board__c = PastLeadershipBoard, Emeritus_Trustee__c = EmeritusTrustee, Leadership_Board__C = LeadershipBoard )); // Update Contact record update contacts.values(); // Account Affiliation Leadership Board count LeadershipBoardcount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Current']; if(LeadershipBoardCount >= 1) LeadershipBoard = TRUE; Else { LeadershipBoard = FALSE; } // Account Affiliation Past Leadership Board count PastLeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Former']; if(PastLeadershipBoardCount >= 1) PastLeadershipBoard = TRUE; Else { PastLeadershipBoard = FALSE; } // Account Affiliation Emeritus Trustee count EmeritusTrusteeCount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Role__c LIKE :'%'+'Emeritus'+'%']; if(EmeritusTrusteeCount >= 1) EmeritusTrustee = TRUE; Else { EmeritusTrustee = FALSE; } // Account data to update accounts.put(record.npe5__Organization__c, new account(id=record.npe5__Organization__c, Leadership_Board_s__c = LeadershipBoard, Past_Leadership_Board_s__c = PastLeadershipBoard, Emeritus_Trustee__c = EmeritusTrustee )); // Update Account record update accounts.values(); } } if (trigger.isdelete) { // Create trigger for deleted npe5__Affiliation__c record for(npe5__Affiliation__c record:trigger.old) { // Contact Affiliation Speaker count SpeakingRecordCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c = 'Speaker']; // Contact Affiliation Leadership Board count LeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Current']; if(LeadershipBoardCount >= 1) LeadershipBoard = TRUE; Else { LeadershipBoard = FALSE; } // Contact Affiliation Past Leadership Board count PastLeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Former']; if(PastLeadershipBoardCount >= 1) PastLeadershipBoard = TRUE; Else { PastLeadershipBoard = FALSE; } // Contact Affiliation Emeritus Trustee count EmeritusTrusteeCount = [select count() from npe5__Affiliation__c where npe5__Contact__c = :record.npe5__Contact__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Role__c LIKE :'%'+'Emeritus'+'%']; if(EmeritusTrusteeCount >= 1) EmeritusTrustee = TRUE; Else { EmeritusTrustee = FALSE; } // Contact data to update contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, AffAASpeakingCnt__c = SpeakingRecordCount, Past_Leadership_Board__c = PastLeadershipBoard, Emeritus_Trustee__c = EmeritusTrustee, Leadership_Board__C = LeadershipBoard )); // Update Contact record update contacts.values(); // Account Affiliation Leadership Board count LeadershipBoardcount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Current']; if(LeadershipBoardCount >= 1) LeadershipBoard = TRUE; Else { LeadershipBoard = FALSE; } // Account Affiliation Past Leadership Board count PastLeadershipBoardCount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Status__c = 'Former']; if(PastLeadershipBoardCount >= 1) PastLeadershipBoard = TRUE; Else { PastLeadershipBoard = FALSE; } // Account Affiliation Emeritus Trustee count EmeritusTrusteeCount = [select count() from npe5__Affiliation__c where npe5__Organization__c = :record.npe5__Organization__c AND Type__c LIKE :'%'+'Board'+'%' AND npe5__Role__c LIKE :'%'+'Emeritus'+'%']; if(EmeritusTrusteeCount >= 1) EmeritusTrustee = TRUE; Else { EmeritusTrustee = FALSE; } // Account data to update accounts.put(record.npe5__Organization__c, new account(id=record.npe5__Organization__c, Leadership_Board_s__c = LeadershipBoard, Past_Leadership_Board_s__c = PastLeadershipBoard, Emeritus_Trustee__c = EmeritusTrustee )); // Update Account record update accounts.values(); } } } catch (Exception e) { } }

 

Hello, I need to know how to reference a Contact field within the trigger of a child object.  I need to reference the Contact field in an IF THEN statement.

 

Thanks!

 

Kevin

 

Here is more detail:

Trigger needs to reference Customer field and custom object.

I'm doing a record count on an object related to Contacts: npe5__Affiliation__c
This works great.  However, I need to use a Customer field in my IF statement.
I want to change it from:

if(recordcount >= 1)
to various versions of:
if(recordcount >= 1 && Customer.Guest_Speaker__c = TRUE) 


Integer recordcount = [select count() from npe5__Affiliation__c where npe5__contact__c = :record.npe5__contact__c AND Type__c = 'Speaker'];

if(recordcount >= 1)
{
	// Update checkbox field on the Contact record to TRUE                
	contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = TRUE)); 
Else {
       // Update checkbox field on the Contact record to TRUE                
	contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = FALSE));                
	}
	update contacts.values();   
}

 

 

 

 

 

I want to set a checkbox on the current contact record.

My trigger queries a related Interaction object to perform a record count.

After counting records,I set the check box either True or False.

I can't figure how to properly reference the current Contact record to update the checkbox field,

 

I'm getting this error:

Compile Error: Invalid field contact for SObject Contact at line 19 column 31

 

On this line:

contacts.put(record.contact, new contact(id = record.id, Attended_Event_Within_Past_Year__c = TRUE));                    

 

The problem reference: "record.contact"

 

Thanks for any help.

 

Kevin

 

 

The entire trigger.

trigger ContactsAttendedEventPastYear on Contact (after update) {
    try {
    // Your code here
    
        date todaysDate = date.today();
        date CutoffDate = todaysDate.addDays(-365);
        
        // Store Contact record ID
        map< id, contact > contacts = new map< id, contact >();
           
        //look through the data
        For (Contact record : trigger.new)
        {
        
            Integer recordcount = [select count() from Interaction__c where Contact__c = :record.id  AND Start_Date_Time__c > :CutoffDate and Selected_Sub_type__c = 'Attend Event' AND (Interaction_Type__c = 'Alumni Interaction' OR Interaction_Type__c = 'Advancement Interaction' )];
         
            if(recordcount >= 1)
            
                 contacts.put(record.contact, new contact(id = record.id, Attended_Event_Within_Past_Year__c = TRUE));                     
            Else {
                 //contacts.put(record.contact__c, new contact(id=record.contact__c, Attended_Event_Within_Past_Year__c = FALSE));    
            
            }               
            
        //update contacts.values(); 
        
        }
        
    } catch (Exception e) {
    // Generic exception handling code here

    }
}

 

I'm trying to count records having a date within the past year.  I'm having trouble with the date comparrison.

My error: Compile Error: unexpected token: 'CutoffDate' at line.........

 

I don't know how to compare the DateTime record field: Start_Date_Time__c against a date variable.

 

Thanks for any help.

 

Kevin

 

trigger Contacts_Attended_Event_Within_Past_Year_Checkbox_test on Interaction__c (after insert, after update) {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();

    date todaysDate = date.today();
    date CutoffDate = todaysDate.addDays(-365);

Integer recordcount = [select count() from Interaction__c where Start_Date_Time__c > CutoffDate ];    

 

I want to query the selected Contact's Interaction records with a Sub_type__c = 'Attend Event' AND Start_Date_Time__c within one year of the current date.
If I have a record count = 0 I know there has not been a record of attending an Event in the past year.  I will set the Contact checkbox to FALSE.
If I have a record count of >= 1, they came to an event within the past year.  I will set the Contact checkbox to TRUE.

 

Can anyone give me a hand for the counting records part of the code?  Here is what I have so far:

 

Thanks

 

Kevin

// Update Contact field: "Attended_Event_Within_Past_Year__c" to TRUE when a new Interaction__c record is inserted or updated. 
trigger Contacts_Attended_Event_Within_Past_Year_Checkbox_test on Interaction__c (after insert, after update) {

// Will store Contact record ID
map< id, contact > contacts = new map< id, contact >();

integer recordcount = 0;

// Create trigger for new or selected Interaction__c record
for(Interaction__c record:trigger.new)        



//I want to query the selected Contact's Interaction records with a Sub_type__c = 'Attend Event' AND Start_Date_Time__c within one year of the current date.
//If I have a record count = 0 I know there has not been a record of attending an Event in the past year.  I will set the Contact checkbox to FALSE.
//If I have a record count of >= 1, they came to an event within the past year.  I will set the Contact checkbox to TRUE.


if(recordcount >= 1)

     // Update checkbox field on the Contact record to TRUE
     contacts.put(record.contact__c, new contact(id=record.contact__c, Attended_Event_Within_Past_Year__c = TRUE));    
Else {
     // Update checkbox field on the Contact record to TRUE
     contacts.put(record.contact__c, new contact(id=record.contact__c, Attended_Event_Within_Past_Year__c = FALSE));    

}     


update contacts.values(); 

}

 

 

 

 

So, new at this..... Having said that...

I wrote a trigger that sets a Contact field checkbox to True.  I created a test for the trigger and the test ran but there is no code coverage.  What am I doing wrong, how do I connect my trigger test to my real trigger to get coverage?

 

Thanks!

 

Here is my trigger:

// Update Contact field: "Guest_Speaker__c" to TRUE when a new Interaction__c record is inserted or updated. 
// Interaction__c is not a directly related child object of Contact.
trigger Contacts_Guest_Speaker_Checkbox on Interaction__c (after insert, after update) {

// Will store Contact record ID
map< id, contact > contacts = new map< id, contact >();

// Create trigger for new or selected Interaction__c record
for(Interaction__c record:trigger.new)        

if(record.Selected_Sub_type__c == 'Speaker')

     // Update checkbox field on the Contact record to TRUE
     contacts.put(record.contact__c, new contact(id=record.contact__c, Guest_Speaker__C = TRUE));      

update contacts.values(); 

}

 

Here is my attempt at writing a test.  The test does compile, run and passes but it has no code coverage.

@isTest
private class Contacts_Guest_Speaker_CheckboxTest
{
    //set condition for True statement.
    static TestMethod void Test0_StatementTrue()
    {  
        string test0Value = 'Speaker';      
        updateCheckBox(test0Value);       
    }

    //set condition for False statement.    
    static TestMethod void Test1_StatementFalse()
    {
        string test1Value = 'Instructor';       
        updateCheckBox(test1Value);       
    }

    //set checkbox to True if value is 'Speaker', False otherwise.
    private static void updateCheckBox(String inputString)
     {
         contact con = new contact();
         con.Id = '003J000000O2KR0';         
         if (inputstring == 'Speaker')
            { 
                 con.Guest_Speaker__C = True;
            } else {
                 con.Guest_Speaker__C = False;
                    }                                         
         update con;
    }
}

 Results:

Apex Test Result Detail 

Time Started 3/26/2013 10:40 AM 
Class Contacts_Guest_Speaker_CheckboxTest 
Method Name Test1_StatementFalse 
Pass/Fail Pass 
Error Message   
Stack Trace  

 Code Coverage:

Code Coverage 

Contacts_Guest_Speaker_Checkbox (Code Covered: 0%)

 line  source 
 1   // Update Contact field: "Guest_Speaker__c" to TRUE when a new Interaction__c record is inserted or updated.  
 2   // Interaction__c is not a directly related child object of Contact. 
 3   trigger Contacts_Guest_Speaker_Checkbox on Interaction__c (after insert, after update) { 
 4    
 5   // Will store Contact record ID 
 6   map< id, contact > contacts = new map< id, contact >(); 
 7    
 8   // Create trigger for new or selected Interaction__c record 
 9   for(Interaction__c record:trigger.new)         
 10    
 11   if(record.Selected_Sub_type__c == 'Speaker') 
 12    
 13        // Update checkbox field on the Contact record to TRUE 
 14        contacts.put(record.contact__c, new contact(id=record.contact__c, Guest_Speaker__C = TRUE));       
 15    
 16   update contacts.values();  
 17    
 18   } 

 

New at this, so, I create a trigger to send email if certain Contact fields have been updated.  To my surprise, I find I cannot deploy or create the trigger in the production company, I need to create a trigger test.  So I find an example and experiment but I'm not getting it.  Could someone help with how I can create an APEX Class test for this trigger, just enought so I get the idea to finish it?

 

Thanks if you can.

 

Kevin

 

This is the begginging of may last attempt at writing an APEX test.

@isTest
private class ContactChangeCodeCoverageTestTest
 {


    //this section of the test should send no email because the field values 
    //have not changed.    
    static TestMethod void Test0_TestNoFieldChanges()
    {
        string OldEmail = 'OldEmail@test.com';
        sendContactEmailAlertValue(OldEmail);    
        

                 
    }


    //this section of the test should send email because the field values 
    //have changed.    
    static TestMethod void Test1_TestWithFieldChanges()
    {
        string NewEmail = 'NewEmail@test.com';
        sendContactEmailAlertValue(NewEmail);  
    }

    private static void sendContactEmailAlertValue(String EmailValue)
    {
    
        if (EmailValue == 'OldEmail@test.com')
            {
                //don't send email            
            } 
            else 
                {    
                    //send email      
                }             
                
    }
        


}

 

 

trigger ContactChangeCodeCoverageTest on Contact (after update) {


try {
// Your code here

    //holds value before change
    date OldBirthdate;
    String OldEmail;
    String OldHomePhone;
    String OldMobilePhone;
    String OldPhone;
    String OldTitle;
    string OldMailingAddress;
    string OldOtherAddress;
    
    date NewBirthdate;
    String NewEmail;
    String NewHomePhone;
    String NewMobilePhone;
    String NewPhone;
    String NewTitle;
    string NewMailingAddress;
    string NewOtherAddress;
    
    String oldAccountName;
    String newAccountName;
    
    string GraduationYear;
    string FieldChanges;
    string ContactName;
    Boolean SendEmail;
    
    //I don't want to send the email unless the fields I'm checking for have changed.    
    //In other words, non-significant Contact fields can change and I won't send Contact Changed spam.    
    SendEmail = False;    
    
    //create a set to hold all of the parent account ids that we need to get names for
    Set<Id> accIds=new Set<id>();
    //create a set for contacts that need checked so we don't have to go through the whole list again
    List<Contact> checkMe = new List<Contact>();
    
    
    //look at the data
    for (Contact con : trigger.new)
    {
    
        NewBirthdate = con.Birthdate;
        NewEmail = con.Email;
        NewHomePhone = con.HomePhone;
        NewMobilePhone = con.MobilePhone;
        NewPhone = con.Phone;
        NewTitle = con.Title;   
        GraduationYear = con.Graduation_Year__c;
        ContactName = con.FirstName;
        ContactName = ContactName + ' ' + con.LastName;
        NewMailingAddress = con.MailingStreet + ', ' + con.MailingCity + ', ' + con.MailingState  + ', ' + con.MailingCountry;
        NewOtherAddress = con.OtherStreet + ', ' + con.OtherCity + ', ' + con.OtherState  + ', ' + con.OtherCountry;        
    
    
        //This looks to see if the current Account ID matches the 
        //previous Account ID    
        if(con.AccountId != trigger.oldMap.get(con.Id).AccountId){
              
            //add the new account to the set
            accIds.add(con.accountId);
                
            //add the old account to the set
            accIds.add(trigger.oldMap.get(con.Id).AccountId);
                
            //add this contact with a changed account to the list to be checked later on
            checkMe.add(con);
            
            //go get the accounts that we need names for and put them in a map that that is keyed by the accountID
            Map<Id, Account> accMap = new Map<Id,Account>([select id, Name from Account where id in :accIds]);    
    
                 for (Contact c : checkMe) {
                    
                    //get the old account id
                    Id oldAcctID = trigger.oldMap.get(con.Id).AccountId;
                    
                    //get the name of the old account
                     oldAccountName = accMap.get(oldAcctID).Name;
                     
                     //get the new account id
                     Id newAcctID = c.AccountId;
                     
                     //get the new Account name
                     newAccountName = accMap.get(newAcctID).Name;
                     
                     //do you comparison from here 
    
                }
        }
    }
    
    for (Contact c : Trigger.old) {
    
            OldBirthdate = c.Birthdate;
            OldEmail = c.Email;
            OldHomePhone = c.HomePhone;
            OldMobilePhone = c.MobilePhone;
            OldPhone = c.Phone;
            OldTitle = c.Title;
            OldMailingAddress = c.MailingStreet + ', ' + c.MailingCity + ', ' + c.MailingState + ', ' + c.MailingCountry;       
            OldOtherAddress = c.OtherStreet + ', ' + c.OtherCity + ', ' + c.OtherState  + ', ' + c.OtherCountry;                
    }
    
    FieldChanges = 'Alumni Contact Information for ' + ContactName + ' has changed:' + '<br>';
    
    FieldChanges = FieldChanges + '<br>';
    
    if (OldMailingAddress != NewMailingAddress) {
    FieldChanges = FieldChanges + 'Mailing Address: ' + OldMailingAddress + ' --> ' + NewMailingAddress + '<br>';
    SendEmail = True;    
    }
    
    if (OldOtherAddress != NewOtherAddress) {
    FieldChanges = FieldChanges + 'Other Address: ' + OldOtherAddress + ' --> ' + NewOtherAddress + '<br>';
    SendEmail = True;    
    }
    
    if (oldAccountName != newAccountName) {
    FieldChanges = FieldChanges + 'Account: ' + oldAccountName + ' --> ' + newAccountName + '<br>';
    SendEmail = True;
    }
    
    if (OldTitle != NewTitle) {
    FieldChanges = FieldChanges + 'Title: ' + OldTitle + ' --> ' + NewTitle + '<br>';
    SendEmail = True;
    }
    
    if (OldEmail != NewEmail) {
    FieldChanges = FieldChanges + 'Email: ' + OldEmail + ' --> ' + NewEmail + '<br>';
    SendEmail = True;
    }
    
    if (OldPhone != NewPhone) {
    FieldChanges = FieldChanges + 'Phone: ' + OldPhone + ' --> ' + NewPhone + '<br>';
    SendEmail = True;
    }
    
    if (OldBirthdate != NewBirthdate) {
    FieldChanges = FieldChanges + 'Birthdate: ' + OldBirthdate + ' --> ' + NewBirthdate + '<br>';
    SendEmail = True;
    }
    
    if (OldHomePhone != NewHomePhone) {
    FieldChanges = FieldChanges + 'Home Phone: ' + OldHomePhone + ' --> ' + NewHomePhone + '<br>';
    SendEmail = True;
    }
    
    if (OldMobilePhone != NewMobilePhone) {
    FieldChanges = FieldChanges + 'Mobile Phone: ' + OldMobilePhone + ' --> ' + NewMobilePhone + '<br>';
    SendEmail = True;
    }
    
    
    if (GraduationYear <> null && SendEmail ) {
      
        // Send Email Notification
            
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'testaddress@testaddress.com'};
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('emailaddress@test.com');
        mail.setSenderDisplayName('My Name');
        mail.setSubject('Alumn: ' + ContactName + ' Contact Information changed.');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setHtmlBody(FieldChanges);            
        //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });       
                
        }
    
    } catch (Exception e) {
    // Generic exception handling code here

    }

}

 

I'm trying to capture the new and old contact Account names into string fields for string comparrison, and sending in an email.  The code below is a test trigger on a contact record.  The email sent to me after changing the contact's account to another give me values of null.  If I do this, for example, with the c.Title field I don't have this issue, I can pull the value into a string.

 

The email message: "Account field changed: Old Account: null New Account null"

 

Thanks for any help.

 

Kevin

================

 

The trigger:

 

trigger EmailAccountName on Contact (after update) {

string OldAccount;

string NewAccount;

 

for (Contact c : Trigger.old) {

          OldAccount = c.account.name;

}

 

for (Contact c : Trigger.new) {

          NewAccount = c.account.name;

}

 

            // Send Email Notification

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            String[] toAddresses = new String[] {'my email address'};

            mail.setToAddresses(toAddresses);

            mail.setReplyTo('my email address');

            mail.setSenderDisplayName('Me');

            mail.setSubject('Alumni Contact Information has changed.');

            mail.setBccSender(false);

            mail.setUseSignature(false);

            mail.setHtmlBody('Account field changed: Old Account: ' + OldAccount + ' New Account ' + NewAccount );

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });     

}

I'm trying to loop through a local list a number of times.  It seems like it works when I have a single record but not when I have more.  I suspect that my multiple 'for' loops don't start from the begining of the list each time.

Can anyone tell me where I'm going wrong?

 

Thanks,

 

Kevin

 

Integer SpeakingRecordCount;
Boolean PastLeadershipBoard;
Boolean EmeritusTrustee;
Boolean LeadershipBoard;


try {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();
map< id, account > accounts = new map< id, account >();
       
if (trigger.isinsert || trigger.isupdate) 
    {
     
       // Create trigger for new or selected npe5__Affiliation__c  record
       for(npe5__Affiliation__c  record:trigger.new)        
       {

        // Build local list of Affiliation records 
       List<npe5__Affiliation__c > affl = new List <npe5__Affiliation__c>();
       affl=[Select id, npe5__Contact__c, npe5__Organization__c ,Type__c, npe5__Status__c, npe5__Role__c from npe5__Affiliation__c  
       WHERE npe5__Contact__c = :record.npe5__Contact__c AND npe5__Organization__c = :record.npe5__Organization__c ];

        // Loop through the local Affiliation list to parse results for Contact Record


        for (npe5__Affiliation__c li: affl) {
            if (li.npe5__Contact__c == record.npe5__Contact__c 
                && li.Type__c.contains('Board') && li.npe5__Status__c == 'Current' )
                LeadershipBoard = TRUE;
            else {
                LeadershipBoard = FALSE;
            }
        }
        
        for (npe5__Affiliation__c li: affl) {
            if (li.npe5__Contact__c == record.npe5__Contact__c 
                && li.Type__c.contains('Board') && li.npe5__Status__c == 'Former' )
                PastLeadershipBoard = TRUE;
            else {
                PastLeadershipBoard = FALSE;
            }
        }
        
     // Construct Contact record update with results  
        contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Leadership_Board__C = LeadershipBoard, Past_Leadership_Board__c = PastLeadershipBoard ));               

     // Update Contact record
        update contacts.values(); 


        }   
    }

 

 

I'm trying to query against a map of local records I populated and getting the error:

Error: sObject type 'Affiliations' is not supported.

 

 // Contact Affiliation count
RecordCount = [select count() from Affiliations where npe5__Contact__c = :record.npe5__Contact__c ];

 

What might I be doing incorrectly?

 

Thanks,

 

Kevin

 

Partial code listing:

 // Create trigger for new or selected npe5__Affiliation__c  record
       for(npe5__Affiliation__c  record:trigger.new)        
       {
        
        // Store temporary Affiliation records
        map< id, npe5__Affiliation__c > Affiliations = new map< id, npe5__Affiliation__c >();
     
        // Building local list
        for (npe5__Affiliation__c afl : [Select Id, npe5__Contact__c, npe5__Organization__c, Type__c, npe5__Status__c, npe5__Role__c from npe5__Affiliation__c  
            WHERE npe5__Contact__c = :record.npe5__Contact__c AND npe5__Organization__c = :record.npe5__Organization__c ] ){
            Affiliations.put(afl.id, afl);
        }
      

           // Contact Affiliation Speaker count
           RecordCount = [select count() from Affiliations where npe5__Contact__c = :record.npe5__Contact__c ]; 

        

 

I'm trying to populate a local map.  I'm getting the error:

 

Compile Error: Initial term of field expression must be a concrete SObject: MAP<Id,npe5__Affiliation__c> at line 33 column 13 

 

What am I doing incorrectly?

 

Thanks,

 

Kevin

        // Store temporary Affiliation records
        map< id, npe5__Affiliation__c > Affiliations = new map< id, npe5__Affiliation__c >();
     
        // Building local list
        for (npe5__Affiliation__c afl : [Select npe5__Contact__c, npe5__Organization__c, Type__c, npe5__Status__c, npe5__Role__c from npe5__Affiliation__c  
            WHERE npe5__Contact__c = :record.npe5__Contact__c AND npe5__Organization__c = :record.npe5__Organization__c ] ){
            Affiliations.npe5__Contact__c = afl.npe5__Contact__c;
            Affiliations.npe5__Organization__c = afl.npe5__Organization__c;
            Affiliations.Type__c = afl.Type__c;
            Affiliations.npe5__Status__c = afl.npe5__Status__c;
            Affiliations.npe5__Role__c = afl.npe5__Role__c;
            // Add Affiliation records
           Affiliations.add(Affiliations);
        }

 

Hello, I need to know how to reference a Contact field within the trigger of a child object.  I need to reference the Contact field in an IF THEN statement.

 

Thanks!

 

Kevin

 

Here is more detail:

Trigger needs to reference Customer field and custom object.

I'm doing a record count on an object related to Contacts: npe5__Affiliation__c
This works great.  However, I need to use a Customer field in my IF statement.
I want to change it from:

if(recordcount >= 1)
to various versions of:
if(recordcount >= 1 && Customer.Guest_Speaker__c = TRUE) 


Integer recordcount = [select count() from npe5__Affiliation__c where npe5__contact__c = :record.npe5__contact__c AND Type__c = 'Speaker'];

if(recordcount >= 1)
{
	// Update checkbox field on the Contact record to TRUE                
	contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = TRUE)); 
Else {
       // Update checkbox field on the Contact record to TRUE                
	contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Guest_Speaker__c = FALSE));                
	}
	update contacts.values();   
}

 

 

 

 

 

I want to set a checkbox on the current contact record.

My trigger queries a related Interaction object to perform a record count.

After counting records,I set the check box either True or False.

I can't figure how to properly reference the current Contact record to update the checkbox field,

 

I'm getting this error:

Compile Error: Invalid field contact for SObject Contact at line 19 column 31

 

On this line:

contacts.put(record.contact, new contact(id = record.id, Attended_Event_Within_Past_Year__c = TRUE));                    

 

The problem reference: "record.contact"

 

Thanks for any help.

 

Kevin

 

 

The entire trigger.

trigger ContactsAttendedEventPastYear on Contact (after update) {
    try {
    // Your code here
    
        date todaysDate = date.today();
        date CutoffDate = todaysDate.addDays(-365);
        
        // Store Contact record ID
        map< id, contact > contacts = new map< id, contact >();
           
        //look through the data
        For (Contact record : trigger.new)
        {
        
            Integer recordcount = [select count() from Interaction__c where Contact__c = :record.id  AND Start_Date_Time__c > :CutoffDate and Selected_Sub_type__c = 'Attend Event' AND (Interaction_Type__c = 'Alumni Interaction' OR Interaction_Type__c = 'Advancement Interaction' )];
         
            if(recordcount >= 1)
            
                 contacts.put(record.contact, new contact(id = record.id, Attended_Event_Within_Past_Year__c = TRUE));                     
            Else {
                 //contacts.put(record.contact__c, new contact(id=record.contact__c, Attended_Event_Within_Past_Year__c = FALSE));    
            
            }               
            
        //update contacts.values(); 
        
        }
        
    } catch (Exception e) {
    // Generic exception handling code here

    }
}

 

I'm trying to count records having a date within the past year.  I'm having trouble with the date comparrison.

My error: Compile Error: unexpected token: 'CutoffDate' at line.........

 

I don't know how to compare the DateTime record field: Start_Date_Time__c against a date variable.

 

Thanks for any help.

 

Kevin

 

trigger Contacts_Attended_Event_Within_Past_Year_Checkbox_test on Interaction__c (after insert, after update) {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();

    date todaysDate = date.today();
    date CutoffDate = todaysDate.addDays(-365);

Integer recordcount = [select count() from Interaction__c where Start_Date_Time__c > CutoffDate ];    

 

So, new at this..... Having said that...

I wrote a trigger that sets a Contact field checkbox to True.  I created a test for the trigger and the test ran but there is no code coverage.  What am I doing wrong, how do I connect my trigger test to my real trigger to get coverage?

 

Thanks!

 

Here is my trigger:

// Update Contact field: "Guest_Speaker__c" to TRUE when a new Interaction__c record is inserted or updated. 
// Interaction__c is not a directly related child object of Contact.
trigger Contacts_Guest_Speaker_Checkbox on Interaction__c (after insert, after update) {

// Will store Contact record ID
map< id, contact > contacts = new map< id, contact >();

// Create trigger for new or selected Interaction__c record
for(Interaction__c record:trigger.new)        

if(record.Selected_Sub_type__c == 'Speaker')

     // Update checkbox field on the Contact record to TRUE
     contacts.put(record.contact__c, new contact(id=record.contact__c, Guest_Speaker__C = TRUE));      

update contacts.values(); 

}

 

Here is my attempt at writing a test.  The test does compile, run and passes but it has no code coverage.

@isTest
private class Contacts_Guest_Speaker_CheckboxTest
{
    //set condition for True statement.
    static TestMethod void Test0_StatementTrue()
    {  
        string test0Value = 'Speaker';      
        updateCheckBox(test0Value);       
    }

    //set condition for False statement.    
    static TestMethod void Test1_StatementFalse()
    {
        string test1Value = 'Instructor';       
        updateCheckBox(test1Value);       
    }

    //set checkbox to True if value is 'Speaker', False otherwise.
    private static void updateCheckBox(String inputString)
     {
         contact con = new contact();
         con.Id = '003J000000O2KR0';         
         if (inputstring == 'Speaker')
            { 
                 con.Guest_Speaker__C = True;
            } else {
                 con.Guest_Speaker__C = False;
                    }                                         
         update con;
    }
}

 Results:

Apex Test Result Detail 

Time Started 3/26/2013 10:40 AM 
Class Contacts_Guest_Speaker_CheckboxTest 
Method Name Test1_StatementFalse 
Pass/Fail Pass 
Error Message   
Stack Trace  

 Code Coverage:

Code Coverage 

Contacts_Guest_Speaker_Checkbox (Code Covered: 0%)

 line  source 
 1   // Update Contact field: "Guest_Speaker__c" to TRUE when a new Interaction__c record is inserted or updated.  
 2   // Interaction__c is not a directly related child object of Contact. 
 3   trigger Contacts_Guest_Speaker_Checkbox on Interaction__c (after insert, after update) { 
 4    
 5   // Will store Contact record ID 
 6   map< id, contact > contacts = new map< id, contact >(); 
 7    
 8   // Create trigger for new or selected Interaction__c record 
 9   for(Interaction__c record:trigger.new)         
 10    
 11   if(record.Selected_Sub_type__c == 'Speaker') 
 12    
 13        // Update checkbox field on the Contact record to TRUE 
 14        contacts.put(record.contact__c, new contact(id=record.contact__c, Guest_Speaker__C = TRUE));       
 15    
 16   update contacts.values();  
 17    
 18   }