1

Trying to make an Accounts List View with a button. Selected accounts go to my visual force page, where the user selects to Account Owner (via search field) they want those records/accounts to be changed to.

In short I'm trying to get the value from Account Owner field and pass it to the extension where I am attempting to override the save function from the standardsetcontroller to save both the accounts and contacts OwnerId.

Apex:

    public with sharing class quickEmail {

    ApexPages.StandardSetController setCon;

    public sObject ownThing {get;set;}


    public quickEmail(ApexPages.StandardSetController ctrl) {
    setCon = ctrl;

    }

    public PageReference doSomething() {
    //do something with the selected records

    /*Test - get contact id as well*/
    List<Contact> selectedContacts = new List<Contact>();
    List<Account> selectedAccounts = new List<Account>();
    /*******************************/

    for (Account acc : (Account[])setCon.getSelected() ) {
     System.debug('Account name = ' + acc.Name);
     System.debug('Here is Account: ' + acc);

     List<Account> alist = [SELECT Id, Name FROM Account WHERE Id = :acc.Id];
     System.debug('a' + alist);

     List<Contact> clist = [SELECT Id, Name FROM Contact WHERE Contact.AccountId IN :alist];
     System.debug('c' + clist);

     for(Contact c : clist) {
     c.OwnerId = Account.OwnerId; //*****Error Here*****
    selectedContacts.add(c);
    }
    System.debug('Here is selectedContacts: ' + selectedContacts);
    /****/
    }


    /*Test - get contact id as well*/
    ApexPages.StandardSetController myContacts = new ApexPages.StandardSetController(selectedContacts);
    myContacts.save();
    ApexPages.StandardSetController myAccounts = new ApexPages.StandardSetController(selectedAccounts);
    myAccounts.save();
    /*******************************/

    return null;
    }
    }

Here is VisualForce Page:

<apex:page standardController="Account" recordSetVar="accounts" extensions="quickEmail">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection >
                <!--I Want to use this value in the extension-->
                <apex:inputField value="{!Account.OwnerId}" />
            </apex:pageBlockSection>

            <apex:pageBlockTable value="{!selected}" var="account">
                <apex:column value="{!account.name}"/>
            </apex:pageBlockTable>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Do Something" action="{!doSomething}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

UPDATE:

Thank you @AdrianLarson very helpful answer/post.

To summarize: On my VF page I needed to get the {!Account.OwnerId} Inputfield variable into my apex controller extension. @AdrianLarson pointed out the the line to do this would be This snippets of code:

     final Account template;
     template = (Account)controller.getRecord();//<--same record as{!Account.OwnerId}
     Id ownerId = template.OwnerId; 
3
  • where exactly the issue is? Commented Dec 29, 2016 at 18:27
  • In your OWD, what is the settings of Contact? is that 'Controlled by Parent'? Commented Dec 29, 2016 at 18:34
  • In the OWD the Contact IS controlled by the parent. The main issue I have with this is I really just want the OwnerId from the VF page so I can set the appropriate Accounts and Contacts to that ownerId.
    – greenSloth
    Commented Dec 29, 2016 at 18:39

1 Answer 1

1

You don't even need Apex, this is the exact behavior the StandardSetController was designed for.

<apex:page standardController="Account" recordSetVar="accounts">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection >
                <!--I Want to use this value in the extension-->
                <apex:inputField value="{!Account.OwnerId}" />
            </apex:pageBlockSection>

            <apex:pageBlockTable value="{!selected}" var="account">
                <apex:column value="{!account.name}"/>
            </apex:pageBlockTable>
            <apex:pageBlockButtons location="bottom">


                <apex:commandButton value="Save" action="{!save}"/>
                <!-- THIS FUNCTIONALITY IS OUT OF THE BOX -->


                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

However, if you want to get this data into Apex, you can call getRecord() from the controller, which is essentially exactly what you are doing on the page when you bind to Account.OwnerId.

public with sharing class MyExtension
{
    final Account template;
    final PageReference redirect;
    public MyExtension(ApexPages.StandardSetController controller)
    {
        template = (Account)controller.getRecord();//<--same record as {!Account.OwnerId}
        redirect = controller.cancel();
    }
    public PageReference save()
    {
        Id ownerId = template.OwnerId;
        try
        {
            // do stuff
        }
        catch (DmlException d)
        {
            ApexPages.addMessages(d);
            return null;
        }
        return redirect;
    }
}
1
  • Perfect! Thank you @adrianLarson ! This line: "template = (Account)controller.getRecord();//<--same record as {!Account.OwnerId}" is exactly what I was looking for!
    – greenSloth
    Commented Dec 30, 2016 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .