• Gaurav Shukla 61
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

public class ShareholderCompanyTriggerHandler {
    
    public static final Integer MAXIMUM_PERCENTAGE_OF_OWNERSHIP = 100;
    
    public static final String ERROR_MESSAGE_FOR_PERCENTAGE = 'Total Shareholding Percentage cannot be greater than 100% for this application.';
    
    public static ID ShareholderCompanyRecTypeId = RecordTypeHelper.getIdByDeveloperName(
                                                   Consts.Shareholder.getDevName(),
                                                   Consts.Shareholder.RecordTypes.Shareholder_Company);    
    
    //methods invocation for after event on Insert operation
    public static void onAfterInsert(List<Shareholder_Company__c> shareholderList) {
        validatePercentageOfOwnershipField(shareholderList);
    }
    
    //methods invocation for after event on Update operation
    public static void onAfterUpdate(Map<Id,Shareholder_Company__c> oldShareHolderMap, Map<Id,Shareholder_Company__c> newShareHolderMap) {
        validatePercentageOfOwnershipField(oldShareHolderMap,newShareHolderMap);
    }
    

    //method for checking overall percentage of ownership on application object
    //related to it's child object contact and shareholdercompany
    //count all contact records and shareholder company records of shareholder recordtype
    //on insertion of shareholder company object record
    //display error if overall percentage beyond 100%
    private static void validatePercentageOfOwnershipField(List<Shareholder_Company__c> shareholderList) {
                
        Set<ID> applicationIDSet = new Set<ID>();
        for(Shareholder_Company__c company : shareholderList) {
            if(company.RecordTypeId == ShareholderCompanyRecTypeId && company.Percentage_of_Ownership__c != null) {
                applicationIDSet.add(company.Application__c);
            }
        }
        
        Decimal AggregatePercentageOfOwnerShip;
        AggregatePercentageOfOwnerShip = TriggerHandlerUtil.getAllContactsAndShareHolderCompanyRecordsFromApplicationRecord(ApplicationIdSet);
        for(Shareholder_Company__c company : shareholderList) {
            if(AggregatePercentageOfOwnerShip > MAXIMUM_PERCENTAGE_OF_OWNERSHIP) {
                try {
                      company.Percentage_of_Ownership__c.addError(ERROR_MESSAGE_FOR_PERCENTAGE);
                    }
                catch(Exception e) {}
            }
        }
    }
    

    //method for checking overall percentage of ownership on application object
    //related to it's child object contact and shareholdercompany
    //count all contact records and shareholder company records of shareholder recordtype
    //on updation of shareholder company object record
    //display error if overall percentage beyond 100%
    private static void validatePercentageOfOwnershipField(Map<Id,Shareholder_Company__c> oldShareHolderMap, Map<Id,Shareholder_Company__c> newShareHolderMap) {      
        List<Shareholder_Company__c> shareholderList = newShareHolderMap.values();
        Set<ID> ApplicationIDSet = new Set<ID>();
        for(Shareholder_Company__c company : shareholderList) {
            if(
                (company.RecordTypeId == ShareholderCompanyRecTypeId && company.Percentage_of_Ownership__c != null) &&
                (
                    (company.Percentage_of_Ownership__c != oldShareHolderMap.get(company.Id).Percentage_of_Ownership__c) || 
                    (company.Application__c != oldShareHolderMap.get(company.Id).Application__c)
                )
            ) {
                ApplicationIDSet.add(company.Application__c);
              }
        }
        
        Decimal AggregatePercentageOfOwnerShip;
        AggregatePercentageOfOwnerShip = TriggerHandlerUtil.getAllContactsAndShareHolderCompanyRecordsFromApplicationRecord(ApplicationIdSet);
        for(Shareholder_Company__c company : shareholderList) {
            if(AggregatePercentageOfOwnerShip > MAXIMUM_PERCENTAGE_OF_OWNERSHIP) {
                try {
                      company.Percentage_of_Ownership__c.addError(ERROR_MESSAGE_FOR_PERCENTAGE);
                    }
                catch(Exception e) {}
            }
        }
    }
}
=====================================================

//test class for shareholdercompanytriggerhandler class

@IsTest
public class ShareholderCompanyTriggerHandler_Test {
    
    @IsTest
    private static void test_shareholdercompanyPercentage() {
        Consts.Contact_RecordTypes contactRTs = new Consts.Contact_RecordTypes();
        String contactRT = RecordTypeHelper.getIdByDeveloperName('Contact', contactRTs.Acquiring);
        
        Application__c application = TestUtil.createTestApp2();
        if(Application!= null){
            insert application;///////Error in this line 
        }
        system.debug('Application:::::::::'+application);
        //Creating Contact records
        List<Contact> contactsToInsert = new List<Contact>();
        for(Integer i=0; i<10; i++){
            Contact contact1 = new Contact(
                accountcontact__c = true,
                PassportNumber__c = '12345678qwertyuiop',
                PassportExpirationDate__c = Date.today() + 100,
                LastName = 'Test1',
                Nationality__c = 'Indian',
                MobilePhone = '97121333131',
                RecordTypeId = contactRT);            
            Integer randomNumber = Integer.valueof((math.random() * 10));
            if(randomNumber<3)
                Contact1.PercentageOfOwnership__c = 10;
            else if(randomNumber > 3 && randomNumber <=5)
                Contact1.PercentageOfOwnership__c = 30;
            else if(randomNumber > 5 && randomNumber <=8)
                Contact1.PercentageOfOwnership__c = 20;
            else 
                Contact1.PercentageOfOwnership__c = 130;
            contactsToInsert.add(contact1);            
        }  
        Database.insert (contactsToInsert,false);
        for(Contact contact: [Select id,PercentageOfOwnership__c From Contact WHERE PercentageOfOwnership__c!= null ]){
            system.debug('Percentage:::::::::'+contact.PercentageOfOwnership__c);
            System.AssertEquals(true,contact.PercentageOfOwnership__c<100);
        }
        //Creating Shareholder records
        List<Shareholder_Company__c> shareholderCompanyToInsert = new List<Shareholder_Company__c>();        
        Id ShareHolderComRecordTypeId = RecordTypeHelper.getIdByDeveloperName(Consts.Shareholder.getDevName(), Consts.Shareholder.RecordTypes.Shareholder_Company);
        for(Integer j=0; j<10;j++){
            Shareholder_Company__c shareholder1 = new Shareholder_Company__c();
            shareholder1.RecordTypeId = ShareHolderComRecordTypeId;
            Integer randomNumber = Integer.valueof((math.random() * 10));
            if(randomNumber<3)
                shareholder1.Percentage_of_Ownership__c = 10;
            else if(randomNumber > 3 && randomNumber <=5)
                shareholder1.Percentage_of_Ownership__c = 30;
            else if(randomNumber > 5 && randomNumber <=8)
                shareholder1.Percentage_of_Ownership__c = 20;
            else 
                shareholder1.Percentage_of_Ownership__c = 130;
            shareholder1.Trade_License_number__c = '4824';
            shareholder1.Name = 'test shareholder company';
            shareholder1.Date_of_Establishment__c = Date.today() - 20;
            shareholder1.Country_of_Establishment__c = 'United Arab Emirates';
            shareholderCompanyToInsert.add(shareholder1);
        }         
        Database.insert (shareholderCompanyToInsert,false);
        for(Shareholder_Company__c shareholder: [Select id,Percentage_of_Ownership__c From Shareholder_Company__c WHERE Percentage_of_Ownership__c!= null ]){
            System.AssertEquals(true,shareholder.Percentage_of_Ownership__c<100);
        }
    }
    
}
 

public class ShareholderCompanyTriggerHandler {
    
    public static final Integer MAXIMUM_PERCENTAGE_OF_OWNERSHIP = 100;
    
    public static final String ERROR_MESSAGE_FOR_PERCENTAGE = 'Total Shareholding Percentage cannot be greater than 100% for this application.';
    
    public static ID ShareholderCompanyRecTypeId = RecordTypeHelper.getIdByDeveloperName(
                                                   Consts.Shareholder.getDevName(),
                                                   Consts.Shareholder.RecordTypes.Shareholder_Company);    
    
    //methods invocation for after event on Insert operation
    public static void onAfterInsert(List<Shareholder_Company__c> shareholderList) {
        validatePercentageOfOwnershipField(shareholderList);
    }
    
    //methods invocation for after event on Update operation
    public static void onAfterUpdate(Map<Id,Shareholder_Company__c> oldShareHolderMap, Map<Id,Shareholder_Company__c> newShareHolderMap) {
        validatePercentageOfOwnershipField(oldShareHolderMap,newShareHolderMap);
    }
    

    //method for checking overall percentage of ownership on application object
    //related to it's child object contact and shareholdercompany
    //count all contact records and shareholder company records of shareholder recordtype
    //on insertion of shareholder company object record
    //display error if overall percentage beyond 100%
    private static void validatePercentageOfOwnershipField(List<Shareholder_Company__c> shareholderList) {
                
        Set<ID> applicationIDSet = new Set<ID>();
        for(Shareholder_Company__c company : shareholderList) {
            if(company.RecordTypeId == ShareholderCompanyRecTypeId && company.Percentage_of_Ownership__c != null) {
                applicationIDSet.add(company.Application__c);
            }
        }
        
        Decimal AggregatePercentageOfOwnerShip;
        AggregatePercentageOfOwnerShip = TriggerHandlerUtil.getAllContactsAndShareHolderCompanyRecordsFromApplicationRecord(ApplicationIdSet);
        for(Shareholder_Company__c company : shareholderList) {
            if(AggregatePercentageOfOwnerShip > MAXIMUM_PERCENTAGE_OF_OWNERSHIP) {
                try {
                      company.Percentage_of_Ownership__c.addError(ERROR_MESSAGE_FOR_PERCENTAGE);
                    }
                catch(Exception e) {}
            }
        }
    }
    

    //method for checking overall percentage of ownership on application object
    //related to it's child object contact and shareholdercompany
    //count all contact records and shareholder company records of shareholder recordtype
    //on updation of shareholder company object record
    //display error if overall percentage beyond 100%
    private static void validatePercentageOfOwnershipField(Map<Id,Shareholder_Company__c> oldShareHolderMap, Map<Id,Shareholder_Company__c> newShareHolderMap) {      
        List<Shareholder_Company__c> shareholderList = newShareHolderMap.values();
        Set<ID> ApplicationIDSet = new Set<ID>();
        for(Shareholder_Company__c company : shareholderList) {
            if(
                (company.RecordTypeId == ShareholderCompanyRecTypeId && company.Percentage_of_Ownership__c != null) &&
                (
                    (company.Percentage_of_Ownership__c != oldShareHolderMap.get(company.Id).Percentage_of_Ownership__c) || 
                    (company.Application__c != oldShareHolderMap.get(company.Id).Application__c)
                )
            ) {
                ApplicationIDSet.add(company.Application__c);
              }
        }
        
        Decimal AggregatePercentageOfOwnerShip;
        AggregatePercentageOfOwnerShip = TriggerHandlerUtil.getAllContactsAndShareHolderCompanyRecordsFromApplicationRecord(ApplicationIdSet);
        for(Shareholder_Company__c company : shareholderList) {
            if(AggregatePercentageOfOwnerShip > MAXIMUM_PERCENTAGE_OF_OWNERSHIP) {
                try {
                      company.Percentage_of_Ownership__c.addError(ERROR_MESSAGE_FOR_PERCENTAGE);
                    }
                catch(Exception e) {}
            }
        }
    }
}
=====================================================

//test class for shareholdercompanytriggerhandler class

@IsTest
public class ShareholderCompanyTriggerHandler_Test {
    
    @IsTest
    private static void test_shareholdercompanyPercentage() {
        Consts.Contact_RecordTypes contactRTs = new Consts.Contact_RecordTypes();
        String contactRT = RecordTypeHelper.getIdByDeveloperName('Contact', contactRTs.Acquiring);
        
        Application__c application = TestUtil.createTestApp2();
        if(Application!= null){
            insert application;///////Error in this line 
        }
        system.debug('Application:::::::::'+application);
        //Creating Contact records
        List<Contact> contactsToInsert = new List<Contact>();
        for(Integer i=0; i<10; i++){
            Contact contact1 = new Contact(
                accountcontact__c = true,
                PassportNumber__c = '12345678qwertyuiop',
                PassportExpirationDate__c = Date.today() + 100,
                LastName = 'Test1',
                Nationality__c = 'Indian',
                MobilePhone = '97121333131',
                RecordTypeId = contactRT);            
            Integer randomNumber = Integer.valueof((math.random() * 10));
            if(randomNumber<3)
                Contact1.PercentageOfOwnership__c = 10;
            else if(randomNumber > 3 && randomNumber <=5)
                Contact1.PercentageOfOwnership__c = 30;
            else if(randomNumber > 5 && randomNumber <=8)
                Contact1.PercentageOfOwnership__c = 20;
            else 
                Contact1.PercentageOfOwnership__c = 130;
            contactsToInsert.add(contact1);            
        }  
        Database.insert (contactsToInsert,false);
        for(Contact contact: [Select id,PercentageOfOwnership__c From Contact WHERE PercentageOfOwnership__c!= null ]){
            system.debug('Percentage:::::::::'+contact.PercentageOfOwnership__c);
            System.AssertEquals(true,contact.PercentageOfOwnership__c<100);
        }
        //Creating Shareholder records
        List<Shareholder_Company__c> shareholderCompanyToInsert = new List<Shareholder_Company__c>();        
        Id ShareHolderComRecordTypeId = RecordTypeHelper.getIdByDeveloperName(Consts.Shareholder.getDevName(), Consts.Shareholder.RecordTypes.Shareholder_Company);
        for(Integer j=0; j<10;j++){
            Shareholder_Company__c shareholder1 = new Shareholder_Company__c();
            shareholder1.RecordTypeId = ShareHolderComRecordTypeId;
            Integer randomNumber = Integer.valueof((math.random() * 10));
            if(randomNumber<3)
                shareholder1.Percentage_of_Ownership__c = 10;
            else if(randomNumber > 3 && randomNumber <=5)
                shareholder1.Percentage_of_Ownership__c = 30;
            else if(randomNumber > 5 && randomNumber <=8)
                shareholder1.Percentage_of_Ownership__c = 20;
            else 
                shareholder1.Percentage_of_Ownership__c = 130;
            shareholder1.Trade_License_number__c = '4824';
            shareholder1.Name = 'test shareholder company';
            shareholder1.Date_of_Establishment__c = Date.today() - 20;
            shareholder1.Country_of_Establishment__c = 'United Arab Emirates';
            shareholderCompanyToInsert.add(shareholder1);
        }         
        Database.insert (shareholderCompanyToInsert,false);
        for(Shareholder_Company__c shareholder: [Select id,Percentage_of_Ownership__c From Shareholder_Company__c WHERE Percentage_of_Ownership__c!= null ]){
            System.AssertEquals(true,shareholder.Percentage_of_Ownership__c<100);
        }
    }
    
}