What I was originally trying to do (create an account for each lead as it was created):

trigger LeadCreateAccount on Lead (Before Insert) {
    Public List<Account> lstAccts = new List<Account>();
    Public List<Account> lstNewAccts = new List<Account>();
    Public set<String> setLeadNames = new set<String>();
    Public set<String> setExistingAcctNames = new set<String>();
    public String name = '';

    // Create the set for al the names of the leads
    for(Lead l: trigger.new)
        setLeadNames.add(l.firstname + ' ' + l.lastname);

    // fetch the matching accounts if any exists
    lstAccts = [select id,name from account where name IN : setLeadNames];

    for(Account a: lstAccts)

    // Iterate through all leads to create acocunt if it does nto exists
    for(Lead l: trigger.new)
        name = l.firstname + ' ' + l.lastname;
        if (! setExistingAcctNames.contains(name))
            Account acct = new account();
            acct.name = name;

            // Add this to avoid any duplication in current set fo leads


    if (lstNewAccts.size() > 0)
        insert lstNewAccts;

This didn't work though, so I tried something simpler:

trigger LeadCreateAccount on Lead (Before Insert) {

            Account acct = new account();
            acct.name = 'asdf';

            insert acct;

If I use a system.debug() log then it shows up in my debug logs, but no account ever gets created with either piece of code. They're both definitely triggered when a lead is created though because the debug message works.

What am I doing wrong here?

EDIT: Here's my debug log:

12:35:17.059 (59411704)|EXECUTION_STARTED
12:35:17.059 (59491218)|CODE_UNIT_STARTED|[EXTERNAL]|01qe00000004a07|LeadCreateAccount on Lead trigger event BeforeInsert for [new]
12:35:17.147 (147465285)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Account
12:35:17.169 (169830325)|CODE_UNIT_FINISHED|Workflow:Account
12:35:17.382 (170640617)|CUMULATIVE_LIMIT_USAGE

12:35:17.170 (170695438)|CODE_UNIT_FINISHED|LeadCreateAccount on Lead trigger event BeforeInsert for [new]
12:35:17.192 (192314341)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Lead:new
12:35:17.192 (192327943)|VALIDATION_RULE|03d40000000Gy27|Lead_Type
12:35:17.192 (192620384)|VALIDATION_FORMULA|AND(
    ISPICKVAL( Primary_Contact_Type__c , "Arch"), 
    ISPICKVAL( Primary_Contact_Type__c, "Cont")
)|Primary_Contact_Type__c=null , Company=fdsfasdf
12:35:17.192 (192631693)|VALIDATION_PASS
12:35:17.192 (192634435)|VALIDATION_RULE|03d40000000H3Wa|Salesforce
12:35:17.192 (192907646)|VALIDATION_FORMULA|IF(RecordType.Id = "01240000000M72r", IF(NOT(ISPICKVAL( Status ,"lost to others"))  && NOT(ISPICKVAL( Status , "lost: project killed")) && NOT(ISPICKVAL( Status , "Disqualified")),True,False)

,False)|Status=Qualifying , RecordType.Id=01240000000M6kH
12:35:17.192 (192918240)|VALIDATION_PASS
12:35:17.192 (192936229)|CODE_UNIT_FINISHED|Validation:Lead:new
12:35:17.290 (290323193)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Lead
12:35:17.299 (299467432)|WF_RULE_EVAL_BEGIN|Workflow
12:35:17.299 (299501880)|WF_CRITERIA_BEGIN|[Lead: fdsaf 00Qe0000003fQPO]|Auto-Notify-New-Web-To-Lead|01Q40000000VIL5|ON_CREATE_ONLY
12:35:17.302 (302505007)|WF_RULE_FILTER|[Lead : Homeowner/Trade equals Web]
12:35:17.302 (302516120)|WF_RULE_EVAL_VALUE|
12:35:17.302 (302521098)|WF_CRITERIA_END|false
12:35:17.302 (302535891)|WF_SPOOL_ACTION_BEGIN|Workflow
12:35:17.302 (302543305)|WF_ACTION| None
12:35:17.302 (302546977)|WF_RULE_EVAL_END
12:35:17.302 (302596265)|WF_ACTIONS_END| None
12:35:17.302 (302603377)|CODE_UNIT_FINISHED|Workflow:Lead
12:35:17.379 (379352270)|CODE_UNIT_FINISHED|TRIGGERS
12:35:17.379 (379371588)|EXECUTION_FINISHED
  • note, in general, it is best to do related record DML in after triggers -- especially if you ever need to associate the related record SObject with the inserted SObject -- as ids are not known in the before trigger. You are also guaranteed that if there were any validation errors on the insert, your after trigger won't be executed.
    – cropredy
    Aug 12, 2014 at 20:38

1 Answer 1


Try modifying your code as follows:

trigger LeadCreateAccount on Lead (Before Insert) {

            Account acct = new account();
            acct.name = 'asdf';
try {
            insert acct; 
catch (system.Dmlexception e) {
       system.debug (e);      
system.debug (acct.id);    // it will print new account id to console   

This way you might find the error (say validation rule) that prevented insert. Alternatively, try Executing Anonymous (from Debug menu) code block without the first and last line.

  • see edits to post
    – Max Hudson
    Aug 12, 2014 at 19:36
  • Seems your code does not attempt to insert anything - probably there is flaw in logic. Try adding more system.debug() after each step and pretty quickly you will realize whats wrong.
    – dzh
    Aug 12, 2014 at 19:41
  • There's not really anything else I can log out besides the name....
    – Max Hudson
    Aug 12, 2014 at 20:00
  • I feel like we are not on the same boat :) have you tried running my code and getting response? Also try running your code from execute anonymous with hardcoded names, it will save you time from going into debug monitoring.
    – dzh
    Aug 12, 2014 at 20:03
  • 1
    Oh my god I was on the recently viewed default setting, not recently created
    – Max Hudson
    Aug 12, 2014 at 20:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .