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
MattStevensonMattStevenson 

Incremental Project Number

Hi,

 

I know it s possible to have incremental fields. But what i would like is for the automatic field to be determined by another field on the page. For example if they select Department A the project field would be DEPA001, and so forth. But if they were to choose Department B from the field the project number would be DEPB001.

I have tried a few things but i cannot get the project number to increase incrementally based upon the department selection.

E.g If someone creates an opportunity for Department A the Project Number is DEPA001, but if someone then creates an opportunity for Department B it gives DEPB002.

 

What i would ideally like is for the Project Number number to increase incrementally for each Department, not for all of the opportunites as a whole. If someone could let me know if this is possible to achieve and perhaps point me in a helpful direction that would be great.

 

Many Thanks,

Matt

jhurstjhurst

Matt,

 

What you are talking about would not be possible with the standard Autonumber field.  It could be done through the use of a custom object and apex triggers.

 

The idea would be:

 

1. The custom object would have one record per department.  There would be a name field, department prefix, and a number field.  So the record may be something like:

 

Name: Department A Number Incrementer

Prefix: DEPA

Number: 1

 

2. On your object where you want to have the incremental field, you would have a custom field to hold the number.  This field should be read only to your users, as it will be set when you create the record and then never touched

 

3. On the same object as #2 you will want to create a trigger.  The trigger will look up the Custom record from #1 based on your determining field on the object in #2.  For instance, if you want Record Type A to have a number of DEPA0001 and Record Type B to use DEPB001, Record Type would be your determining field.  The trigger would basically query the custom object, grab the current number and the prefix, combine them to add to the new object, and then increment the number field on the record from #1.

 

This is just a start, and you would have to code to make sure the trigger worked well in bulk, but hopefully it helps.


Jay

b-Forceb-Force

I have similar code sample,

It may help you

 

Class code:

 

public class Utills{
public Utills(){}
public static void LeadAutoNumbering(Lead ld)
{
String ls=ld.LeadSource;
List<Lead> lstLead=[Select LeadReferenceNumber__c, Id From Lead  where LeadSource=:ls order by CreatedDate desc limit 1];
if(lstLead.size() > 0)
{
if(lstLead[0].LeadReferenceNumber__c==null || lstLead[0].LeadReferenceNumber__c=='')
{
ld.LeadReferenceNumber__c=GetFirstNumber(ls);
}
else
{
ld.LeadReferenceNumber__c=GetNextNumber(ls,lstLead[0].LeadReferenceNumber__c);
}
}
else
{
ld.LeadReferenceNumber__c=GetFirstNumber(ls);
}
}
public static string GetNextNumber( String strLeadSource, String strCurrentNumber){
Integer nextNumber=0;
string retRefNumber='PL1001';
if(strLeadSource=='Powerlegal')
{
nextNumber =Integer.ValueOf(strCurrentNumber.replace('PL',''))+1;
retRefNumber='PL'+String.ValueOf(nextNumber);
}
else if(strLeadSource=='Lorrells')
{
nextNumber =Integer.ValueOf(strCurrentNumber.replace('L',''))+1;
retRefNumber='L'+String.ValueOf(nextNumber);
}
else if(strLeadSource=='MA2J')
{
nextNumber =Integer.ValueOf(strCurrentNumber.replace('M',''))+1;
retRefNumber='M'+String.ValueOf(nextNumber);
}
return retRefNumber;
}

public static string GetFirstNumber( string strLeadSource){
Integer nextNumber=0;
string retRefNumber='PL1001';
if(strLeadSource=='Powerlegal')
{
retRefNumber='PL1001';
}
else if(strLeadSource=='Lorrells')
{
retRefNumber='L1001';
}
else if(strLeadSource=='MA2J')
{
retRefNumber='M1001';
}
return retRefNumber;
}

}

 

Trigger code

 

trigger AutoRefNumber on Lead (before insert ) 
{
Utills.LeadAutoNumbering(Trigger.New[0]);
}

 

 

Cheers,

Bala

 

 

b-Forceb-Force

This code is not for BULK,

 

you need to modify it for Bulk

 

Cheers,

Bala