So How’s The New Job Goin’

I have completed three weeks at the State of Vermont and while we wait for my background check to clear I will be studying Apex and Salesforce so that I can be useful to my teammates. We have to have the check cleared before I can get any specific training on the legacy tools that I will be supporting.

Not gonna lie, there are some parts of Trailhead (basically Technet but for Salesforce) that make studying for Microsoft Server Admin certificates look exciting. And we all know what r/sysadmin has to say about Salesforce. Nevertheless, we wade in with all the optimism in the world.

Learning Apex

Honestly, this is about as soul-shattering as discovering that I liked (a proper, scratch-made) ranch dressing (on pizza, even). I think I might actually like Java. Of course, I discover this right as fractureiser is the latest in a long list of reasons for why it’s difficult to do anything in Java to the level of security that I want to reach.

Liveblogging Trailhead

So they do start you with your typical primitives and introductory methods. I found that my recent experience with TypeScript and my ancient experience with C/++ was really helpful for picking up the syntax quickly.

Create an Apex class with a method that returns a list of strings
Create an Apex class with a method that returns a list of formatted strings. The length of the list is determined by an integer parameter. You can also use an array if you prefer, but these instructions assume you’re using a list.

The Apex class must be called StringArrayTest and be in the public scope
The Apex class must have a public static method called generateStringArray
The generateStringArray method must return a list of strings
    The method must accept an incoming Integer as a parameter, which will be used to determine the number of returned strings
    The method must return a list of strings. Each element in the list must have the format Test n, where n is the index of the current string in the list. For example, if the input is 3, then the output should be ['Test 0', 'Test 1', 'Test 2']. Remember that in Apex, the index position of the first element in a list is always 0.
publicclassStringArrayTest {

    publicstatic List<String> generateStringArray( Integer num ) {
        List<String> strings = newList<String>();
        for (Integer i=0;i<num;i++) {
            strings.add('Test '+i);
        }
       return strings;
    }

}

I was tripped up here for only a moment but key to note – you have to type the list element data (a list of strings), and you have to type the iterator when you declare it in the for loop.

Resources:

Manipulating records with DML

public with sharing classAccountHandler {
    publicstatic Account insertNewAccount(String accountName) {
            Accountacct=newAccount(Name=accountName);

            try {
                 insert acct;
                } catch (DmlException e) {
                System.debug('A DML exception has occurred: ' + e.getMessage());
                returnnull;
            }
            return acct;
    }
}

This has a persistent and annoying error that shows in the IDE that does not apparently affect the code once it is deployed. Type arguments provided for a non-parameterized type: String (2:35)ApexClass will get marked up in the IDE, but the way I actually got this to deploy was to move the return acct; statement out of the try block and place it after the logic. I suspect that at compile time, that it’s possible for an account insert to fail without specifically throwing a DML Exception. This would mean that it is unlikely but possible for this function to never end. By placing the return statement outside of this logic, there will always be a valid exit point from this method.

No solution yet for the paramaterized typing error that appears. But I did change the code to this sample which I like better:

publicclassAccountHandler {
    publicstatic Account insertNewAccount(String accountName) {
            Accountacct=newAccount();
            acct.Name = accountName;
            try {
                 insert acct;
                } catch (DmlException e) {
                System.debug('A DML exception has occurred: ' + e.getMessage());
                returnnull;
            }
            IDacctID= acct.ID;
            return [SELECT Id,Name FROM Account WHEREId=:acct.ID ];
    }
}

Resources: https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000JfzE9SAJ

Writing SOQL in Apex

Genuinely, I’m not sure how I feel about mixing query and general purpose programming languages like this. I can see how it makes things convenient in some ways, but this also kinda feels a little risqué.

public with sharing classContactSearch {

    publicstatic List<Contact> searchForContacts( String lastName, String mailingPostalCode){
        List<Contact> foundContacts = newList<Contact>();
        try {
             foundContacts = [SELECT Id, Name FROM Contact WHERELastName= :lastName ANDMailingPostalCode= :mailingPostalCode];
            } catch (DmlException e) {
            System.debug('A DML exception has occurred: ' + e.getMessage());
            returnnull;
        }
        return foundContacts;

    }
}

Learning to use Triggers

I spent too long hung up on how to evaluate checkboxes. This may even have been covered in a previous unit, but unlike PHP/Javascript where evaluating checkboxes is a tedious chore, we’re directly calling the database here and checkbox values are apparently stored as booleans. Simply evaluate if or if not true and you can continue on.

trigger AccountAddressTrigger on Account (before insert, before update) {
    for(Account a : Trigger.new) {
        if(a.Match_Billing_Address__c == true)
        a.ShippingPostalCode = a.BillingPostalCode;
    }
}

Handling Events in Apex / Lightning Web Components

  • note the provided document has an error in …/data/data.js
  • api version is too low, exposed is set to false and it should be true. select a higher api version like 52 and set to true.

Thank you for reading. Please let me know if there are any clarifications I can make or further questions I can answer either down in the comments, on LinkedIn, or hit me up on Mastodon.

Related Posts