• Andrew Gillan 30
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 6
    Replies
Create custom field on Contact for "Sequence Number". Create trigger to manage sequence of Contacts on an Account. Handle all the events and cases.

# Re-order sequence number when new records are inserted
# Re-order sequence number when records are updated
# Re-order sequence number when records are deleted
# In case of re-parenting, add record at last position
# In case of undelete, add record at last position

- Create test sheet for all the test cases
- Create test methods for all the test cases, please refer acceptance criteria                                                                                                                                                                                                                      1) Bulk Re-parenting
2) Bulk update sequence number
3) Remove Parent
4) Bulk (Create, Update, Delete, Undelete)
5) update legacy (Existing) records

Test class coverage should be above 95%
Hi, I am new to test class. Would anyone help with the Too many SOQL queries 101 test clsss error ?  I am getting the Too many SOQL queries in each method where I quary the user.  Thank you for looking at it. 
@isTest
public class Test_VR_WFMRestrictStatusCxlToSup {
    
    @TestSetup
    static void createTestData(){
        User fsrUser = [Select Id,UserRole.Name,Name,Profile.Name,Username From User 
                        Where UserRole.Name = 'FSR-DX Mid Atlantic' 
                        AND Profile.Name = 'Field Support Rep - CAG'
                        AND Reporting_Role__c = 'FSR DX'
                        AND isActive = true LIMIT 1];    
         User fsrUserFSRDR = [Select Id,UserRole.Name,Name,Profile.Name,Username From User 
                        Where UserRole.Name = 'FSR-DR West' 
                        AND Profile.Name = 'Field Support Rep - CAG'
                        AND Reporting_Role__c = 'FSR DR'
                        AND isActive = true LIMIT 1];                
        
        Account placeHolderAcc1 = new ACCOUNT();
        Account placeHolderAcc2 = new ACCOUNT();
        Account placeHolderAcc3 = new ACCOUNT();
        Account placeHolderAcc4 = new ACCOUNT();
        
        System.runAs(fsrUser) {
            placeHolderAcc1 = new Account(Name='Test Account WFM VR1', SAP_Customer_Number__c ='1111198700', ShippingCountryCode='US', ShippingState='Maine',
                                        BillingCountryCode='US',BillingStreet='1 ', BillingCity='Pord',BillingState='Maine', 
                                        BillingPostalCode='04092',TIN__c='111111111');
            insert placeHolderAcc1;
            
            placeHolderAcc2 = new Account(Name='Test Account WFM VR2', SAP_Customer_Number__c ='1111198701', ShippingCountryCode='US', ShippingState='Maine',
                                        BillingCountryCode='US',BillingStreet='1 street', BillingCity='Wes',BillingState='Mai', 
                                        BillingPostalCode='04092',TIN__c='111111112');
            insert placeHolderAcc2;
            
            placeHolderAcc3 = new Account(Name='Test Account WFM VR3', SAP_Customer_Number__c ='1111198702', ShippingCountryCode='US', ShippingState='Maine',
                                        BillingCountryCode='US',BillingStreet='1 street', BillingCity='West',BillingState='Mae', 
                                        BillingPostalCode='04092',TIN__c='111111113');
            insert placeHolderAcc3;
  }         
    
    System.runAs(fsrUserFSRDR) {
            placeHolderAcc4 = new Account(Name='Test Account WFM VR4', SAP_Customer_Number__c ='1111198704', ShippingCountryCode='US', ShippingState='Maine',
                                        BillingCountryCode='US',BillingStreet='1 streeet', BillingCity='We',BillingState='ME', 
                                        BillingPostalCode='04092',TIN__c='111111111');
            insert placeHolderAcc4;
    
    
     }    
      
       RecordType woProactive4 = [SELECT Id,Name,SobjectType FROM RecordType WHERE SobjectType = 'WorkOrder' and Name = 'Proactive' Limit 1];
        System.runAs(fsrUserFSRDR) {
            WorkOrder createWorkOrder4 = new WorkOrder(AccountId = placeHolderAcc4.Id, Status = 'New', RecordTypeId = woProactive4.Id);
            insert createWorkOrder4;
    
   }    
        
        RecordType woProactive = [SELECT Id,Name,SobjectType FROM RecordType WHERE SobjectType = 'WorkOrder' and Name = 'Proactive' Limit 1];
        System.runAs(fsrUser) {
            WorkOrder createWorkOrder1 = new WorkOrder(AccountId = placeHolderAcc1.Id, Status = 'New', RecordTypeId = woProactive.Id);
            insert createWorkOrder1;
            WorkOrder createWorkOrder2 = new WorkOrder(AccountId = placeHolderAcc2.Id, Status = 'New', RecordTypeId = woProactive.Id);
            insert createWorkOrder2;
            WorkOrder createWorkOrder3 = new WorkOrder(AccountId = placeHolderAcc3.Id, Status = 'New', RecordTypeId = woProactive.Id);
            insert createWorkOrder3;
        
        }
    }
    
    // Testing that non-direct supervisor, supervisor can cancel work order
    @IsTest(SeeAllData = false)
    static void testSupervisorCanCancelWO() {
        User fsrSupUser = [Select Id,UserRole.Name,Name,Profile.Name,Username From User 
                           Where UserRole.Name = 'FSR-DX Supervisor North Central' 
                           AND Profile.Name = 'Field Support Rep - CAG'
                           AND Reporting_Role__c = 'FSR DX Supervisor'
                           AND isActive = true LIMIT 1];
        
        system.debug('Supervisor User: ' + fsrSupUser.Name);
        
        WorkOrder w = [Select id From WorkOrder WHERE Account.Name = 'Test Account WFM VR1'];
        w.status = 'Canceled';
        w.Cancellation_Reason__c = 'Duplicate';
        w.Cancellation_Notes__c = 'Some notes';
        Test.startTest();
        System.runAs(fsrSupUser) {
            Update w;
        }
        Test.stopTest();
        
        w = [Select id, status, Cancellation_Reason__c From WorkOrder WHERE Account.Name = 'Test Account WFM VR1'];
        System.assertEquals('Canceled', w.Status);
        
    }
    
    // testing that owner of work order can cancel work order
    @IsTest(SeeAllData = false)
    static void testOwnerCanCancelWO() {
        User fsrUser = [Select Id,UserRole.Name,Name,Profile.Name,Username From User 
                        Where UserRole.Name = 'FSR-DX Mid Atlantic' 
                        AND Profile.Name = 'Field Support Rep - CAG'
                        AND Reporting_Role__c = 'FSR DX'
                        AND isActive = true LIMIT 1];
        
        system.debug('fsr User: ' + fsrUser.Name);
        
        WorkOrder w = [Select id From WorkOrder WHERE Account.Name = 'Test Account WFM VR2'];
        w.status = 'Canceled';
        w.Cancellation_Reason__c = 'Duplicate';
        w.Cancellation_Notes__c = 'Some notes';
        Test.startTest();
        System.runAs(fsrUser) {
            Update w;
        }
        Test.stopTest();
        
        w = [Select id, status, Cancellation_Reason__c From WorkOrder WHERE Account.Name = 'Test Account WFM VR2'];
        System.assertEquals('Canceled', w.Status);
        
    }
    
    // testing that owner of work order can cancel work order US78297 Added new Reporting role FSR DR that can cancel the order
    @IsTest(SeeAllData = false)
    static void testOwnerCanCancelWOFSRDR() {
        User fsrUserFSRDR = [Select Id,UserRole.Name,Name,Profile.Name,Username From User 
                        Where UserRole.Name = 'FSR-DR West' 
                        AND Profile.Name = 'Field Support Rep - CAG'
                        AND Reporting_Role__c = 'FSR DR'
                        AND isActive = true LIMIT 1];
        
        system.debug('fsrUserFSRDR User: ' + fsrUserFSRDR.Name);
        
        WorkOrder w = [Select id From WorkOrder WHERE Account.Name = 'Test Account WFM VR4'];
        w.status = 'Canceled';
        w.Cancellation_Reason__c = 'Duplicate';
        w.Cancellation_Notes__c = 'Some notes';
        Test.startTest();
        System.runAs(fsrUserFSRDR) {
            Update w;
        }
        Test.stopTest();
        
        w = [Select id, status, Cancellation_Reason__c From WorkOrder WHERE Account.Name = 'Test Account WFM VR4'];
        System.assertEquals('Canceled', w.Status);
        
    }
    
    // Test that non owner, non supervisor can't cancel work order
    @IsTest(SeeAllData = false)
    static void testNonOwnerNonSupCantCancelWO() {
        WorkOrder w = [SELECT Id, OwnerId FROM WorkOrder WHERE Account.Name = 'Test Account WFM VR3'];
        User fsrUser = [SELECT Id, UserRole.Name, Name, Profile.Name, Username FROM User 
                        WHERE UserRole.Name = 'FSR-DX Mid Atlantic' 
                        AND Profile.Name = 'Field Support Rep - CAG'
                        AND Reporting_Role__c = 'FSR DX'
                        AND Id != :w.OwnerId 
                        AND isActive = true LIMIT 1];
        w.status = 'Canceled';
        w.Cancellation_Reason__c = 'Duplicate';
        w.Cancellation_Notes__c = 'Some notes';
        Test.startTest();
        try {
            System.runAs(fsrUser) {
                Update w;
            }
        } catch(DMLException de) {
            System.debug('Error: ' + de.getMessage());
        }
        
        Test.stopTest();
        
        w = [Select id, status, Cancellation_Reason__c From WorkOrder WHERE Account.Name = 'Test Account WFM VR3'];
        System.assertNotEquals('Canceled', w.Status);
        
    }
}
I've a requirement where I need to send an alert when the last case created for each category (record type) is more than 5 days from today. Example: For record type "A", the last record created was on June 5, 2023. So, I should be sending an alert for this record type. I've created a Schedule Trigger Flow which runs at a particular interval and is sending an alert when it finds the last record for all the record types created was more than 5 days. How do I put a loop inside this to segregate the records according to the record type and send alerts for each record type. This is how my Flow and the "Get Cases" node are looking now: 
User-added imageUser-added imageHow do I segregate the records based on record types? Please advise. Thanks.

Hi, 

I have created a trigger which is on account team member object, this trigger is initiated when we delete(after delete) an account team member that is associated with an account. My trigger is working in all the conditions expect for the last record. ex: when there is only one member left in the account team member it is able to delete it on the account team member object but, the same thing is not been reflected on the Account object's network id field. On this field Account.network_id__c field I am trying to log all the account team members associated with that account. 

Below is the trigger  along with the helper class

trigger AccountTeamMemberTrigger on AccountTeamMember (after delete, after update) {
    
    if(trigger.isAfter){
         if(trigger.isDelete){
        AccountTeamMemberTriggerHelper.updateAccountNetworkId(trigger.old);
    }}
    

 

-------------------helper class------------
public without sharing class AccountTeamMemberTriggerHelper {
    private static Set<Id> accountIdSet= new Set<Id>();
    private static List<Account> accListToUpdate = new List<Account>();
    private static Map<Id, String> accountMapInfo = new Map<Id, String>();
    
public static void updateAccountNetworkId(List<AccountTeamMember> triggerNew){
    for(AccountTeamMember atm : triggernew){
       accountIdSet.add(atm.AccountId);
    }

    networkIdProcess();
}

private static void networkIdProcess(){
    for(AccountTeamMember atm : [SELECT User.Network_Id__c, AccountId FROM AccountTeamMember WHERE AccountId IN : accountIdSet]){
        addNetworkId(atm);
    }

    // Loop through the account map a second time to update the accounts
    for(Id accId : accountMapInfo.keySet()){
        Account acc = new Account(Id = accId, Account_Team_Network_ID_String__c = accountMapInfo.get(accId));
        accListToUpdate.add(acc);
    }

    if(accListToUpdate.size() > 0) updateAccounts();
}

private static void addNetworkId(AccountTeamMember atm){
    String teamNetworkId = accountMapInfo.get(atm.AccountId) != null ? accountMapInfo.get(atm.AccountId) : '';

   if(String.isNotBlank(atm.User.Network_Id__c))
        {
            teamNetworkId += atm.User.Network_Id__c + '-';
        }

    accountMapInfo.put(atm.AccountId, teamNetworkId);
}

private static void updateAccounts(){
    Savepoint sp = Database.setSavepoint();
    try{
        update accListToUpdate;
    } catch(DmlException e){
        system.debug('Error updating the Account: ' + e);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Could not update the Account on Trigger. Please contact your system administrator.'));
        Database.rollback(sp); 
    }
}

}

 

I found a formula that returns an age expressed in years, months and days when a date is entered in the DOB field. Now I need a DOB entered based on the age field (again expressed as years, months, days.) 

This is for animals where we can be supplied with either an approximate DOB or an approximate age. We need which ever of these two fields is blank to auto-populate based on it's complementary field. 

Thank you.