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)
{
setExistingAcctNames.add(a.name);
}
// 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;
// MAP ALL THE FIELDS U WANT TO MAP HERE
lstNewAccts.add(acct);
// Add this to avoid any duplication in current set fo leads
setExistingAcctNames.add(name);
}
}
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:
31.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
12:35:17.059 (59411704)|EXECUTION_STARTED
12:35:17.059 (59448789)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
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.382|CUMULATIVE_LIMIT_USAGE_END
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(
OR(
ISPICKVAL( Primary_Contact_Type__c , "Arch"),
ISPICKVAL( Primary_Contact_Type__c, "Cont")
),
ISBLANK(Company)
)|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
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 '14 at 20:38