Write a trigger on Account when account is insert then calcuate total account those are vendor or buyer

Write a trigger on Account when Account is insert then calculate the  MaxCount for Account Type:Create Two custom field on account one field is AccountType(PickList) and add two value "Vendor" and other is "Buyer" and create Second field MaxCount(Number). when Account is added and AccountType__c = Vendor or Buyer the calculate the No of Account those have AccountType__C = Vendor or Buyer and Add value in MaxCount__c field.


trigger onAccountUpdateAccoutType on Account (before insert) {
      AggregateResult aggResBuyer = [select max(maxcount__c) from account where AccoutType__c='buyer'];
       AggregateResult aggResVendor = [select max(maxcount__c) from account where AccoutType__c='vendor'];
       Integer buyer,vendor;
       if(aggResBuyer.get('expr0')==null)
       {
        buyer=0;
       }
       else
       {
         buyer = integer.valueof(aggResBuyer.get('expr0'));
       
       }
       if(aggResVendor.get('expr0')==null)
       {
        vendor=0;
       }
       else
       {
         vendor = integer.valueof(aggResVendor.get('expr0'));
       }
      
   buyer++;
      vendor++;
     
      
    for(account acc:trigger.new)
    {
        if(acc.AccoutType__c!='none')
        {
            if(acc.AccoutType__c=='buyer')
            {
                acc.maxcount__c=buyer;
                 buyer++;
            }
            else
            {
                acc.maxcount__c=vendor;
                 vendor++;
            }
        }
    }

}

Comments

Popular Post

Show Hyper Link On Add Error in Salesforce Trigger

Find Day of selected Date on VF Page

Write The Dynamic Query in Salesforce and bind in page block table with filter conditions