+ Start a Discussion
Navneeth RajNavneeth Raj 

System.DmlException TRIGGER ERROR

SCENARIO: If Lead Status is "Open - Not Contacted" add to Account else if it is "Working - Contacted" add to Opportunity
trigger AutoQualifyLead2 on Lead (after insert) {
    List<Account> accs=new List<Account> ();
    List<Opportunity> opps=new List<Opportunity> ();
    for(Lead l:Trigger.New)
    {
        if(l.Status=='Open - Not Contacted'){
            Account a=new Account();
            a.Fax=l.Fax;
            a.Phone=l.Phone;
            a.Industry=l.Industry;
            accs.add(a);
        }
        else{
            if(l.Status=='Working - Contacted'){
                Opportunity op=new Opportunity();
                op.Name=l.Name;
                op.Description=l.Description;
                opps.add(op);
            }
        }
    }
    if(accs.size()>0){    insert accs;    }
    if(opps.size()>0){    insert opps;    }
}
ERROR: As seen in attached Image
User-added image
Peter YohannaPeter Yohanna
Hello,

The error message seems to indicate that you are missing required fields for the new opportunity. You need to add something like this:
 
if(l.Status=='Working - Contacted'){
   Opportunity op=new Opportunity();
   op.Name=l.Name;
   op.Description=l.Description;
   
   op.closeDate=Date.Today();
   op.Stage='Prospecting';
   
   opps.add(op);
}

Hope this helps.

Cheers,
Peter
 
Mubarak HussainMubarak Hussain
Hi Naveen, 
 You have missed the required field in account and opportunity objects ,that is the root cause for this error.

Thanks
Muba