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
Sumanta SatpathySumanta Satpathy 

getting the below exception:Update_SalesRep: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Update_SalesRep: line 18, column 1

/*Create “Sales Rep”(Sales_Rep__c) field with data type (Text) on the Account Object. 
When we create the Account record, the Account Owner will be automatically added to Sales Rep field.
When we update the Account owner of the record, then also the Sales Rep will be automatically updated.
*/
trigger Update_SalesRep on Account (before insert,before update) {
    Set<id> accountOwnerId= new Set<ID>();
    for(Account a: trigger.new){
        accountOwnerId.add(a.OwnerId);
    }
    Map<Id,User> userMap= new Map<Id,User>();
    
    List<User>userList=[Select id,name from User where id IN:accountOwnerId];
    for(User u:userList){
        userMap.put(u.Id,u);
    }
    for(Account ac: trigger.new){
        user u=userMap.get(ac.Id);
        ac.Sales_Rep__c=u.Name;
    }
    

}

 
Raj VakatiRaj Vakati
Try this code

this line is wrong in your code

user u=userMap.get(ac.OwnerId);
 
trigger Update_SalesRep on Account (before insert,before update) {
    Set<id> accountOwnerId= new Set<ID>();
    for(Account a: trigger.new){
        System.debug('OwnerId'+a.OwnerId);
        accountOwnerId.add(a.OwnerId);
    }
    Map<Id,User> userMap= new Map<Id,User>();
    
    List<User> userList=[Select id,name from User where id IN:accountOwnerId];
    for(User u:userList){
        userMap.put(u.Id,u);
    }
    for(Account ac: trigger.new){
        user u=userMap.get(ac.OwnerId);
        ac.Sales_Rep__c=u.Name;
    }
    
    
}