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
Andy BoettcherAndy Boettcher 

Lead Insert returning different Id?

I have an APEX class that is inserting Lead information, *randomly* (yes, I know there is no random) I'm getting back an Id from an existing Lead.  There is only one "before insert" trigger and the only thing that does is query some other objects to populate a custom field with some instructions on what to do next with that lead.

 

Environment - this is running in Sites.

 

Pseudo-code and flow:

 

1.  Instantiate variables via Controller:

public Lead ldNew {get;set;}

ldNew = new Lead();

 

2.  Display and collect on VF Page

<apex:inputField value="{!ldNew.Email}" />

 

3.  Insert Lead object

insert ldNew;

 

4.  Requery Lead for instruction info 

Lead ldRequery = [SELECT ... FROM Lead WHERE Id = ldNew.Id];

 

5.  Send Emails to people, etc...

etc...

 

Right there between Step 3 and 4, sometimes I"m getting back an Id from an EXISTING LEAD instead of the expected new lead.

 

I'm stumped - ideas?  Anyone seen this behavior before?  I didn't think it was possible to return the Id from another Lead within a before insert trigger??!

 

-Andy

Daniel BallingerDaniel Ballinger

Not sure what is happening, but I'd expand the IdNew get; set; out and add some logging in to detect when a new value is being set.

 

Other than that I'd be on the lookout for extra static keywords somewhere in the code.

 

Cheers,

Daniel

craigmhcraigmh

This pseudo-code works fine, displays the same IDs:

 

<apex:page controller="LeadTest">
    <apex:form>
        <apex:inputField value="{!ldNew.Email}" />
        <br /><br />
        <apex:commandButton value="Click" action="{!btnClicked}" />
        <br /><br />
        {!newId}
    </apex:form>
</apex:page>

 

public with sharing class LeadTest {
    public Lead ldNew { get; set; }
    public string newId { get; set; }
    
    public LeadTest() {
        ldNew = new Lead();
        ldNew.LastName = 'Test';
        ldNew.Company = 'Test Company';
        newId = '';
    }
    
    public void btnClicked() {
        insert ldNew;
        newId = ldNew.Id;
        Lead ldRequery = [Select Id From Lead Where Id = :ldNew.Id Limit 1];
        newId += ' - ' + ldRequery.Id;
        
        delete ldRequery;
    }
}

 

So I'm guessing that it's something that you omitted.

Andy BoettcherAndy Boettcher

99% of the time it returns the right Id - the code is sound.

 

It's weird, just every now and then it returns a different Id.

 

Nevermind - I'm recoding it differently.

 

-Andy

craigmhcraigmh

Sometimes, the best answer is to redo it and hope that you don't make the same, hidden mistake again. :)

Devendra@SFDCDevendra@SFDC

Hello techman,

 

Did you solve the issue? What causing this issue?

 

Thanks,

Devendra

 

Andy BoettcherAndy Boettcher

There has been no solution yet.  Only happens on this one org (I put the code in another org and no problems recur).

 

Was a long shot if anyone had experienced it, we'll see what time tells.

 

-Andy

craigmhcraigmh

do you have any triggers on Leads that may affect it?

Andy BoettcherAndy Boettcher

That's the kicker - we do, but as part of identifying what the heck was going on, I disabled all triggers and workflows.

 

-Andy