Definition of SObject

Account myAcct = new Account();
myAcct.Name = 'The Tea Factory';
myAcct.Phone= '555-0129';
myAcct.AccountNumber = '356281';

Account SObject Decleration creates an empty memory which is Exactly the same as the Account Record.

Variable name myAcct.

myAcct.Name can make you the fill the exact object definitions. We can call it SObject Property


public class NewAccounts {
    public static void sObjectsInsert(){
        Account store = new Account();
        store.Name = 'The Tea Factory';
        store.AccountNumber = '356281';
        store.Phone = '555-0158';
        insert store;
    }

 public static void ListAccountInsert(){
        List<Account> Liststore = new List<Account>();
        // run loop and fill out the list then  Account store = new Account();
        insert Liststore;
    }
}

// invoke from Anonymous Window
NewAccounts.sObjectsInsert();
// this will call the newaccounts class and run the SObjectsInsert method

// Example Trailhead Study

public class AccountHandler {

    public static void insertAccount(Integer newAcc){
        List<Account> addAccounts=new List<Account>();
        Integer N=0;
        
        while (N<=newAcc){
            Account store = new Account();
            store.Name='Acme Inc N';
            store.AccountNumber='A000'+n;   
            addAccounts.add(store);
       N++;
        }
        
        insert addAccounts;
        
    }
}

Generic Sobject

List<Sobject> accounts = [SELECT Id, Name,Industry,Phone FROM Account];
List<Sobject> contacts = [SELECT Id, Name,Phone,Email FROM Contact];
List<Sobject> jenericList = New List<Sobject>();
jenericList.addAll(accounts);
jenericList.addAll(contacts);

for(Sobject jeneric:jenericList){
   // jeneric.getSObjectType()
    System.debug(jeneric.get('Id') + ' id record Details : ' 
                 + jeneric.get('Name') + ' - ' 
                 + jeneric.get('Phone'));
   String sobjectApiName = String.valueof(jeneric.getSObjectType());
     switch on sobjectApiName {
        when 'Account' {
           System.debug('Account Industry :' + jeneric.get('Industry')); 
        }
        when 'Contact' {
           System.debug('Contact Email : ' + jeneric.get('Email')); 
        }
    }
   
        
    /*    if(jeneric.getSObjectType()=='Account'){
        
    } else if (jeneric.getSObjectType()=='Contact'){
        
    }

*/
   
}

Sobject Inclass Exercies

Child to Parent SOQL SOBJECT

Parent to Child SOQL SOBJECT

This is published by Selçuk GÖKTAŞ