+ Start a Discussion
AzarudeenAzarudeen 

error: Compile Error: Loop variable must be of type SOBJECT

 

trigger CreateAccountContact on Opportunity (after insert, after update,before Delete)
{
List<Account__c> accList=new List<Account__c>();
List<Contact__c> conList=new List<Contact__c>();
List<Lead__c> l=new List<Lead__c>();
//List<Opportunity__c> OppList=new List<Opportunity__c>();
for(Opportunity__c Opp:trigger.new)
{
if(Opp.Stage__c=='Closed Won') 
 {
Opportunity__c objopp =[select Lead__c from Opportunity__c where id =:Opp.id];
Lead__c objlead = [select First_Name__c,Company__c,Last_Name__c,Email__c from Lead__c where id=:objopp.Lead__c];
Account__c ac=new Account__c(Company__c=objlead.Company__c);
accList.add(ac);
Contact__c con=new Contact__c(Last_Name__c=objlead.Last_Name__c,First_Name__c =objlead.First_Name__c,Email__c=objlead.Email__c);   // map the other fields of lead to contact as you required
conList.add(con);    
//l=[select id,name,Company,LastName,Email from Lead where id=:objopp.Lead__c];
//Opportunity objopp1=new Opportunity(Description=objlead.Name);
//OppList.add(objopp1);
  }  
 } 
 insert accList;
 insert conList;
 //update OppList;
 
  Delete l;
 
 
}

trigger CreateAccountContact on Opportunity (after insert, after update,before Delete){List<Account__c> accList=new List<Account__c>();List<Contact__c> conList=new List<Contact__c>();List<Lead__c> l=new List<Lead__c>();//List<Opportunity__c> OppList=new List<Opportunity__c>();for(Opportunity__c Opp:trigger.new){if(Opp.Stage__c=='Closed Won')  {Opportunity__c objopp =[select Lead__c from Opportunity__c where id =:Opp.id];Lead__c objlead = [select First_Name__c,Company__c,Last_Name__c,Email__c from Lead__c where id=:objopp.Lead__c];Account__c ac=new Account__c(Company__c=objlead.Company__c);accList.add(ac);Contact__c con=new Contact__c(Last_Name__c=objlead.Last_Name__c,First_Name__c =objlead.First_Name__c,Email__c=objlead.Email__c);   // map the other fields of lead to contact as you requiredconList.add(con);    //l=[select id,name,Company,LastName,Email from Lead where id=:objopp.Lead__c];//Opportunity objopp1=new Opportunity(Description=objlead.Name);//OppList.add(objopp1);  }   }  insert accList; insert conList; //update OppList;   Delete l; 
 }

 

Kevin SwiggumKevin Swiggum

You might be running into problems because you're referencing Trigger.new within a Before Delete trigger....which isn't available (I'm not sure you'd get a compile error with that though)

 

The availability of the trigger context variables is provided at

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm

 

One other thing. I noticed you have a query inside your for loop. That works for one record at a time, but any batch inserts/update of data are going to break once you hit the 25 query limit. 

 

See the link below to learn more about "bulkifying" triggers

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bulk_idioms.htm

 

--Kevin

rahulsharmarahulsharma

Hello Azarudeen,

 

You can avoid queries in the for loop by using below code:


------------------------------------------------------------------------------------------------------------------------------------------------

 

trigger CreateAccountContact on Opportunity (after insert, after update,before Delete)
{
    List<Account__c> accList=new List<Account__c>();
    List<Contact__c> conList=new List<Contact__c>();
    List<Lead__c> l=new List<Lead__c>();
    //List<Opportunity__c> OppList=new List<Opportunity__c>();
    for(Opportunity__c objOpportunity: [select Lead__c from Opportunity__c where id In :trigger.new and Stage__c='Closed Won'])
    {
        for(Lead__c objLead : [select First_Name__c,Company__c,Last_Name__c,Email__c from Lead__c where id=:objOpportunity.Lead__c])
        {
            Account__c ac=new Account__c(Company__c=objlead.Company__c);
            accList.add(ac);
            Contact__c con=new Contact__c(Last_Name__c=objlead.Last_Name__c,First_Name__c =objlead.First_Name__c,Email__c=objlead.Email__c);
            conList.add(con);
        }
    }
    insert accList;
    insert conList;
    Delete l;
}

------------------------------------------------------------------------------------------------------------------------------------------------

 

Hope this help.

Rahul Sharma