You need to sign in to do that
Don't have an account?
Suraj Demgunde
i have three field item no,vendor name and lead time now user want if item no is same and vendor name is same the need avg of both Lead time how its possible using trigger? x
like
item no vendor Lead time calcalted filed 1
1 x 5 avg==5
1 x 5
item no vendor Lead time calcalted filed 1
1 x 5 avg==5
1 x 5
-----------------------
trigger AccountTrigger on Account (after insert, before update) {
// On Before Update
if(Trigger.isBefore && Trigger.isUpdate) {
AccountTriggerHandler.onBeforeUpdate(Trigger.New);
}
// On After Insert
if(Trigger.isAfter && Trigger.isInsert) {
AccountTriggerHandler.onAfterSave(Trigger.New);
}
}
Handler:
-------------------
public class AccountTriggerHandler {
// Method to execute on After Insert
public static void onAfterSave(List<Account> accounts) {
// Map to update the List of Accounts
Map<String, Account> updateAccounts = new Map<String, Account>();
// To perform the logic on Account
accounts = performLogic(accounts);
// Condition to check for the size of the List of Accounts after performing the logic
if(accounts.size() > 0) {
// Loop to iterate over the list of Accounts that you want to update
for(Account account : accounts) {
Account updateAccount = new Account(Id = account.Id, Name = account.Name, Avg_Time__c = account.Avg_Time__c);
updateAccounts.put(updateAccount.Name, updateAccount);
}
}
// Preforming the DML operation
if(updateAccounts.size() > 0) {
update updateAccounts.Values();
}
}
// Method to excuete on Before save of Account Records
public static void onBeforeUpdate(List<Account> accounts){
// To perform the logic on Account
accounts = performLogic(accounts);
}
// Method to perform the logic on Account Records
private static List<Account> performLogic(List<Account> accounts) {
Set<Decimal> accNumSet = new Set<Decimal>();
Set<String> accNameSet = new Set<String>();
// Loop to iterate over the list of accounts
for(Account account : accounts) {
accNumSet.add(account.Account_Number__c);
accNameSet.add(account.Name);
}
// condition to check for the prepared sets
if(accNumSet.size() > 0 && accNameSet.size() > 0) {
Map<String, account> accountMap = getAccounts(accNumSet, accNameSet);
}
return accounts;
}
// Method to get the existing accounts from database
private static Map<String, account> getAccounts(Set<Decimal> accNumSet, Set<String> accNameSet) {
Map<String, account> accountMap = new Map<String, account>();
List<Account> accountList = [SELECT ID, Account_Number__c , Name, Lead_Time__c, Avg_Time__c
FROM Account
WHERE Name = :accNameSet
AND Account_Number__c = :accNumSet];
// Condtion to check for the List of accounts that returned based on the SOQL query
if(accountList.size() > 0) {
Decimal avgTime = 0.0;
Integer size = 0;
// Loop to iterate over the query results
for(Account account : accountList) {
// Condition to check if the account name is already exists in account Map
if(accountMap.containsKey(account.Name)) {
size = size + 1;
avgTime = +account.Lead_Time__c /size;
account.Avg_Time__c = avgTime;
accountMap.put(account.Name, account);
} else {
size = size + 1;
avgTime = +account.Lead_Time__c /size;
account.Avg_Time__c = avgTime;
accountMap.put(account.Name, account);
}
}
}
return accountMap;
}
}
Test class with 100% code coverage:
----------------------------------------------------------
@isTest
public class AccountTriggerHandlerTest {
@testSetUp static void prepareTestData() {
Account account = new Account(Name = 'WXYZ', Account_Number__c = 1, Lead_Time__c = 2);
insert account;
List<Account> accounts = new List<Account>();
for(Integer i=0; i<2; i++) {
Account account1 = new Account(Name = 'XYZ', Account_Number__c = 2, Lead_Time__c = 2);
accounts.add(account1);
}
insert accounts;
}
@isTest static void AccountInsertTest() {
Account account = [Select Id, Name from Account Where Name = 'WXYZ'];
System.assert(account != null);
}
@isTest static void AccountUpdateTest() {
Account account = [Select Id, Name from Account Where Name = 'WXYZ'];
update account;
}
}
Hope this will help you.