+ Start a Discussion
lucky25lucky25 

trigger

for example in new account if you choose industry type=education   account number=1 next you choose industry type=eduction ,account number=2 how to write using trigger

CheyneCheyne

In your trigger, you could start by getting the maximum account number for industry type = education, and storing that in a variable, "max". Next, you could loop through the records in the trigger, and check if the industry type is education. If it is, set its account number to "max" + 1, and then increase max by one. You would need something like the code below (not tested, assuming you are working with Leads):

 

trigger updateAcctNumber on Lead(after insert, after update){
    List<Lead> leadsToUpdate = new List<Lead>();
    
    //Get the current maximum account number here
    List<Lead> existingLeads = [SELECT Account_Number__c FROM Lead WHERE Industry_Type__c = 'Education' ORDER BY Account_Number__c DESC LIMIT 1];
    Integer max = existingLeads[0].Account_Number__c;
    
    for (Lead l: Trigger.new){
        if (l.Industry_Type__c == 'Education') {
            max = max + 1;
            l.Account_Number__c = max;
            leadsToUpdate.add(l);
        }
    }
    update leadsToUpdate;
}

 

If you have other industry types that you need to do this for, you could use the same process, but you might want to use a Map to store the maximum values for each industry type.