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
Stuart Turnbull 9Stuart Turnbull 9 

Apex Trigger to update an Account field from a Lookup on the same Account

We are trying to Copy the Lookup field value on an Acount into another field on that same Account. 

User-added image


The error occurs at Line 6
Invalid field Account for SObject Account 

I'm new to Apex Programming, and a bit unsure if this is the right solution..I did some research, but only found Discussions on updating a Lookup from one Object to a field in another Object. Can someone offer further guidance? Are there any other articles out the there for copying a Lookup to another field in the same Object?
Neetu_BansalNeetu_Bansal
Hi Stuart,

At line 6, change the code like this:
Account.add(acct.Id);
Because you have created a Set<Id> and you are trying to pass invalid field. acct.Account as Account object doesn't have any Account field.

Also you might get exception at line 9, beacuse what is Account2.

Thanks,
Neetu
Krishna SambarajuKrishna Sambaraju
I didn't quite understand the scenario. Why do you want to copy the value from one field (Campus__c) to another field (Campus_For_Campaigns__c) on the same object?
Stuart Turnbull 9Stuart Turnbull 9
Our Campaign solution (Pardot) does not allow us to reference a Lookup field from the Acount. We need it to pull this value from a non Lookup field.
Stuart Turnbull 9Stuart Turnbull 9
Hi Neetu,

I adjusted the Code, updating Line 6, and correcting Account2 to Account in Line 9, and now recive the same message as before, but at line 5
    
if(acct.Account != null) {

Invalid field Account for SObject Account 

Thanks.

 
Neetu_BansalNeetu_Bansal
Hi Stuart,

Please update the code at line 5 also like this:
if( acct.Id != null )

Although you don't need to put that check, as Id will always be populated in after insert/update call.

Thanks,
Neetu
Stuart Turnbull 9Stuart Turnbull 9
Thanks Neetu,

I made the adjustment, and it now references an exception at Line 12

Initial term of field expression must be a concrete SObject: Set<Id>

I modilfied LIne 12 from

Campus__c from Account where ID IN: Account.Campus__c]);

To

Campus__c from Account where ID IN: ID]);

and now...

Variable does not exist: ID
Neetu_BansalNeetu_Bansal
Hi Stuart,

Is Campus__c is a look up field and Campus_For_Campaigns__c is Text field, which store Campus__c Id. If yes, You can achieve this by creating formula field. but if you need to write trigger. Try this code:
trigger CampusAcctPardotTrg on Account( before insert, before update )
{
	for( Account acc : trigger.new )
	{
		if( trigger.isInsert
			|| ( trigger.isUpdate && trigger.oldMap.get( acc.Id ).Campus__c != acc.Campus__c ))
		{
			acc.Campus_For_Campaigns__c = acc.Campus__c;
		}
	}
}

Thanks,
Neetu
Stuart Turnbull 9Stuart Turnbull 9
Hi Neetu,

Actually, it is not the ID, but the specific set of Account Names associated with the ID (based on RecordType).  

Thank you.
 
Krishna SambarajuKrishna Sambaraju
Hi Stuart,

If you want to bring the data from the lookup object (Campus__c) then there is no need for the trigger, you can create a formula field on the Account and get the values you need. Here is the example of the expression for the formula field.

Campus__r.Name

The above expression will display the value of the Name field of the lookup object (Campus__c).

You can change the field names, or concatenate other field values also as below.

Campus__r.Name + ' ' + Campus__r.field2 + ' ' + Campus__r.field3

Change the field names accordingly.

Hope this helps.