Custom field is not found

1.4k views Asked by At

I'm attempting to self learn Salesforce and want to add a custom field to an Account.

To achieve this I've done the following:

I've added the Total custom field to Account:

enter image description here

Here is the trigger:

trigger AccountTotalTrigger on Account (before insert) {

    List<String> accountNames = new List<String>{};
  
   //Loop through all records in the Trigger.new collection
   for(Account a: Trigger.new){
      //Concatenate the Name and billingState into the Description field
      a.Description = a.Name + ':' + a.BillingState;
   }
    
}

and accompanying test:

   @IsTest
    private with sharing class AccountsTest {
        @IsTest
        static void testAccountTriggerViaDML()
        {
                // This example is simple, illustrates how to invoke the trigger code via DML (required), 
                //   but can become complex and detract from TDD and more granularly testing of the Accounts class
                Account testAccount = new Account( Name = 'Test Account' , Total = 100 );
                insert testAccount;
                testAccount = [select Id, Name from Account where Id = :testAccount.Id];
                System.assertEquals(testAccount.Name, 'Test Account');  
        }
    
    }

But there is a problem on the Problems tab:

enter image description here the message is: Field does not exist: Total on Account

Have I not setup the Account field correctly?

Should the test not be failing? It appears to pass:

ustom

2

There are 2 answers

2
eyescream On BEST ANSWER

The API name of the field (database column) is Total__c. "Total" is just the visible label. Your Salesforce might have French/German/Spanish speakers and they'd prefer to see it translated but the code should still run OK.

Most custom things you do to SF database end with this suffix. Custom tables ("objects") like Application__c for example, custom fields like your Total__c. There are more suffixes for geolocation fields, big objects, external data... but that's battle for another day ;)

0
Bonish Koirala On

Whenever you try to use custom field in apex, be sure to append the __c at the end. In your case, you should be using Total__c instead of Total in your Test and Trigger wherever used.