How can I troubleshoot and resolve the issue of “unexpected error” in the apex code editor?

382    Asked by DominicPoole in Salesforce , Asked on Apr 16, 2024

 I am currently working on a Salesforce apex class which includes complex logic with multiple conditional statements and data manipulation operations. However, when I was going through to save the class I encountered a scenario where an issue message occurred which was stating that “unexpected Token” the apex code editor. How can I troubleshoot and resolve this particular issue? 

Answered by Crowny Hasegawa

 In the context of Salesforce, here are the steps given of how you can troubleshoot and resolve this particular issue:-

Review the code segment

First, try to identify the specific line where the error or “unexpected token” is reported. You should look for any syntax errors, misspelled keywords, etc.

Checking for typos or incorrect syntax

You should also look for reviewing the code around the reported error location.

Verify the keyword and symbols

You should try to ensure that you are using valid apex keywords, symbols, and operators according to your Salesforce syntax rules.

Examine conditional statements

If the error is occurring in the conditional statements then you should make sure that the conditions should be properly formatted and closed with appropriate parentheses and curly braces.

Here is the combined example given with the Salesforce apex trigger, utility class, and test class in one coding snippet:-

// MyUtilityClass.apxc
Public class MyUtilityClass {
    // Method to format a date into a specified format
    Public static String formatDate(Date inputDate, String formatString) {
        Return inputDate.format(formatString);
    }
    // Method to serialize an object to JSON format
    Public static String serializeToJson(Object obj) {
        Return JSON.serialize(obj);
    }
}
// OpportunityUpdateTrigger.trigger
Trigger OpportunityUpdateTrigger on Opportunity (after insert, after update) {
    For (Opportunity opp : Trigger.new) {
        If (opp.StageName == ‘Closed Won’) {
            Opp.Amount = opp.Amount * 1.1;
        } else if (opp.StageName == ‘Closed Lost’) {
            Opp.Amount = opp.Amount * 0.9;
        }
    }
}
// OpportunityUpdateTriggerTest.cls
@isTest
Public class OpportunityUpdateTriggerTest {
    @isTest static void testOpportunityUpdateTrigger() {
        // Create test Opportunity record
        Opportunity testOpp = new Opportunity(Name=’Test Opportunity’, StageName=’Closed Won’, Amount=1000);
        Insert testOpp;
        // Verify trigger logic
        testOpp = [SELECT Id, Amount FROM Opportunity WHERE Id = :testOpp.Id];
        System.assertEquals(1100, testOpp.Amount, ‘Amount should be updated for Closed Won Opportunity’);
        // Create JSON data for testing utility class
        Map testData = new Map{
            ‘key1’ => ‘value1’,
            ‘key2’ => 123,
            ‘key3’ => true
        };
        String jsonData = MyUtilityClass.serializeToJson(testData);
        System.assertNotEquals(‘’, jsonData, ‘JSON serialization should not be empty’);
    }
}

You should ensure to deploy this above coding to your Salesforce org appropriately. The test class would ensure that the trigger functions correctly and that the utility class’ method works as expected.



Your Answer

Interviews

Parent Categories