You need to sign in to do that
Don't have an account?
Jaynandan 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..
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 ,
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
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
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) .
Getting below error from the handler class- which is
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
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 .
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.
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
getting the below error-
Variable does not exist: cs.AccountId at line 11 column 6
Try with below code .
Let me know if it helps .
Error:
Trigger.CopyAccount: line 13, column 1 10:43:45.240 (240986099)|FATAL_ERROR|System.FinalException: Record is read-only
Try below code plz .
Let me know if it help !
I am able to save it. But it does not populate the std. account field..
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 ,
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 .