You need to sign in to do that
Don't have an account?
Update Lookp field using Apex Trigger
Here Case Is Parent and Opportunity is Child,,
1)When from Existing Case [Without Parent Account],i create an opportunity and then select Parent account the its reflect on Opportunity Parent as well
2)But Update is Not Working,,when i change case parent Account its not reflecting in Opportunity Acccount field.
Below is the Trigger:
public class Data {
public static void InsertMethod(list<Case> newCas){
set<Id> lstAccId = new set<Id>();
set<Id> lstOppId = new set<Id>();//Opportunity Parent
for(Case cas:newCas){
if(cas.AccountId!=null){
lstAccId.add(cas.AccountId);
}
if(cas.Opportunity__c!=null){
lstOppId.add(cas.Opportunity__c);
}
}
list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
list<Opportunity> lstOpp=[Select id,AccountId from Opportunity where id IN :lstOppId];
for(Account s :lstAcc){
for(Opportunity op:lstOpp){
op.AccountId=s.Id;
}
}
update lstOpp;
}
public static void UpdateMethod(list<Case> newCas,map<Id,Case> oldmap){
set<Id> lstAccId = new set<Id>();
set<Id> lstOppId = new set<Id>();//Opportunity Parent
for(Case cas:newCas ){
if(cas.AccountId!=null && cas.AccountId!=oldmap.get(cas.id).AccountId){
lstAccId.add(cas.AccountId);
}
if(cas.Opportunity__c!=null && cas.Opportunity__c!=oldmap.get(cas.id).Opportunity__c){
lstOppId.add(cas.Opportunity__c);
}
}
list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
list<Opportunity> lstOpp=[Select id,AccountId from Opportunity where id IN :lstOppId];
for(Account s :lstAcc){
for(Opportunity op:lstOpp){
op.AccountId=s.Id;
}
}
update lstOpp;
}
}
Trigger:
trigger TriggerOnCase on Case (after insert,after update) {
If(Trigger.IsAfter){
If(Trigger.IsInsert){
Data.InsertMethod(Trigger.new);
}
If(Trigger.IsUpdate){
Data.UpdateMethod(Trigger.new,Trigger.oldMap);
}
}
}
1)When from Existing Case [Without Parent Account],i create an opportunity and then select Parent account the its reflect on Opportunity Parent as well
2)But Update is Not Working,,when i change case parent Account its not reflecting in Opportunity Acccount field.
Below is the Trigger:
public class Data {
public static void InsertMethod(list<Case> newCas){
set<Id> lstAccId = new set<Id>();
set<Id> lstOppId = new set<Id>();//Opportunity Parent
for(Case cas:newCas){
if(cas.AccountId!=null){
lstAccId.add(cas.AccountId);
}
if(cas.Opportunity__c!=null){
lstOppId.add(cas.Opportunity__c);
}
}
list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
list<Opportunity> lstOpp=[Select id,AccountId from Opportunity where id IN :lstOppId];
for(Account s :lstAcc){
for(Opportunity op:lstOpp){
op.AccountId=s.Id;
}
}
update lstOpp;
}
public static void UpdateMethod(list<Case> newCas,map<Id,Case> oldmap){
set<Id> lstAccId = new set<Id>();
set<Id> lstOppId = new set<Id>();//Opportunity Parent
for(Case cas:newCas ){
if(cas.AccountId!=null && cas.AccountId!=oldmap.get(cas.id).AccountId){
lstAccId.add(cas.AccountId);
}
if(cas.Opportunity__c!=null && cas.Opportunity__c!=oldmap.get(cas.id).Opportunity__c){
lstOppId.add(cas.Opportunity__c);
}
}
list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
list<Opportunity> lstOpp=[Select id,AccountId from Opportunity where id IN :lstOppId];
for(Account s :lstAcc){
for(Opportunity op:lstOpp){
op.AccountId=s.Id;
}
}
update lstOpp;
}
}
Trigger:
trigger TriggerOnCase on Case (after insert,after update) {
If(Trigger.IsAfter){
If(Trigger.IsInsert){
Data.InsertMethod(Trigger.new);
}
If(Trigger.IsUpdate){
Data.UpdateMethod(Trigger.new,Trigger.oldMap);
}
}
}
It is not updating the account on opportunity when you are updating the acccount on case because of the below line.
As you are not updating the opportunity on case so this results in null and so it is not updating the opportunity.
If you want to change the opportunity irrespective of the previous value then you can use the below line of code.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
It is not updating the account on opportunity when you are updating the acccount on case because of the below line.
As you are not updating the opportunity on case so this results in null and so it is not updating the opportunity.
If you want to change the opportunity irrespective of the previous value then you can use the below line of code.
If this solution helps, Please mark it as best answer.
Thanks,
Thanks for Quick Reply.
Its Working Perfectly.