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
Parteek Goyal 3Parteek Goyal 3 

I want to add change owner button on account list view page

HI All
I want to add change owner button on account list view page but shows this error.
Null reference exception
public class AccountController1 
{
    public Account accounts{get;set;}
    

    private ApexPages.StandardSetController standardController;
    public AccountController1(ApexPages.StandardController controller) {

    }

    public AccountController1(ApexPages.StandardSetController standardController)
    {
        this.standardController = standardController;
        List<Account> selectedAccounts = (List<Account>) standardController.getSelected();
        for(Account selectedAccount : selectedAccounts)
        {
            //if(selectedAccount.ownerid== null) 
                selectedAccount.ownerid = selectedAccount.ownerid;
            //else if(selectedAccount.ownerid == 'Low')
            System.debug('selectownerid>>>'+selectedAccount.ownerid);
           //  System.debug('selectownerid>>>'+selectedAccount.ownerid);
            //    selectedAccount.ownerid = 'Medium';
           /// else if(selectedAccount.ownerid == 'Medium')
            //    selectedAccount.ownerid = 'High';
        }       
    }

    
    
    public PageReference updateAccount()
    {       
        //update accounts;
        return standardController.save();
        // Call StandardSetController 'save' method to update (optional, you can use your own DML)
        //return null;   
    }
}


vd page

<apex:page standardController="Account" extensions="AccountController1">
    <apex:form >
        <apex:pageblock >
            <apex:pageblockbuttons >
                <apex:commandButton action="{!updateAccount}" value="Save"/>
            </apex:pageblockbuttons>
            <apex:pageblockSection >
                <apex:inputField value="{!accounts.OwnerId}"/>
            </apex:pageblockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>
please help me
 
Mudasir WaniMudasir Wani
Hello Dear,

Use the following code.
 
public class AccountController1 
{
    public Account accounts{get;set;}
    
    public List<Account> selectedAccountList = new List<Account>();
    private ApexPages.StandardSetController standardController;
    public AccountController1(ApexPages.StandardController controller) {

    }

    public AccountController1(ApexPages.StandardSetController standardController)
    {
        this.standardController = standardController;
        List<Account> selectedAccounts = (List<Account>) standardController.getSelected();
        for(Account selectedAccount : selectedAccounts)
        {
            //if(selectedAccount.ownerid== null) 
            selectedAccount.ownerid = selectedAccount.ownerid;
            //else if(selectedAccount.ownerid == 'Low')
            System.debug('selectownerid>>>'+selectedAccount.ownerid);
           //  System.debug('selectownerid>>>'+selectedAccount.ownerid);
            //    selectedAccount.ownerid = 'Medium';
           /// else if(selectedAccount.ownerid == 'Medium')
            //    selectedAccount.ownerid = 'High';
            selectedAccountList.add(selectedAccount);
        }       
    }

    
    
    public PageReference updateAccount()
    {       
        //update accounts;
        //standardController.save();
        update selectedAccountList;
        return null;
        // Call StandardSetController 'save' method to update (optional, you can use your own DML)
        //return null;   
    }
}

Mark It as solution if this solves your problem
Parteek Goyal 3Parteek Goyal 3
hi mudasir,

it gives a error

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.OwnerId
Class.AccountController1.<init>: line 18, column 1     
Mudasir WaniMudasir Wani
Hi Prateek,

We are using SObject so this error is not supposed to occur.

Can you paste your SOQL if you are using them in your actual code

I have run the code in my developer org and it is working fine.

Apex Class
public class AccountController1 
{
    public Account accounts{get;set;}
    
    public List<Account> selectedAccountList = new List<Account>();
    private ApexPages.StandardSetController standardController;
    public AccountController1(ApexPages.StandardController controller) {

    }

    public AccountController1(ApexPages.StandardSetController standardController)
    {
        this.standardController = standardController;
        List<Account> selectedAccounts = (List<Account>) standardController.getSelected();
        for(Account selectedAccount : selectedAccounts)
        {
            //if(selectedAccount.ownerid== null) 
            selectedAccount.ownerid = selectedAccount.ownerid;
            //else if(selectedAccount.ownerid == 'Low')
            System.debug('selectownerid>>>'+selectedAccount.ownerid);
           //  System.debug('selectownerid>>>'+selectedAccount.ownerid);
            //    selectedAccount.ownerid = 'Medium';
           /// else if(selectedAccount.ownerid == 'Medium')
            //    selectedAccount.ownerid = 'High';
            selectedAccountList.add(selectedAccount);
        }       
    }

    
    
    public PageReference updateAccount()
    {       
        //update accounts;
        //standardController.save();
        update selectedAccountList;
        return null;
        // Call StandardSetController 'save' method to update (optional, you can use your own DML)
        //return null;   
    }
}
Visualforce Page
<apex:page standardController="Account" extensions="AccountController1">
    <apex:form >
        <apex:pageblock >
            <apex:pageblockbuttons >
                <apex:commandButton action="{!updateAccount}" value="Save"/>
            </apex:pageblockbuttons>
            <apex:pageblockSection >
                <apex:inputField value="{!accounts.OwnerId}"/>
            </apex:pageblockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Try it in developer org and see if you have any issue.

If you are using same code in some other org and you are getting some error then we need to look into your profile.
 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Parteek Goyal 3,

Please try below controller.
public class AccountController1 
{
    public Account accounts{get;set;}
    
    public List<Account> selectedAccountList = new List<Account>();
    private ApexPages.StandardSetController standardController;
    public AccountController1(ApexPages.StandardController controller) {

    }

    public AccountController1(ApexPages.StandardSetController standardController)
    {
		List<String> fieldList = new List<String>();
		fieldList.add('Account.OwnerId');
		standardController.addFields();
        this.standardController = standardController;
        List<Account> selectedAccounts = (List<Account>) standardController.getSelected();
        for(Account selectedAccount : selectedAccounts)
        {
            //if(selectedAccount.ownerid== null) 
            selectedAccount.ownerid = selectedAccount.ownerid;
            //else if(selectedAccount.ownerid == 'Low')
            System.debug('selectownerid>>>'+selectedAccount.ownerid);
           //  System.debug('selectownerid>>>'+selectedAccount.ownerid);
            //    selectedAccount.ownerid = 'Medium';
           /// else if(selectedAccount.ownerid == 'Medium')
            //    selectedAccount.ownerid = 'High';
            selectedAccountList.add(selectedAccount);
        }       
    }

    
    
    public PageReference updateAccount()
    {       
        //update accounts;
        //standardController.save();
        update selectedAccountList;
        return null;
        // Call StandardSetController 'save' method to update (optional, you can use your own DML)
        //return null;   
    }
}

Let us know if it helps.
 
Parteek Goyal 3Parteek Goyal 3
its not working
show error in this

standardController.addFields();
 
Parteek Goyal 3Parteek Goyal 3
Hi mudasir,
 
public class AccountOwnerUpdate{
    public Account accounts{get;set;}
    public List<Account> selectedAccountList = new List<Account>();
    private ApexPages.StandardSetController standardController;
    public AccountOwnerUpdate(ApexPages.StandardController controller) {
    }
    public AccountOwnerUpdate(ApexPages.StandardSetController standardController)
    {
        this.standardController = standardController;
        List<Account> selectedAccounts = (List<Account>) standardController.getSelected();
        system.debug('ownerid>>>'+selectedAccounts);
        for(Account selectedAccount : [SELECT id,name,ownerid from Account where id=:selectedAccounts])
        {
            selectedAccount.ownerid = selectedAccount.ownerid;
            selectedAccountList.add(selectedAccount);
        }       
    }
    public PageReference updateAccount()
    {       
        update selectedAccountList;
        return null;    
    }
}
please check this code again

 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Prateek,

Sorry My bad, Please try below code. Missed to pass list as parameter.
public class AccountController1 
{
    public Account accounts{get;set;}
    
    public List<Account> selectedAccountList = new List<Account>();
    private ApexPages.StandardSetController standardController;
    public AccountController1(ApexPages.StandardController controller) {

    }

    public AccountController1(ApexPages.StandardSetController standardController)
    {
		List<String> fieldList = new List<String>();
		fieldList.add('Account.OwnerId');
		standardController.addFields(fieldList);
        this.standardController = standardController;
        List<Account> selectedAccounts = (List<Account>) standardController.getSelected();
        for(Account selectedAccount : selectedAccounts)
        {
            //if(selectedAccount.ownerid== null) 
            selectedAccount.ownerid = selectedAccount.ownerid;
            //else if(selectedAccount.ownerid == 'Low')
            System.debug('selectownerid>>>'+selectedAccount.ownerid);
           //  System.debug('selectownerid>>>'+selectedAccount.ownerid);
            //    selectedAccount.ownerid = 'Medium';
           /// else if(selectedAccount.ownerid == 'Medium')
            //    selectedAccount.ownerid = 'High';
            selectedAccountList.add(selectedAccount);
        }       
    }

    
    
    public PageReference updateAccount()
    {       
        //update accounts;
        //standardController.save();
        update selectedAccountList;
        return null;
        // Call StandardSetController 'save' method to update (optional, you can use your own DML)
        //return null;   
    }
}

Let us know if it works.
AthiAthi
Does this work? I m dealing with same scenario.. Any input will be helpful