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

To give you some background, I have a parent object called Concept_Master_Contact__c and child object called Concept_Email__c

 I am trying to write a tirgger that would update the Unique_person ID on Concept_Email__c when it is changed in Concept_Master_Contact__c

Any help with fixing the trigger below will be appreciated. 

Problem: initial term of field expression must be a concrete sobject: List<Concept_Master_Contact>

trigger updateFields on Concept_Email__c (before update) {
    
    //The parent Object
    List<Concept_Master_Contact__c> conmast = new List<Concept_Master_Contact__c>();
    //The child object
    public List<Concept_Email__c> conemail{get;set;}
    
    for (Concept_Email__c obj: trigger.new){
    //if the Unique person ID changes on the parent object, change the person ID on the child object to match the parent object
    If (conemail.Unique_Person_ID__c != conmast.Unique_Person_ID__c){
            conmast.Unique_Person_ID__c = conemail.Unique_Person_ID__c;
        }
    }
    
}
Best Answer chosen by Gavin Britto
Anirudh SinghAnirudh Singh
Oh sorry Gavin, it should be 
parentIds.add(emailRecord.Contact__c);

It should be working now.

Please let me know if this helps.
If yes, please mark the Question as Solved.


Thanks and Regards,
Anirudh Singh
 

All Answers

Anirudh SinghAnirudh Singh
Hi Gavin,

There are a few mistakes in the Trigger. I have re-written it and explained the code. I hope it helps.

Important: Please change FieldAPINameForParent__c accordingly. 
The FieldAPINameForParent__c should be the API Name of the field on Concept_Email__c object linking to the Parent Concept_Master_Contact__c
trigger updateFields on Concept_Email__c (before update)
{
    //Fetch all the Parent Ids in this Set. Set is used for having unique values.
	Set<Id> parentIds=new Set<Id>();
	
	//Iterate the Concept_Email__c records.
	//Add the Parent Ids into the parentIds Set.
	for(Concept_Email__c emailRecord: Trigger.New)
	{
		//The FieldAPINameForParent__c should be the API Name of the field on Concept_Email__c object linking to the Parent Concept_Master_Contact__c
		//(Important: Please change FieldAPINameForParent__c accordingly.)
		parentIds.(emailRecord.FieldAPINameForParent__c);
	}
    
    //This list will hold the parent Object records.
    List<Concept_Master_Contact__c> conmast = new List<Concept_Master_Contact__c>();
	conmast=[SELECT Unique_Person_ID__c FROM Concept_Master_Contact__c WHERE Id IN: parentIds];
	
    //This list will hold the Concept_Email__c records to be updated.
    List<Concept_Email__c> conemail=new List<Concept_Email__c>();
    
	//Iterate the Concept_Email__c records
    for(Concept_Email__c obj: trigger.new)
	{
		//Iterate the Parent records
		for(Concept_Master_Contact__c parentRecord: conmast)
		{
			//Match if the obj(Concept_Email__c) record and the parentRecord(Concept_Master_Contact__c) Id matches.
			//If yes, then check if the Unique_Person_ID__c field on obj(Concept_Email__c) is not equal to Unique_Person_ID__c field on parentRecord(Concept_Master_Contact__c)
            //(Important: Please change FieldAPINameForParent__c accordingly.)
			if(obj.FieldAPINameForParent__c==parentRecord.Id && obj.Unique_Person_ID__c != parentRecord.Unique_Person_ID__c)
			{
				//Populate the value from parentRecord(Concept_Master_Contact__c) to obj(Concept_Email__c) record.
				obj.Unique_Person_ID__c = parentRecord.Unique_Person_ID__c;
			}
		}
    }
}

Please let me know if this helps.
If yes, please mark the Question as Solved.


Thanks and Regards,
Anirudh Singh
Gavin BrittoGavin Britto

Hi Anirudh,

Thank you so much for the detailed explanation and the help you've provided. I understand the explanation well. Only challenge I am running into is that line 11:

parentIds.(emailRecord.Contact__c);

has a Method does not exist or incorrect signature: [Set<Id>].(Id)

Any idea on how to get around this?
Anirudh SinghAnirudh Singh
Oh sorry Gavin, it should be 
parentIds.add(emailRecord.Contact__c);

It should be working now.

Please let me know if this helps.
If yes, please mark the Question as Solved.


Thanks and Regards,
Anirudh Singh
 
This was selected as the best answer