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
Nasif HasnainNasif Hasnain 

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;
                    }
        }
Best Answer chosen by Nasif Hasnain
Ajay K DubediAjay K Dubedi
Hi Nasif,
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

Ajay K DubediAjay K Dubedi
Hi Nasif,
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
This was selected as the best answer
Nasif HasnainNasif Hasnain
Thanks its working fine.