function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jos VervoornJos Vervoorn 

Code effeciency


I have created 4 VF pages. 2 for case creation on comm portal (different types) and 2 for internal support agents. As we have come a long way we now also have 4 controller extensions.
  • case type A - Portal
  • case type B - portal
  • case type A - internal
  • case type B - internal
Now all these extensions have lot's of different conditions to retrieve options for creating a case. I know it's not ideal to keep these 4 extensions but given the situation, we have already made huge steps in efficiency and most of all .. eliminate JavaScript. 

However .. we would like to make it more efficient as the final method within these extensions all deal with the actual insert of the case record. We would like to create a separate class providing a single method to actually perform the insert.
//** Mapping of values for case record creation
            objCase.RecordTypeId          = RecType.id;
            objCase.AssetId                     = AssetID;
            objCase.BusinessHoursId     = objCase.Entitlement.BusinessHoursId;

            If (IsMainContactTypeValue == 'Partner'){ 
                objCase.CaseCreatedByPartnerUser__c = true;
            } else {
                objCase.CaseCreatedByPartnerUser__c = false;
            }


            try{
                insert objCase;
            } catch (Exception e){
                System.debug('** ** ** Controller || Creation of Service Request failed  ....');
            }
Now, in general, the mapping of these various case fields for every type of case record is the same. Some have fewer values .. could be explicitly NULL.

Now my question is .. would .. it be easy to create another controller providing a method to actually create the case and prevent repeating the mapping of values in each extension as when we need to add/remove a field .. we need to make sure it's done in all 4 classes. if possible please share the basic idea ...
Best Answer chosen by Jos Vervoorn
Jos VervoornJos Vervoorn
I have implemented a solution based on extending a custom controller.

Public class MyComp_XYZ extends MyComp_ABC

So this extension now contains all the 'shared' methods .... reducing code duplication.

Regards,
Jos/
 

All Answers

jigarshahjigarshah
Jas,

You could use the following approach.
  • Based on your explanation, I notice that there are 2 responsibilities that need to be fulfilled through this new utility class
    • Case Field Mapping
    • Case Record Creation 
  • In case the Case field mappings don't vary except for whether it is are created by a Partner or not, you could add a parameter to determine this. Additionally you should include an additional method to insert Case records which plainly inserts the provided records irrrepsective of their type.
  • The below code for your utility class can be invoked and reused from all 4 Visualforce Controller Extension Classes. You can populate the returned blank instance from the below getInstance() method with the respective field value.
public with sharing class CaseService{

	//Returns a single empty Case instance depending on whether it is created by a Partner or not
	public static Case getInstance(Boolean pIsCaseCreatedByPartner){
		return new Case(CaseCreatedByPartnerUser__c = pIsCaseCreatedByPartner);
	}

	//Inserts the provided Case instances and returns the success failure results
	public static List<Database.SaveResult> insert(List<Case> pCases){
			
		if(pCases.size() > 0){
			return Database.insert(pCases, false);
		}
		return null;
	}
}

Hope this helps.
jigarshahjigarshah
Jos, Is this issue resolved for you or are you still facing a challenege?
Jos VervoornJos Vervoorn
I have implemented a solution based on extending a custom controller.

Public class MyComp_XYZ extends MyComp_ABC

So this extension now contains all the 'shared' methods .... reducing code duplication.

Regards,
Jos/
 
This was selected as the best answer