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
Patrick G. BrownPatrick G. Brown 

Help with error "Variable does not exist: Id" when trying to default an Account on Contact

I'm writing a trigger to default the Account of a Contact each time a Contact is created.  The trigger is relatively simple.  I'm receiving an error that reads: Variable does not exist: Id.  Can anyone point me in the direction of what I'm doing wrong?
 
trigger setDefaultAcc on Contact (before insert){
    List<Contact> lstCon = new List<Contact>();
    // Find out and prepare list of Contacts
    for(Contact var : Trigger.new){
        if(var.AccountId == null){
            lstCon.add(var);
        }
    }
    // Fetch the Default account
    if(lstCon.size()>0){
        List<Account> acc = new List<Account>();
        acc = [SELECT Id, Name FROM Account WHERE Name = 'Strategic Market Advisors' LIMIT 1];
        if(acc != null){
            for(Contact var : lstCon){
                // Assign the id of the default account to the selected contacts
                var.AccountId = acc.Id;
            }
        }
    }
}

 
Best Answer chosen by Patrick G. Brown
HARSHIL U PARIKHHARSHIL U PARIKH
If you use the Query inside the FOR loop then this trigger would fail after the first 100 records inser. But however, you can try the trigger below which is pretty straight forward!
 
Trigger setDefaultAcc on Contact (before insert)
{
   List<Account> defaultAccountInfo = [Select Id, Name FROM Account WHERE Name =:'Strategic Market Advisors'];
   
   If(!defaultAccountInfo.IsEmpty()){
       For(Contact con : Trigger.New){
           If(con.AccountId == null){
               con.AccountId = defaultAccountInfo[0].Id;
           }
       }
   }
   
}
When you try to access the record which is in list, you need to identify the Index for it though..

Hope this helps & Mark it Solved if it solves the query!
 

All Answers

om gupta(sfdc)om gupta(sfdc)
acc is a list so you have to either get it by acc[0].id and check !acc.isEmpty() instead of acc != null
HARSHIL U PARIKHHARSHIL U PARIKH
If you use the Query inside the FOR loop then this trigger would fail after the first 100 records inser. But however, you can try the trigger below which is pretty straight forward!
 
Trigger setDefaultAcc on Contact (before insert)
{
   List<Account> defaultAccountInfo = [Select Id, Name FROM Account WHERE Name =:'Strategic Market Advisors'];
   
   If(!defaultAccountInfo.IsEmpty()){
       For(Contact con : Trigger.New){
           If(con.AccountId == null){
               con.AccountId = defaultAccountInfo[0].Id;
           }
       }
   }
   
}
When you try to access the record which is in list, you need to identify the Index for it though..

Hope this helps & Mark it Solved if it solves the query!
 
This was selected as the best answer
pradeep kumar yadavpradeep kumar yadav
try this

 if(acc != null){
           for(Contact var : lstCon){
               var.AccountId = acc[0].Id;
            }
   }
Patrick G. BrownPatrick G. Brown
Thanks, @Govind Guru.  It's working now.