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
Joe2008Joe2008 

Apex Trigger Complie Error

Hello,
 
I am a newbie and tried creating a trigger for creating a account on a field value change in Lead object.
when i compile i get the error:
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST:SOBJECT:Lead at line 5 column 5
 
Any direction ?
 
code:
trigger create_Accnt on Lead (after update) {
//Check if the lead field Intrest_to_Buy is changed to 'Willing to Buy'
if (Trigger.New.Intrest_to_Buy = 'Willing to Buy'&& Trigger.old.Intrest_to_Buy != 'Willing to Buy')  {
   
        Account CreateAccnt = new Account ();
        CreateAccnt.Name = 'testing_trig';
        insert CreateAccnt;
    }   
}
 
Joe


Message Edited by Joe2008 on 07-31-2008 03:34 AM

Message Edited by Joe2008 on 07-31-2008 05:51 AM
Best Answer chosen by Admin (Salesforce Developers) 
JonPJonP
Trigger.new is an array.  You need to loop through its contents to access each Account one at a time, e.g.:

Code:
trigger create_Accnt on Lead(after update) {

  // create a container to bulk insert accounts
  List<Account> accountsToInsert = new List<Account>();  

  // loop through trigger records
  for (int i=0; i<Trigger.new.size(); i++)
  {
    if (Trigger.new[i].Interest_to_Buy__c == 'Willing to Buy'
        && Trigger.old[i].Interest_to_Buy__c != 'Willing to Buy')
    {
      Account CreateAccnt = new Account();
      CreateAccnt.Name = 'testing_trig';
      accountsToInsert.add( CreateAccnt );
    }
  }

  // bulk insert accounts
  if (!accountsToInsert.isEmpty()) {
    insert accountsToInsert;
  } 
}

Since I added a loop, I also "bulkified" the trigger: Inside the loop, it creates Accounts and adds them to a List.  Once the loop is finished, it uses a single "insert" statement to add all the Accounts to the database in a single operation.

As a rule of thumb, you should almost never use a DML statement (insert/update/delete) inside a loop, in Apex or just about any other database-accessing code.

All Answers

JonPJonP
Trigger.new is an array.  You need to loop through its contents to access each Account one at a time, e.g.:

Code:
trigger create_Accnt on Lead(after update) {

  // create a container to bulk insert accounts
  List<Account> accountsToInsert = new List<Account>();  

  // loop through trigger records
  for (int i=0; i<Trigger.new.size(); i++)
  {
    if (Trigger.new[i].Interest_to_Buy__c == 'Willing to Buy'
        && Trigger.old[i].Interest_to_Buy__c != 'Willing to Buy')
    {
      Account CreateAccnt = new Account();
      CreateAccnt.Name = 'testing_trig';
      accountsToInsert.add( CreateAccnt );
    }
  }

  // bulk insert accounts
  if (!accountsToInsert.isEmpty()) {
    insert accountsToInsert;
  } 
}

Since I added a loop, I also "bulkified" the trigger: Inside the loop, it creates Accounts and adds them to a List.  Once the loop is finished, it uses a single "insert" statement to add all the Accounts to the database in a single operation.

As a rule of thumb, you should almost never use a DML statement (insert/update/delete) inside a loop, in Apex or just about any other database-accessing code.

This was selected as the best answer
Joe2008Joe2008

Thx

Joe