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
Gavin BrittoGavin Britto 

Field Update Trigger 2

To give you some background, Contact is the parent object and Concept_Email__c is the child object. 

 I am trying to write a tirgger that would update the
The arctics ID which is named "Concept_email_Arctics_ID_MSE__c" on Concept_Email__c object when it is changed 

Below is the code snippet that I've started. Any help with fixing the trigger below will be appreciated.

As of now I am getting a syntactical error "Invalid field contact for sObject contact"

 
trigger UpdateArchticIDEmail on Contact (After Update) {
for(Contact cont: Trigger.New)
 {
 //Select Arctics ID from Contact__c object
 Contact ArchID=[select Archtics_ID__c from Contact where id=:cont.Contact];
 //Select Arctics ID from Concept_Email__c  object
 list<Concept_Email__c> cec=[select Concept_email_Arctics_ID_MSE__c from Concept_Email__c WHERE Contact=:cont.id];
 List<Concept_Email__c> listemail=new List<Concept_Email__c>();
 for(Concept_Email__c uid:cec)
 {
 //Assign Updated Details from Contact__c object to Concept_Email__c Object
 uid.Concept_email_Arctics_ID_MSE__c=ArchID.Archtics_ID__c;
 listemail.add(uid);
 }
 update listemail; 
 }
}

 
Best Answer chosen by Gavin Britto
David ZhuDavid Zhu

open the field setting of Concept_Email__c object, find the api name of the field which Concept_Email__c is related to Contact (parent) object. replace contact__c with that api name.

All Answers

Gavin BrittoGavin Britto
The Syntactical error is on line 5
"Invalid field contact for sObject contact"
KaranrajKaranraj
Gavin - You have to mention the API name of the field in the apex code, which you can find in the object fields sections.
Your code doesn't meet the salesforce best practice, you should not use SOQL query inside the for loop.
Try the below code 
trigger UpdateArchticIDEmail on Contact (After Update) {

List<Concept_Email__c> listemail=new List<Concept_Email__c>();
for(Concept_Email__c uid : select Concept_email_Arctics_ID_MSE__c,Contact__c from Concept_Email__c WHERE Contact__C IN:Trigger.NewMap.keySet()){
    uid.uid.Concept_email_Arctics_ID_MSE__c = Trigger.NewMap.get(Contact__c.Id).Archtics_ID__c
    listemail.add(uid);
}
update listemail;
}
I strongly recommend you to take a look into the trailhead modules to learn about apex trigger and other custmozation topic - https://developer.salesforce.com/trailhead
 
Gavin BrittoGavin Britto
Hi Karanraj,

Thank you very muh for the suggestion and for enlightening me on the best practice. I ran the code you sent me and I got an "unexpected token select" error on line 4. Is it because the select should go inside brackets?
KaranrajKaranraj
Gavin - Try the updated code below
 
trigger UpdateArchticIDEmail on Contact (After Update) {

List<Concept_Email__c> listemail=new List<Concept_Email__c>();
for(Concept_Email__c uid : [select Concept_email_Arctics_ID_MSE__c,Contact__c from Concept_Email__c WHERE Contact__C IN:Trigger.NewMap.keySet()]){
    uid.uid.Concept_email_Arctics_ID_MSE__c = Trigger.NewMap.get(Contact__c.Id).Archtics_ID__c
    listemail.add(uid);
}
update listemail;
}

 
David ZhuDavid Zhu
I think Karanraj has done the heavy lifting job. The code would work with fixing with typos.
trigger UpdateArchticIDEmail on Contact (After Update) {

List<Concept_Email__c> listemail=new List<Concept_Email__c>();
for(Concept_Email__c uid : [select Concept_email_Arctics_ID_MSE__c,Contact__c from Concept_Email__c WHERE Contact__C IN:Trigger.NewMap.keySet()])
{
    uid.Concept_email_Arctics_ID_MSE__c = Trigger.NewMap.get(Contact__c.Id).Archtics_ID__c;
    listemail.add(uid);
}
update listemail;
}

 
Gavin BrittoGavin Britto
Karanraj & David,

Thank You've both. I whole heartledly appreciate your contributions. Both of your code samples have the same Variable does not exist Contact__c.Id error on Line 6

What could possibly be wrong here? 
David ZhuDavid Zhu

open the field setting of Concept_Email__c object, find the api name of the field which Concept_Email__c is related to Contact (parent) object. replace contact__c with that api name.
This was selected as the best answer