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
Santro652Santro652 

Before record creation

There are 3 objects Accounts --MD--Procss unit--MD--Royalty contact;(Master Detail) I want to pull the account name into royalty contact object in a field so that i can apply lookup filter to another field name contact(Lookup to contact object) so that only account related contacts show up:

I have copied few written triggers and tried to modify this but not able to get through in the last section:Please can anyone check the trigger and let me know the syntax and correct this.. Many Thanks for all of you.. 

 

trigger acname on Royalty_Contacts__c (before insert, before update) { 

Id[] accIds = new Id[]{}; 

for (Royalty_Contacts__c c : trigger.new)accIds.add(c.Process_Unit__r.Id); 

for(Process_Unit__c acc : [Select Id, Account__c from Process_Unit__c where Id in :accIds]){

c.Account_Name__c=acc.Account__c;

}

 

Error:variable does not exist:c.Account_Name__c

I am not a developer so i have a feeble idea of what i am doing. I do not know if this trigger will work even after its corrected, so if anyone can explain the syntax of what is written it will bw hepfull:

I know line1 of trigger.

IbigfootIbigfoot
Hi,

This error is saying thatyou do not have a custom field Account_Name__c defined on the Royalty_Contacts__c object.

Perhaps a trigger is not the best option here, how about creating the Account_Name__c as a fomula field and insering the value this way? You cn reference up to 5 objects up a relationship heirarchy in this way. (Plus, you dont need to write code!)

Cheers,
Santro652Santro652

The reason i am going with the trigger instead of a formula is : in the object Royalty contact object i have a lookup to contacts and i want to display all the contacts related to the account (Lookup filter)and this is not possible if i write a formula. regarding the feild not present i must have written the last step wrongly which i am not able to figure out.

vishal@forcevishal@force

trigger acname on Royalty_Contacts__c (before insert, before update) { 

Id[] accIds = new Id[]{}; 

for (Royalty_Contacts__c c : trigger.new)accIds.add(c.Process_Unit__r.Id); 

for(Process_Unit__c acc : [Select Id, Account__c from Process_Unit__c where Id in :accIds]){

c.Account_Name__c=acc.Account__c;

}



In the above code, the scope of your Royalty_Contacts loop ends as soon as you add Process Unit Id's in accIds. And then you are trying to refer to it's field in a different for loop (inside Process_Unit__c), that's the reason it is showing you an error there.

Santro652Santro652

Let me tell you my requirement: I want to populate one field from process unit object into Royalty contact object when a new royalty contact is created: The fields name in process unit is Account__c(Account name,lookup) and the field in royalty contact is Account_Name__c(Text field) Can anyone help me out by giving the right code.