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
MikeCloudMikeCloud 

Apex trigger for populating Account lookup field

I have a custom object - Case Management, when the Contact lookup field is entered I want the Account lookup field associated with that Contact to be automatically entered.  I am new at this so here's what I have,  

Case_Management__c (before insert, before update) { for(Case_Management__c co: Trigger.new){ if (co.Contact__c != null){ co.Account__c = 'Yes'; } } }

 

This needs to be edited, but I am lost.

Best Answer chosen by Admin (Salesforce Developers) 
Chris ZhuangChris Zhuang

Use this code

trigger CasemanagementAccount on caseManagement__c (before insert,before update){
if trigger.isBefore &&(trigger.isInsert||trigger.isUpdate))
{
  set<Id> ContactIds = new set<ID>();
  for(caseMangement__c cm: trigger.new)
  { 
	contactIds.add(cm.contact__c);
  }
  Map<Id, Id> contactToAccountMap = new Map<Id, Id>();
  for(contact c :[select Id, AccountId from contact where id in: contactIds])
  {
       contactToAccountMap.put(c.Id,c.AccountId);
  }
  for(caseMangement__c cm: trigger.new)
  {
       cm.Account__c = contactToAccountMap.get(cm.contact__c);
  }
  
}
}

All Answers

s_k_as_k_a

Hi,

 

Try This Code

trigger CasemanagementAccount on caseManagement__c (before insert,before update){
if trigger.isBefore &&(trigger.isInsert||trigger.isUpdate))
{
  set<Id> ContactIds = new set<ID>();
  for(caseMangement__c cm: trigger.new)
  { 
	contactIds.add(cm.contact__c);
  }
  Map<Id, Id> contactToAccountMap = new Map<Id, Id>();
  for(contact c :[select Id, Account from contact where id in: contactIds])
  {
       contactToAccountMap.put(c.Id,c.account);
  }
  for(caseMangement__c cm: trigger.new)
  {
       cm.Account__c = contactToAccountMap.get(cm.contact__c);
  }
  
}
}

 

MikeCloudMikeCloud

 

Error: Compile Error: No such column 'Account' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 10 column 22
Chris ZhuangChris Zhuang

Use this code

trigger CasemanagementAccount on caseManagement__c (before insert,before update){
if trigger.isBefore &&(trigger.isInsert||trigger.isUpdate))
{
  set<Id> ContactIds = new set<ID>();
  for(caseMangement__c cm: trigger.new)
  { 
	contactIds.add(cm.contact__c);
  }
  Map<Id, Id> contactToAccountMap = new Map<Id, Id>();
  for(contact c :[select Id, AccountId from contact where id in: contactIds])
  {
       contactToAccountMap.put(c.Id,c.AccountId);
  }
  for(caseMangement__c cm: trigger.new)
  {
       cm.Account__c = contactToAccountMap.get(cm.contact__c);
  }
  
}
}
This was selected as the best answer
MikeCloudMikeCloud

Just some typos then worked perfectly thanks you guys this was for a non-profit

DanLTranDanLTran

Hey Mike, 

Could you share the test classes you wrote for this trigger, im just getting into this whole Apex malarkey and I have altered the triggers to work in my sandbox with some different custom objects but not managed to get the test methods down just yet - its also for a non-profit so good causes all round!

Danny