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
SFDC n12SFDC n12 

Apex trigger help needed

Hi,

I need help on the following req

1) I am having a custom field called as "Dealer Principal__c" in my custom object called as "Account Exceptions__c"

2) There is a lookup on my custom object called as Account__c which is a lookup to the account object


My req is i want to populate my custom field  "Dealer Principal__c"  on Account exception__c custom object with  the contact first name and last name of the contact  related to the account that i select if the contact title is set as "Dealer Principal"


if the account record type is group account , the child accounts contact is set which is having 
contact title as "Dealer Principal"

Help me with the requirement please

Thanks in Advance

 

Best Answer chosen by SFDC n12
RamuRamu (Salesforce Developers) 
Hope this should resolve the issue
 
trigger updateaccountexceptions on Account_Exceptions__c(after insert, after update){
	map<id,string> dealercontacts=new map<id,string>();
	set<id> accids=new set<id>();
	
	for(account_exceptions__c acex:trigger.new){
		accids.add(acex.account__c);
	}
	
	List<contact> cons=new List<contact>([select accountid,firstname,lastname from contact where title='Dealer Principal' and accountid=:accids]);
	
	for(Contact con:cons){
		string conname=con.firstname+' '+con.lastname;
		dealercontacts.put(con.accountid,conname);
	}
	
	list<account_exceptions__c> acexlisttoupdate =new list<account_exceptions__c>();
	
	for(account_exceptions__c acex:trigger.new){
		account_exceptions__c accex1=new account_exceptions__c();
		if(dealdercontacts.containskey(acex.account__c)){
		accex1.id=acex.id;
		accex1.Dealer Principal__c=dealdercontacts.get(acex.account__c);
		acexlisttoupdate.add(accex1);
		}
	}
	
	update acexlisttoupdate;
}

 

All Answers

RamuRamu (Salesforce Developers) 
Hi, I have a quick questin. What if there are multiple contact records with the same title as "Dealer Principal", would there be such instances?
 
SFDC n12SFDC n12
No there wont be such instances , every account has mostly one contact asscociated to it
RamuRamu (Salesforce Developers) 
Try this out and let me know if any errors 
 
select accountid,firstname,lastname from contact where title='Buyer' and accountid='0019000000FeqSR'

trigger updateaccountexceptions on Account_Exceptions__c(after insert, after update){
map<id,string> dealercontacts=map<id,string>();
set<id> accids=new set<id>();

for(account_exceptions__c acex:trigger.new){
accids.add(acex.account__c);
}

List<contact> cons=new List<contact>([select accountid,firstname,lastname from contact where title='Dealer Principal' and accountid=:accids]);

for(Contact con:cons){
string conname=con.firstname+' '+con.lastname;
dealercontacts.put(con.accountid,conname);
}

list<account_exceptions__c> acexlisttoupdate =new list<account_exceptions__c>();

for(account_exceptions__c acex:trigger.new){
if(dealdercontacts.containskey(acex.account__c){
acex.Dealer Principal__c=dealdercontacts.get(acex.account__c);
acexlisttoupdate.add(acex);
}
}

update acexlisttoupdate;
}

 
SFDC n12SFDC n12
I am getting the following error


Error: Compile Error: unexpected token: '{' at line 19 column 47

trigger updateaccountexceptions on AccountExceptions__c (after insert, after update){
map<id,string> dealercontacts= new map<id,string>();
set<id> accids=new set<id>();

for(AccountExceptions__c acex:trigger.new){
accids.add(acex.Account__c);
}

List<contact> cons=new List<contact>([select accountid,firstname,lastname from contact where title='Dealer Principal' and accountid=:accids]);

for(Contact con:cons){
string conname=con.firstname+' '+con.lastname;
dealercontacts.put(con.accountid,conname);
}

list<AccountExceptions__c> acexlisttoupdate =new list<AccountExceptions__c>();

for(AccountExceptions__c acex:trigger.new){
if(dealdercontacts.containskey(acex.Account__c){
acex.Dealer_Principal_s__c=dealdercontacts.get(acex.Account__c);
acexlisttoupdate.add(acex);
}
}

update acexlisttoupdate;
}
RamuRamu (Salesforce Developers) 
I made some corrections. Try this out
 
trigger updateaccountexceptions on Account_Exceptions__c(after insert, after update){
	map<id,string> dealercontacts=new map<id,string>();
	set<id> accids=new set<id>();
	
	for(account_exceptions__c acex:trigger.new){
		accids.add(acex.account__c);
	}
	
	List<contact> cons=new List<contact>([select accountid,firstname,lastname from contact where title='Dealer Principal' and accountid=:accids]);
	
	for(Contact con:cons){
		string conname=con.firstname+' '+con.lastname;
		dealercontacts.put(con.accountid,conname);
	}
	
	list<account_exceptions__c> acexlisttoupdate =new list<account_exceptions__c>();
	
	for(account_exceptions__c acex:trigger.new){
		if(dealdercontacts.containskey(acex.account__c)){
		acex.Dealer Principal__c=dealdercontacts.get(acex.account__c);
		acexlisttoupdate.add(acex);
		}
	}
	
	update acexlisttoupdate;
}

 
SFDC n12SFDC n12
The trigger got saved , but its not saving the actual record


when i enter the values on click on save i m getting the following error,



Apex trigger AF_updateaccountexceptions caused an unexpected exception, contact your administrator: AF_updateaccountexceptions: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.AF_updateaccountexceptions: line 20, column 1


        acex.Dealer_Principal_s__c=dealercontacts.get(acex.Account__c);




 
RamuRamu (Salesforce Developers) 
Hope this should resolve the issue
 
trigger updateaccountexceptions on Account_Exceptions__c(after insert, after update){
	map<id,string> dealercontacts=new map<id,string>();
	set<id> accids=new set<id>();
	
	for(account_exceptions__c acex:trigger.new){
		accids.add(acex.account__c);
	}
	
	List<contact> cons=new List<contact>([select accountid,firstname,lastname from contact where title='Dealer Principal' and accountid=:accids]);
	
	for(Contact con:cons){
		string conname=con.firstname+' '+con.lastname;
		dealercontacts.put(con.accountid,conname);
	}
	
	list<account_exceptions__c> acexlisttoupdate =new list<account_exceptions__c>();
	
	for(account_exceptions__c acex:trigger.new){
		account_exceptions__c accex1=new account_exceptions__c();
		if(dealdercontacts.containskey(acex.account__c)){
		accex1.id=acex.id;
		accex1.Dealer Principal__c=dealdercontacts.get(acex.account__c);
		acexlisttoupdate.add(accex1);
		}
	}
	
	update acexlisttoupdate;
}

 
This was selected as the best answer
SFDC n12SFDC n12
Thanks dude it worked