• Scott Messner
  • NEWBIE
  • 10 Points
  • Member since 2017
  • Application Developer
  • Nicolet College

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I'd like to create an Apex trigger to copy the values from two fields on a custom object to two fields on the Contact object. The custom object hed__Affiliation__c has a lookup to Contact. I'm doing this so we can pull these field into Pardot as our version of Pardot does not allow custom objects. I'm using Apex triggers instead of Process Builder because although Process Builder can copy the files, it cannot handle deleting, which will be my next step.

Here is my code. This works put it populates the account ID for educational institution instead of the name. I'm pulling from the organization field on hed__Affiliation__c which is a lookup to account. If I try to assign education institution to a.hed__Account__c.Name I receive a "A non foreign key field cannot be referenced in a path expression" error when trying to compile. How can I pull the name of the account instead of the ID?

Thanks!
 
trigger CopySchoolToContact on hed__Affiliation__c (after insert, after update) {
List<Contact> conList = new List<Contact>();
    
    for (hed__Affiliation__c a : [SELECT Id,hed__Contact__c,hed__Account__c,Educational_Institution_Class_Year__c FROM hed__Affiliation__c
                     WHERE Id IN :Trigger.New]) {
    
        conList.add(new Contact(Id=a.hed__Contact__c,
            					Educational_Institution__c=a.hed__Account__c, // This populates ID of account and not Name. 
                                Educational_Institution_Class_Year__c=a.Educational_Institution_Class_Year__c)); 
    }
    
    try{
        update conList;
    }
    catch(Exception E){
        system.debug('Error thrown is: ' + E.getMessage());
    }
}



 
I'd like to create an Apex trigger to copy the values from two fields on a custom object to two fields on the Contact object. The custom object hed__Affiliation__c has a lookup to Contact. I'm doing this so we can pull these field into Pardot as our version of Pardot does not allow custom objects. I'm using Apex triggers instead of Process Builder because although Process Builder can copy the files, it cannot handle deleting, which will be my next step.

Here is my code. This works put it populates the account ID for educational institution instead of the name. I'm pulling from the organization field on hed__Affiliation__c which is a lookup to account. If I try to assign education institution to a.hed__Account__c.Name I receive a "A non foreign key field cannot be referenced in a path expression" error when trying to compile. How can I pull the name of the account instead of the ID?

Thanks!
 
trigger CopySchoolToContact on hed__Affiliation__c (after insert, after update) {
List<Contact> conList = new List<Contact>();
    
    for (hed__Affiliation__c a : [SELECT Id,hed__Contact__c,hed__Account__c,Educational_Institution_Class_Year__c FROM hed__Affiliation__c
                     WHERE Id IN :Trigger.New]) {
    
        conList.add(new Contact(Id=a.hed__Contact__c,
            					Educational_Institution__c=a.hed__Account__c, // This populates ID of account and not Name. 
                                Educational_Institution_Class_Year__c=a.Educational_Institution_Class_Year__c)); 
    }
    
    try{
        update conList;
    }
    catch(Exception E){
        system.debug('Error thrown is: ' + E.getMessage());
    }
}