• Brendan M
  • NEWBIE
  • 30 Points
  • Member since 2023

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

Hey Community, 

I'm a junior developer looking to get a feel for best practice when it comes to trigger helper/handler methods that take take an old and new map then apply some logic in a bulkified and efficient way (0 of N if possible). 

Will include my example to see if possible to make more efficient, but if anyone has a better example or template they use it would really help me on my journey? 

Task was to improve this method, business process had actually simplified since this was made so it was easier but I'm sure it has lots of room for improvement. 

Original method: 

public static void assignReopenedCases(Map<ID,Case> oldMap, Map<ID,Case> newMap) {
        
        Case oldCase;
        Case newCase;
        Set<String> formIDs = new Set<String>{'4','11','14','17','21','24'};
        
        if(Userinfo.getLastName()!=Constants.USER_API_USER) return;
        
        List<Case> cases = [SELECT Id, OwnerId, Cease_Related_to__c, FormName__r.FormID__c, LastQueueID__c FROM Case WHERE Id IN :newMap.keySet()];
        Map<String,Id> queuesByName = getQueuesByName();
        Map<Id,Id> caseMap = new Map<Id,Id>();
        for(Case c : cases) {
            newCase = newMap.get(c.Id);
            oldCase = oldMap.get(c.Id);
            if(oldCase.Status==Constants.CASE_STATUS_CLOSED && newCase.Status==Constants.CASE_STATUS_REOPENED) {
                if(newCase.RecordTypeName__c == Constants.CASE_REC_TYPE_OPERATIONS) {
                    if(formIDs.contains(c.FormName__r.FormID__c) && c.Cease_Related_to__c!=null && c.Cease_Related_to__c.contains('Vehicles')) {
                        //assign to 'Vehicles'
                        caseMap.put(c.Id, queuesByName.get(Constants.QUEUE_VEHICLES));
                    }
                    else {
                        //assign to 'General'
                        caseMap.put(c.Id, queuesByName.get(Constants.QUEUE_GENERAL));
                    }
                }
                else if(newCase.RecordTypeName__c == Constants.CASE_REC_TYPE_CMT_RECONCILIATION){
                    //assign to 'CMT Reconciliation' queue
                    caseMap.put(c.Id, queuesByName.get(Constants.QUEUE_VEHICLES));
                }
                else if(newCase.RecordTypeName__c == Constants.CASE_REC_TYPE_CMT_RECOVERY) {
                    //assign to 'CMT Recovery' queue
                    caseMap.put(c.Id, queuesByName.get(Constants.QUEUE_CMT_RECOVERIES));
                }
                else if(c.LastQueueId__c != null && c.OwnerId != queuesByName.get(Constants.QUEUE_VEHICLES) 
                                && c.OwnerId != queuesByName.get(Constants.QUEUE_GENERAL)
                                && newCase.RecordTypeName__c != Constants.CASE_REC_TYPE_SMARTFLEET_CUST_SRV){
                    //assign to last queue if not assigned to 'Vehicles' or 'General' queue
                    caseMap.put(c.Id, c.LastQueueId__c);
                }
            }                
        }
        
        if(!caseMap.isEmpty()) {
            assignCaseToQueue(caseMap);
        }
    }


My Improved method, looking for further improvements:

public static void assignReopenedCases(Map<ID,Case> oldMap, Map<ID,Case> newMap) {
        Case oldCase;
        if(Userinfo.getLastName() != Constants.USER_API_USER) return;
    
        Map<Id, Id> caseMap = new Map<Id, Id>();
        
        for (Case newCase : newMap.values()) {
            oldCase = oldMap.get(newCase.Id);
            if (oldCase.Status == Constants.CASE_STATUS_CLOSED && newCase.Status == Constants.CASE_STATUS_REOPENED) {
                if (newCase.LastQueueId__c != null) {
                    caseMap.put(newCase.Id, newCase.LastQueueId__c);
          
                 }
            }
        }
    
        if (!caseMap.isEmpty()) {
            assignCaseToQueue(caseMap);
        }
    }

 

Please let me know any changes that are possible! Would love to make it more efficient or remove any further unecessary code. 

Thanks community! 

Hi Community, 
 

I'm new to developing and I'm keen to learn the best way to compare two collections for future use on a trigger or method. 

For example for an investigation into org missing data if I wanted to compare Leads and Accounts to check which Leads had matching email addresses with Accounts but other personal information was diffrerent. 

At the moment I only know how to do this with two for loops but there must be a better way? possibly using maps or a map and list?

Account[] personalAccounts = [SELECT Id, FirstName, LastName, Phone, PersonMobilePhone, PersonEmail, EmployerCode__c, DOB__c FROM Account WHERE LastName != null AND PersonEmail != null AND DOB__c != null];

Lead[] leads = [SELECT Id, FirstName, LastName, Phone, MobilePhone, Email, Employer_Code__c, DOB__c FROM Lead WHERE Email != null AND DOB__c != null];

Sudo code

for Leads {
  for Accounts {
   If (Lead.email == Account.PersonalEmail && Lead.DOB__c != Account.DOB__c && Lead.EmployerCode__c != Account.EmployerCode__c etc )

  cleanUpMap.put(Lead.Id, Account.Id);

}
}

Appreciate the help, sorry for the terrible code. 

Hi Community! 

Currently trying to add a field permission to multiple Profiles to save me some time in the future. Have included the error and code below. Any ideas what I'm doing wrong? Or has anyone done this before? 

I'm sure it worked on permission sets. Anyways appreciate any help! Thanks 

Currently getting the error:

Line: 19, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Parent ID: id value of incorrect type: 00e2P000000LtXfQAK: [ParentId]

From: 
// Create a Map of Profiles to update. 
Map<Id, Profile> profileMap = new Map<Id, Profile>([SELECT Id, Name FROM Profile WHERE Name LIKE 'Service%' OR Name LIKE 'API%' OR Name LIKE 'System%']);
// Create List of Field Permissions to add. 
FieldPermissions[] fieldPermissionsToInsert = new List<FieldPermissions>(); 

// Loop through and create Field Permissions. 
For(Id i : profileMap.keyset()) {
  FieldPermissions fp = new FieldPermissions();
  fp.Field ='Package__c.AvailableBalance__c';//the name of new field
  fp.ParentId = i; // Get the Id of the Parent - Permission set or profile.  
  fp.PermissionsEdit=false;//
  fp.PermissionsRead=true;
  fp.SobjectType='Package__c';
  fieldpermissionsToInsert.add(fp);
}

// Insert Field Permissions. 
if(fieldPermissionsToInsert.size() > 0) {
    insert fieldPermissionsToInsert;
}

Looking to save SOQL queries in various apex classes. Just wondering if its possible to use a schema or describe method instead of an SOQL query to retrieve them?

Really appreciate any information, help or sample solution.  

Code sample below where I would like to replace the SOQL if possible: 

 

private static Map<String,ID> getQueuesByName(){
        Map<String,ID> queuesByName = new Map<String,ID>();
        
        Set<String> queueNameSet = new Set<String>{Constants.QUEUE_VEHICLES,Constants.QUEUE_GENERAL,
                                                    Constants.QUEUE_CMT_RECONCILIATIONS,Constants.QUEUE_CMT_RECOVERIES,
                                                    Constants.QUEUE_SFLEET_ADMIN,Constants.QUEUE_SFLEET_ACC_MGMT};
        
        List<Group> queues = [
            SELECT 
                Id,DeveloperName 
            FROM 
                Group 
            WHERE 
            Type = 'Queue' AND DeveloperName IN :queueNameSet
        ];

        if (!queues.isEmpty()){
            for(Group queue : queues ) {
                queuesByName.put(queue.DeveloperName, queue.Id);
            }
        }

        return queuesByName;
    }

Looking to save SOQL queries in various apex classes. Just wondering if its possible to use a schema or describe method instead of an SOQL query to retrieve them?

Really appreciate any information, help or sample solution.  

Code sample below where I would like to replace the SOQL if possible: 

 

private static Map<String,ID> getQueuesByName(){
        Map<String,ID> queuesByName = new Map<String,ID>();
        
        Set<String> queueNameSet = new Set<String>{Constants.QUEUE_VEHICLES,Constants.QUEUE_GENERAL,
                                                    Constants.QUEUE_CMT_RECONCILIATIONS,Constants.QUEUE_CMT_RECOVERIES,
                                                    Constants.QUEUE_SFLEET_ADMIN,Constants.QUEUE_SFLEET_ACC_MGMT};
        
        List<Group> queues = [
            SELECT 
                Id,DeveloperName 
            FROM 
                Group 
            WHERE 
            Type = 'Queue' AND DeveloperName IN :queueNameSet
        ];

        if (!queues.isEmpty()){
            for(Group queue : queues ) {
                queuesByName.put(queue.DeveloperName, queue.Id);
            }
        }

        return queuesByName;
    }