+ Start a Discussion
babrannobabranno 

Auto Number

I am looking for some help on some code.  I am trying to auto number a numeric field and also check for duplicates and prevent them.  I took some code that was originally written for our implentation of SF and tweated it for the new object but I am new to apex and I am not sure what is going wrong.  I have included the code below along with the trigger.  I have tried playing with it but I cannot get it to populate the field with the auto number.  Any help would be appreciated.  Thank you.

 

Class

 

public without sharing class ProjectActions 
{
  static public void AssignRequestNumber(List<PPM_Project__c> triggerProjects)
  {
      integer maxRequestNumber = 0;
      AggregateResult[] results = [SELECT MAX(Request_Number__c) maxRequestNumber FROM PPM_Project__c];
      if (results!=null&&results.size() > 0) 
      {
        maxRequestNumber = integer.valueOf(results[0].get('maxRequestNumber'));
        }
        if(maxRequestNumber!=null && maxRequestNumber > 0)
        {
          for( PPM_Project__c project : triggerProjects)
          {
            if(PPM_Project__c.Request_Number__c == null )
            {
              maxRequestNumber = maxRequestNumber + 1;
              }
          }
        }
    }
  
  
  static public void DedupRequestNumbers(List<PPM_Project__c> triggerProjects)
  {
      Set<Decimal> requestNumbers = new Set<Decimal>();
      for(PPM_Project__c project : triggerProjects)
      {
        requestNumbers.add(project.Request_Number__c);
      }

      List<PPM_Project__c> maybeDupProjects = [Select Id, Request_Number__c From PPM_Project__c WHERE Request_Number__c IN :requestNumbers ORDER BY CreatedDate];
      
      List<PPM_Project__c> dupProjects = new List<PPM_Project__c>(); 
      Set<Decimal> nonDupRequestNumbers = new Set<Decimal>();
      for(PPM_Project__c maybeDupProject : maybeDupProjects)
      {
        if(nonDupRequestNumbers.contains(maybeDupProject.Request_Number__c))
        {
          dupProjects.add(maybeDupProject);
        }
        else
        {
          nonDupRequestNumbers.add(maybeDupProject.Request_Number__c);
        }
      }
      
      if(dupProjects!=null&&dupProjects.size()>0)
      {
        integer maxRequestNumber = 0;
        AggregateResult[] results = [SELECT MAX(Request_Number__c) maxRequestNumber FROM PPM_Project__c];
  
        if (results!=null&&results.size() > 0) 
        {
          maxRequestNumber = integer.valueOf(results[0].get('maxRequestNumber'));
          }
  
          if(maxRequestNumber!=null && maxRequestNumber > 0)
          {
            for( PPM_Project__c project : dupProjects)
              {
system.debug('\nmaxRequestNumber='+maxRequestNumber);          
                maxRequestNumber = maxRequestNumber + 1;
                }
            
          }
          update dupProjects;
      }
    
  
  
  }

  static private testMethod void testProjectActions()
  {
    
    PPM_Project__c project1 = new PPM_Project__c();
    project1.Name = 'TestAccount';


    PPM_Project__c project2 = new PPM_Project__c();
    project2.Name = 'TestAccount';


    PPM_Project__c project3 = new PPM_Project__c();
    project3.Name = 'TestAccount';


    List<PPM_Project__c> projects = new List<PPM_Project__c>();
    projects.add(project1);
    projects.add(project2);
    projects.add(project3);

    Test.StartTest();
  
    insert projects;

    system.debug('\nproject2.Request_Number__c='+project2.Request_Number__c);    
    system.debug('\nproject2.Request_Number__c='+project2.Request_Number__c);    
    ProjectActions.AssignRequestNumber(projects);

    
    update projects;

    Test.stopTest();
  }

}

 

 

Trigger 

 

trigger ProjectTrigger on PPM_Project__c (after insert, after update, before insert, before update)
{
 if (trigger.isBefore && trigger.isInsert) ProjectActions.AssignRequestNumber(trigger.new);
 if (trigger.isAfter && trigger.isInsert) ProjectActions.DedupRequestNumbers(trigger.new);
} 

chubsubchubsub

You could write a separate trigger to avoid duplicates within the custom object.  Here is some code you can alter for your needs:

http://www.salesforce.com/docs/developer/cookbook/Content/apex_dedupe.htm

 

I also finished writing a trigger on the Quote object that auto-numbers a field based on how many Quotes there are for the related Opportunity.  If you think this code will help, I can provide it to you.