• khops
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies

I have encountered a problem where a user will click on a button designed to direct him to a specific page, and 99% of the time, this works. However, occasionally the user will simply see the current page again upon clicking the button, and when he clicks on it a second time, it sends him to the desired page. The problem is that behind the scenes for this click, there is an apex function that is designed to insert a new lead when this button is clicked. This fires 100% of the time, so when the user clicks on the button and it doesn't redirect him, the lead insert still fires, the page just doesn't change. So when the user clicks on the page the second time, he gets sent to the desired page, but another lead is inserted (i.e. A duplicate lead), which isn't good.  I can't think of a reason why the page redirect should work most of the time but fail occasionally.  Below is the very basic nuts and bolts of the code that carries out this mission:

 

VF code:

<apex:commandButton value="Busy "action="{!Busy}"/>

 

Apex Code:

public PageReference Busy()

{

      PageReference pageRef01 =new PageReference('/apex/outbound_home');

      insert L001;   //Lead definitions and values assigned elsewhere.

      return pageRef01;

}

 

I'm wondering if it might possibly be a session issue in the user's browsers or something that causes the button to time out, forcing the page to refresh instead of to redirect to outbound_home, but my knowledge on that sort of thing is extremely limited. I know the user uses the same browser every time (IE8).  Has anyone else run into this issue or have any ideas on possible causes/solutions?

 

  • February 29, 2012
  • Like
  • 0

            I'm having trouble running tests for a trigger when comparing trigger.new to trigger.old for insert/update batches.

For example here is my trigger code

 

if(trigger.isupdate)

{

    list<lead> emailList = new list<lead>();

    for(integer i=0; i < trigger.new.size();i++)

    {

         if(trigger.new[i].emailAddress__c != NULL && trigger.new[i].SendEmail__c==true && trigger.old[i].SendEmail__c == false)

         {

                 emailList.add(trigger.new[i])

         }

    }

}

 

 

 

Now, In my test class if I do something like this:

 

lead myLead = new lead (emailAddress__c ='Alex' )

insert myLead;

myLead.sendEmail__c = true;

update myLead;

 

The update works perfectly fine and as expected.

 

However, if I do something like

 

list<Lead> myLeadsToInsert = new list<Lead>();

list<Lead> myLeadsToUpdate = new list <Lead>();

 

Lead myLead = new Lead( emailAddress__c='Alex');

myLeadsToInsert.add(myLead);

myLead.SendEmail__c=true;

myLeadsToUpdate.add(myLead);

 

insert myLeadsToInsert;

update myLeadsToUpdate;

 

it fails miserably = (

 

I need it to work the second way because I create lots of different leads for scenario testing

and I can't do dml updates/inserts for each one individually or the SF governor goes berserk as it consumes vast

amounts of resources.

 

What I've shown here is just a single example of code in my trigger that utilzes trigger.new vs. trigger.old comparisons.

The tests seem to fail at any point in time that I have these comparisons in the trigger.

I'm just curious if there's something implicitly different about the way trigger.old handles individual records vs.

batches of records that's causing this sort of behavior. Any help would be fantastic.

  • August 18, 2011
  • Like
  • 0

I have a field called 'LeadID'  in an object called 'Outbound__c' that for all intents and purposes is a Lead ID but in text form. Ideally, this would be a great lookup field. Unfortunately, it isn't possible to change a text field to a lookup field. I tried migrating the data into a new lookup field, but because some of the ids being passed reference converted leads, an error gets thrown. I'm not actually trying to update the converted leads, I just want their IDs to get placed into the lookup field so that I can tie the objects together in SOQL queries. Has anyone else run into this problem/ found a workaround?

  • June 06, 2011
  • Like
  • 0

            I'm having trouble running tests for a trigger when comparing trigger.new to trigger.old for insert/update batches.

For example here is my trigger code

 

if(trigger.isupdate)

{

    list<lead> emailList = new list<lead>();

    for(integer i=0; i < trigger.new.size();i++)

    {

         if(trigger.new[i].emailAddress__c != NULL && trigger.new[i].SendEmail__c==true && trigger.old[i].SendEmail__c == false)

         {

                 emailList.add(trigger.new[i])

         }

    }

}

 

 

 

Now, In my test class if I do something like this:

 

lead myLead = new lead (emailAddress__c ='Alex' )

insert myLead;

myLead.sendEmail__c = true;

update myLead;

 

The update works perfectly fine and as expected.

 

However, if I do something like

 

list<Lead> myLeadsToInsert = new list<Lead>();

list<Lead> myLeadsToUpdate = new list <Lead>();

 

Lead myLead = new Lead( emailAddress__c='Alex');

myLeadsToInsert.add(myLead);

myLead.SendEmail__c=true;

myLeadsToUpdate.add(myLead);

 

insert myLeadsToInsert;

update myLeadsToUpdate;

 

it fails miserably = (

 

I need it to work the second way because I create lots of different leads for scenario testing

and I can't do dml updates/inserts for each one individually or the SF governor goes berserk as it consumes vast

amounts of resources.

 

What I've shown here is just a single example of code in my trigger that utilzes trigger.new vs. trigger.old comparisons.

The tests seem to fail at any point in time that I have these comparisons in the trigger.

I'm just curious if there's something implicitly different about the way trigger.old handles individual records vs.

batches of records that's causing this sort of behavior. Any help would be fantastic.

  • August 18, 2011
  • Like
  • 0

Hi All

 

I wrote a simple trigger on case. I am able to save the trigger but it is not working when i am updating the records. I have a checkbox on Case whenever this checkbox becomes true the checkbox in CAF(Custom object) should become true. For this i wrote this trigger. I think i had made mistake in the where clause of the SOQL query. I am mentioning in Red. Please let me know. the relation between CAF__c and Case is lookup.

 

trigger CAFoppUpadate1 on Case (after update)
{
List <CAF__c> CAFList;
List <Id> CaseIdList = new List <Id>();
for(Case c:Trigger.new)
{
    if (c.Closed_won_opportunity__c == True)
     caseIdList.add(c.Id);
 }  
CAFList = [select Id, test_caf__c from CAF__c where ID in :Caseidlist];

    for(CAF__c ca : CAFList)
    {
        if (ca.test_caf__c != true)
            ca.test_caf__c = true;    
    }
    
    update CAFList;
}