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
Jaynandan Prasad 8Jaynandan Prasad 8 

how to Auto-populate look up field

I need to auto-populate a lookup field(Account) on the case object with reference to the text value(having Account Name) on custom text field in same object. How to do this ? any trigger code may help..
Best Answer chosen by Jaynandan Prasad 8
ManojjenaManojjena
Hi ,

Are you able to save the trigger ,If no please let me know th error .
If yes trigger wil fall under recursion as it is both after insert and update .
So please follow below links to solve recursion error .

https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)  

Check if your copyAccount__c  field have value befoe insert context or not . Simply check by adding a debug in trigger with before insert .

If value is there in before context then use below code ,
 
trigger CopyAccount on Case (before  insert, before update) {
   Map<String, Id> mapAcc = new Map<String, Id>();
   Set<String> acNameSet = new Set<String>();
   for(Case cc: Trigger.new){
      acNameSet .add(cc.copyAccount__c);
      
   }
   For(Account acc :[Select Id,Name from Account where Name  in:acNameSet]){
        mapAcc.put(acc.Name,acc.Id);
    }
    for(Case cc:Trigger.new){
        if(!mapAcc.Isempty()){
         cc.AccountId = mapAcc.get(cc.copyAccount__c); 
       }
   }
}
Still you are facing the same issue please click on the field and check whether it is readonly or not if read only change to editable .

Let me know if it helps .
 

All Answers

Sunil PalSunil Pal
Hi Jaynandan,

Please let me know if you are looking for the same.

trigger CaseTrigger on Case (Before Insert, before update) {
  
  CaseTriggerHandler objCaseTriggerHandler = new CaseTriggerHandler();
  
  if(Trigger.isBefore) {
    
    if(trigger.isInsert) {
        objCaseTriggerHandler.onBeforeInsert(Trigger.new);
    }
    else if(trigger.isUpdate) {
      objCaseTriggerHandler.onBeforeUpdate(Trigger.new);
    }
  }
}

===================
public with sharing class LeadTriggerHandler {

    public void onBeforeInsert(List<Case> lstCaseInsert) {
        
        autopopulateAccount(lstCaseInsert);
    }
    
    public void onBeforeUpdate(List<Case> lstCaseINsert){
        
        autopopulateAccount(lstCaseInsert);
    }
    
    private void autopopulateAccount(List<Case> lstCaseInsert) {
        
        for(Case objCase : lstCaseInsert) {
            
            ObjCase.FieldNameNeedsToUpdate = objCase.FromWhereYouneedtotakeVasle;
        }
    }
}

Please let me know if it helps you 

Thanks 
Sunil
 
Jaynandan Prasad 8Jaynandan Prasad 8
here the trigger I wrote-- but its not working
Here, copyAccount__c is a custom text field in the case object that carries the account name. I need to auto-populate the actual account name in the case object with this value(in copyAccount__c) .
 
trigger CopyAccount on Case (after insert, after update) {

List<Id> acId = new List<Id>();

for(Case cc: Trigger.new){
 acId.add(cc.copyAccount__c);
    }

Map<Id, Account> mapAcc = new Map<Id, Account>([Select Name from Account where id in:acId]);

for(Case cc:trigger.new){
    if(!mapAcc.Isempty()){
      Account.Name = mapAcc.get(cc.Account.Name).Name.copyAccount__c;  
        
    }
    
    }

}





 
Jaynandan Prasad 8Jaynandan Prasad 8
Hi Sunil..

Getting below error from the handler class- which is 
public with sharing class CaseTriggerHandler {

    public void onBeforeInsert(List<Case> lstCaseInsert) {
        
        autopopulateAccount(lstCaseInsert);
    }
    
    public void onBeforeUpdate(List<Case> lstCaseINsert){
        
        autopopulateAccount(lstCaseInsert);
    }
    
    private void autopopulateAccount(List<Case> lstCaseInsert) {
        
        for(Case objCase : lstCaseInsert) {
            
            ObjCase.Account.Name = objCase.copyAccount__c;
        }
    }
}


Apex trigger CopyAccount caused an unexpected exception, contact your administrator: CopyAccount: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Class.CaseTriggerHandler.autopopulateAccount: line 17, column 1
 
ManojjenaManojjena
Hi Jayant ,

Please clarify below points .

1.You have  custom text field in Case  named copyAccount__c,How this value is ispopulating ,are you entering manually ?

2.Do you want to populated the account(Standrad Field ) in case  based on the above custom field ,if yes is your account name unique ?
if no it may match many account which account you need to populated .

Please clarify .

 
Jaynandan Prasad 8Jaynandan Prasad 8
Hi Manoj,

1. the value in custom text field- copyAccount__c is coming from S2S connection.. I am not entering manually.

2. Yes, I want to populate the standard Account field in the case object.  It should do a matching and populate the account ..as its unique. 
 
Sunil PalSunil Pal

HI Jaynandan 

In place Of ObjCase.Account.Name = objCase.copyAccount__c;
for(Case objCase : lstCaseInsert) {
           
     if(objCase.copyAccount__c != null ) {
            ObjCase.AccountId = objCase.copyAccount__c;
      }
   }



Thanks
Sunil
Jaynandan Prasad 8Jaynandan Prasad 8
Hi Manoj.

getting the below error-

Variable does not exist: cs.AccountId at line 11 column 6
ManojjenaManojjena
Hi ,

Try with below code .
 
trigger CopyAccount on Case (after insert, after update) {
   Set<id> caseIdSet=new Set<Id>();
   Map<String, Id> mapAcc = new Map<String, Id>();
   List<Case> caseListToupdate=new List<Case>();
   Set<String> acNameSet = new Set<String>();
   for(Case cc: Trigger.new){
      acNameSet .add(cc.copyAccount__c);
	   caseIdSet.add(cc.id);
   }
    For(Account acc :[Select Name from Account where Name  in:acNameSet]){
        mapAcc.put(acc.Name,acc.Id);
    }
    for(Case cc:[SELECT id,AccountId FROM Case WHERE id IN : caseIdSet]){
        if(!mapAcc.Isempty()){
         cc.AccountId = mapAcc.get(cc.copyAccount__c);  
         caseListToupdate.add(cc);
        }
   }
  try{
    update caseListToupdate;
  }catch(DMLException de ){
    System.debug(de);
  }
}

Let me know if it helps .

 
Jaynandan Prasad 8Jaynandan Prasad 8
Manoj,

Error: 
Trigger.CopyAccount: line 13, column 1 10:43:45.240 (240986099)|FATAL_ERROR|System.FinalException: Record is read-only
 
ManojjenaManojjena
Hi ,

Try below code plz .
 
trigger CopyAccount on Case (after insert, after update) {
   Set<id> caseIdSet=new Set<Id>();
   Map<String, Id> mapAcc = new Map<String, Id>();
   List<Case> caseListToupdate=new List<Case>();
   Set<String> acNameSet = new Set<String>();
   for(Case cc: Trigger.new){
      acNameSet .add(cc.copyAccount__c);
       caseIdSet.add(cc.id);
   }
   For(Account acc :[Select Id,Name from Account where Name  in:acNameSet]){
        mapAcc.put(acc.Name,acc.Id);
    }
    for(Case cc:[SELECT id,AccountId,copyAccount__c FROM Case WHERE id IN : caseIdSet]){
        if(!mapAcc.Isempty()){
         cc.AccountId = mapAcc.get(cc.copyAccount__c); 
         caseListToupdate.add(cc);
        }
   }
  try{
    update caseListToupdate;
  }catch(DMLException de ){
 }
}

Let me know if it help !
Jaynandan Prasad 8Jaynandan Prasad 8
Hi Manoj,

I am able to save it. But it does not populate the std. account field..
ManojjenaManojjena
Hi ,

Are you able to save the trigger ,If no please let me know th error .
If yes trigger wil fall under recursion as it is both after insert and update .
So please follow below links to solve recursion error .

https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)  

Check if your copyAccount__c  field have value befoe insert context or not . Simply check by adding a debug in trigger with before insert .

If value is there in before context then use below code ,
 
trigger CopyAccount on Case (before  insert, before update) {
   Map<String, Id> mapAcc = new Map<String, Id>();
   Set<String> acNameSet = new Set<String>();
   for(Case cc: Trigger.new){
      acNameSet .add(cc.copyAccount__c);
      
   }
   For(Account acc :[Select Id,Name from Account where Name  in:acNameSet]){
        mapAcc.put(acc.Name,acc.Id);
    }
    for(Case cc:Trigger.new){
        if(!mapAcc.Isempty()){
         cc.AccountId = mapAcc.get(cc.copyAccount__c); 
       }
   }
}
Still you are facing the same issue please click on the field and check whether it is readonly or not if read only change to editable .

Let me know if it helps .
 
This was selected as the best answer