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
Ragnar Lothbrok 7Ragnar Lothbrok 7 

How to write a test class for this Apex controller class ?

Hi there,
Can anyone help me to write the test class or guide me how to write for this apex class :
public without sharing class PdrNewOpportunityController {

    private static final String VALIDATION_EXCEPTION = 'FIELD_CUSTOM_VALIDATION_EXCEPTION';

    @AuraEnabled

    public static Id saveOpportunity(Map<String, Object> fieldMap, List<Map<String, Object>> contactList){

        Opportunity opp;

        try{

            opp = createOpportunity(fieldMap);

            createContactRoles(contactList, opp.Id);

        } catch (Exception e){

            System.debug('Error Msg-- ' + e.getMessage() + 'Error Line No-- ' + e.getLineNumber());

            SC_ExceptionLogHandler.createExceptionLog(e,'PdrNewOpportunityController');

            throw new AuraHandledException(parseValidationError(e.getMessage()));

        }

        return opp.Id;

    }

    private static Opportunity createOpportunity(Map<String, Object> fieldMap) {

        Opportunity opp = new Opportunity();

        Map<String, Schema.SObjectField> oppFieldDescribeMap = Schema.SObjectType.Opportunity.fields.getMap();

        for (String fieldName : fieldMap.keySet()) {

            if (oppFieldDescribeMap.get(fieldName).getDescribe().getType() == Schema.DisplayType.DATE) {

                opp.put(fieldName, parseDate(fieldMap.get(fieldName)));

            } else {

                opp.put(fieldName, fieldMap.get(fieldName));

            }

        }

        insert opp;

        return opp;

    }

    private static void createContactRoles(List<Map<String, Object>> contactList, Id opportunityId) {

        List<OpportunityContactRole> contactRoles = new List<OpportunityContactRole>();

        for (Map<String, Object> contactRoleData : contactList) {

            String role = (String)contactRoleData.get('role');

            if (!String.isEmpty(role)) {

                contactRoles.add(new OpportunityContactRole(

                    OpportunityId = opportunityId,

                    Role = (String)contactRoleData.get('role'),

                    ContactId = extractContactIdFromUrl((String)contactRoleData.get('fullNameUrl'))

                ));

            }

        }

        if (!contactRoles.isEmpty()) { insert contactRoles; }

    }

    private static Id extractContactIdFromUrl(String fullNameUrl) {

        return fullNameUrl.substringAfter('contact/').substringBefore('/view');

    }

    private static Date parseDate(Object dateObj) {

        String dateStr = (String)dateObj;

        List<String> dateParts = dateStr.split('-');

        return Date.newInstance(

            Integer.valueOf(dateParts[0]),

            Integer.valueOf(dateParts[1]),

            Integer.valueOf(dateParts[2])

        );

    }

    private static String parseValidationError(String errorMsg) {

        String parsedMessage = errorMsg;

        if (!String.isEmpty(errorMsg) && errorMsg.containsIgnoreCase(VALIDATION_EXCEPTION)) {

            parsedMessage = errorMsg.substringAfter(':').substringAfter(',');

            // replace field API name with label, if an Opportunity field is mentioned

            if (parsedMessage.containsIgnoreCase('__c]')) {

                String fieldName = parsedMessage.substringAfterLast('[').substringBefore(']');

                Map<String, Schema.SObjectField> oppFieldDescribeMap = Schema.SObjectType.Opportunity.fields.getMap();

                if (oppFieldDescribeMap.containsKey(fieldName)) {

                    String fieldLabel = oppFieldDescribeMap.get(fieldName).getDescribe().getLabel();

                    parsedMessage = parsedMessage.substringBeforeLast('[') + fieldLabel;

                }

            }

        }

        return parsedMessage;

    }

}

Thanks.
PriyaPriya (Salesforce Developers) 

Hey Ragnar,

If you have attempted any test class code for the your apex class, then kindly provide, we will help you to fix to increase the code coverage.

Thanks, 

Priya Ranjan