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
Vin.Vin. 

How to automatically incremente a number based on a specific record type

Hi,

I would like to automatically incremente a Number field for leads that are in a specific record type. 

The only options I see is to create an assignment rule or a trigger that automatically incremente the number field only when a lead from the record type is created. 

However, I don't know how to create a trigger or which assignment could make such thing. Can someone assist on this?

Thanks.
Dushyant Kumar 10Dushyant Kumar 10
Trigger LeadTrigget on Lead (before Insert) {
    Id recordTpId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('RecordTypeName').getRecordTypeId();
    String query='SELECT COUNT() FROM Account WHERE RecordTypeId=\''+recordTpId+'\'';
    Integer count=database.countQuery(query);

    for(Lead ld:Trigger.new){
        if(ld.RecordTypeId == recordTpId){
            count++;
            ld.Count__c=count;
        }
    }
}

Replace 'RecordTypeName,Count__c' by your record type name and custom field api name respectively.
It's not the best practice to write code like this. But, hope it will solve your problem.