You need to sign in to do that
Don't have an account?

can someone please solve the error: i am getting error message as id doesn't exist and billingstreet doesn't exist
trigger trg1 on Account (after update){
list<Contact> cont = new list<contact> ();
List<Account> acc = new list<Account>();
for(Account a : trigger.new){
acc.add(a);
}
if(trigger.oldmap.get(acc.id).billingstreet != acc.billingstreet){
list<contact> con = [select id,name,accountid, otherstreet from contact
where accountid IN : acc.id];
for(contact c : con){
c.otherstreet = acc.billingstreet;
cont.add(c);
}
update cont;
}
}
list<Contact> cont = new list<contact> ();
List<Account> acc = new list<Account>();
for(Account a : trigger.new){
acc.add(a);
}
if(trigger.oldmap.get(acc.id).billingstreet != acc.billingstreet){
list<contact> con = [select id,name,accountid, otherstreet from contact
where accountid IN : acc.id];
for(contact c : con){
c.otherstreet = acc.billingstreet;
cont.add(c);
}
update cont;
}
}
In your code acc is a list of account, for list you have to include for the loop.
Try this code:
trigger trg1 on Account (after update){
list<Contact> contList = new list<contact> ();
List<Account> accList = new list<Account>();
Set<Id> accIdSet = new Set<Id>();
for(Account a : trigger.new){
accList.add(a);
}
for(Account acc : accList){
if(trigger.oldmap.get(acc.Id).billingstreet != acc.billingstreet){
accIdSet.add(acc.Id);
}
}
contList = [select Id, Name, AccountId, otherstreet from contact
where AccountId IN : accIdSet LIMIT 10000];
for(Account acc : accList) {
for(contact con : contList){
if(con.AccountId == acc.Id) {
con.otherstreet = acc.billingstreet;
}
}
}
update contList;
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
In your code acc is a list of account, for list you have to include for the loop.
Try this code:
trigger trg1 on Account (after update){
list<Contact> contList = new list<contact> ();
List<Account> accList = new list<Account>();
Set<Id> accIdSet = new Set<Id>();
for(Account a : trigger.new){
accList.add(a);
}
for(Account acc : accList){
if(trigger.oldmap.get(acc.Id).billingstreet != acc.billingstreet){
accIdSet.add(acc.Id);
}
}
contList = [select Id, Name, AccountId, otherstreet from contact
where AccountId IN : accIdSet LIMIT 10000];
for(Account acc : accList) {
for(contact con : contList){
if(con.AccountId == acc.Id) {
con.otherstreet = acc.billingstreet;
}
}
}
update contList;
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi