+ Start a Discussion
KIBLKIBL 

Lead Trigger

I have a trigger in production that when a Lead is converted, it will update the Account with the lead information. 

My problem is that I only want this to happen on New Accounts and not Exsting Accounts.  Right now it is overriding

the information on an existing account and we don't want it to.  How can I change this to only trigger when the account is

new?

 

Here is my trigger:

 

trigger UpdateAccountFromLead on Lead (after update) {
account Account = new Account();

  for ( Lead theLead : Trigger.new )
  {
    // Check to see if the lead was converted into an Account
    if ( theLead.ConvertedAccountId != null )
    {
      // Get the Account to update
      Account theAccount = [select id, District_Type__c, District__c, District_Name__c,Region__c, Sales_Rep__c , Field_Sales_Rep_Number__c, Market_Code__c, Site, State__c  from Account where id = :theLead.ConvertedAccountId];
      
      // Update the fields
      theAccount.District_Type__c = theLead.District_Type__c;
      theAccount.District_Name__c = theLead.District_Name__c;
      theAccount.District__c = theLead.District__c;
      theAccount.Region__c = theLead.Region__c;
      theAccount.Sales_Rep__c = theLead.Field_Sales_Rep__c;
      theAccount.Market_Code__c = theLead.Market_Code__c;
      theAccount.Field_Sales_Rep_Number__c  = theLead.Field_Sales_Rep_Number__c ;
      theAccount.State__c = theLead.State;
      theAccount.Site = theLead.City;
      
     // Update the Account
       update theAccount;
    }
  }

}

 

kyle.tkyle.t

Can you get the created date of the account and perform some kind of comparison... not sure if you want to check that the account was created prior to TODAY...just a thought, not sure if it will pan out, but give that a shot.

 

 

dmchengdmcheng

How do you define a "New Account"?  Is that an account created from a converted lead?  If so, the Lead object has a field called ConvertedAccountId that you can refer to.