You need to sign in to do that
Don't have an account?
Update Lookup Field From Child To Parent Using Trigger
Senario:
Opportunity is child and case is Parent Object.
When New Opportunity Is Created along with associated Account,,,and associated case object.
that account Lookup name should be autoPopulate in Case Object of Account Lookup Field.
I create a Lookup Relation where Opportunity is Child and Case is Parent.
ERROR:
TriggerOnOpportunity: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 5005g00000DAPbrAAH Class.Data.insertOpportunity: line 16, column 1 Trigger.TriggerOnOpportunity: line 3, column 1
FIELDS IN HANDLER CLASS:
MY_ACC__c-->On case Object(Text(20))
Case__c-->Looupfield on Opportunity
Handler class:
public class Data{
public static void insertOpportunity(List<opportunity> oppList)
{
List<opportunity> opp = [select id,Case__r.id,Case__r.MY_ACC__c,Account.name, Accountid from opportunity where id=:oppList ];
system.debug(opp[0].Case__r.id);
system.debug('oppList'+oppList);
if(opp[0].Case__r.id != null){
List<case> casselist = [select id, MY_ACC__c from case where id=:opp[0].Case__r.id LIMIT 1];
case c = new case();
c.Id = casselist[0].id;
c.MY_ACC__c = opp[0].Account.name;
casselist.add(c);
// casselist[0].MY_ACC__c = opp[0].Account.name;
Upsert casselist;
}
}
}
Trigger Class:
trigger TriggerOnOpportunity on Opportunity (after insert, after update) {
if (Trigger.isAfter && Trigger.isInsert || Trigger.isUpdate){
Data.insertOpportunity(Trigger.new);
}
}
Opportunity is child and case is Parent Object.
When New Opportunity Is Created along with associated Account,,,and associated case object.
that account Lookup name should be autoPopulate in Case Object of Account Lookup Field.
I create a Lookup Relation where Opportunity is Child and Case is Parent.
ERROR:
TriggerOnOpportunity: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 5005g00000DAPbrAAH Class.Data.insertOpportunity: line 16, column 1 Trigger.TriggerOnOpportunity: line 3, column 1
FIELDS IN HANDLER CLASS:
MY_ACC__c-->On case Object(Text(20))
Case__c-->Looupfield on Opportunity
Handler class:
public class Data{
public static void insertOpportunity(List<opportunity> oppList)
{
List<opportunity> opp = [select id,Case__r.id,Case__r.MY_ACC__c,Account.name, Accountid from opportunity where id=:oppList ];
system.debug(opp[0].Case__r.id);
system.debug('oppList'+oppList);
if(opp[0].Case__r.id != null){
List<case> casselist = [select id, MY_ACC__c from case where id=:opp[0].Case__r.id LIMIT 1];
case c = new case();
c.Id = casselist[0].id;
c.MY_ACC__c = opp[0].Account.name;
casselist.add(c);
// casselist[0].MY_ACC__c = opp[0].Account.name;
Upsert casselist;
}
}
}
Trigger Class:
trigger TriggerOnOpportunity on Opportunity (after insert, after update) {
if (Trigger.isAfter && Trigger.isInsert || Trigger.isUpdate){
Data.insertOpportunity(Trigger.new);
}
}
Try Below TriggerHandlerClass Please Mark It As Best Answer If It Helps
Thank You!
All Answers
Try below Trigger Please Mark It As Best Answer If It Helps
Thank You!
Try Below TriggerHandlerClass Please Mark It As Best Answer If It Helps
Thank You!
So i Updated
Handler:
public class Data{
public static void InsertMethod(list<Opportunity> newOpp){
set<Id> lstAccId = new set<Id>();
set<Id> lstCaseId = new set<Id>();
for(Opportunity opp : newOpp){
if(opp.AccountId != null){
lstAccId.add(opp.AccountId);
}
if(opp.Case__c != null){
lstCaseId.add(opp.Case__c);
}
}
list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
list<Case> lstCase = [Select id,AccountId from Case where id IN :lstCaseId];
for(Account s :lstAcc){
for(Case c : lstCase){
c.AccountId = s.Id;
}
}
update lstCase;
}
public static void UpdateMethod(list<Opportunity> newOpp,map<Id,Opportunity> oldmap){
set<Id> lstAccId = new set<Id>();
set<Id> lstCaseId = new set<Id>();
for(Opportunity opp : newOpp){
if(opp.AccountId != null && opp.AccountId != oldmap.get(opp.Id).AccountId){
lstAccId.add(opp.AccountId);
}
if(opp.Case__c != null && opp.Case__c != oldmap.get(opp.Id).Case__c){
lstCaseId.add(opp.Case__c);
}
}
list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
list<Case> lstCase = [Select id,AccountId from Case where id IN :lstCaseId];
for(Account s :lstAcc){
for(Case c : lstCase){
c.AccountId = s.Id;
}
}
update lstCase;
}
}
Trigger:
trigger OppTrigger on Opportunity (after insert,after update) {
If(Trigger.IsAfter){
If(Trigger.IsInsert){
Data.InsertMethod(Trigger.new);
}
If(Trigger.IsUpdate){
Data.UpdateMethod(Trigger.new,Trigger.oldMap);
}
}
}