-1

We have a Visualforce page that is creating a New Case using a limited number of fields for the user to complete before submitting the Case.

This page can only be accessed after the user logs in to our Community, so they are a logged in Community user.

We need a picklist in the apex:form that will show the User a list of the Assets on their Account.

I am lost on how to get that list onto my existing form. I have the rest of the fields I need working just fine.

It seems I will need to find out who my User is and get their Account or User id and use that to get the list of their Assets and then output that list into an apex:selectList possibly and then of course insert the chosen Asset back into the Users New Case.

Here is the updated VF page minus the CSS after Answers

<apex:page standardController="Case" extensions="myWeb2CaseExtension" sidebar="false" showHeader="false" standardStylesheets="false"
           applyHtmlTag="false" applyBodyTag="false" docType="html-5.0">
<html>
<head>
    <meta charset="UTF-8"/>
<title>XYZ</title>  
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Raleway" />    

</head>

<body>

<div class="container">
  <header>
    <apex:image url="{!$Resource.Logo}" alt="Logo" width="200" height="67" id="logo" />
<ul>
  <li><a href="{!$Page.MyCases}">My Cases</a></li>
  <li><a href="#home">My Assets</a></li>
  <li><a href="#news">My Account</a></li>
  <li><a href="#contact">Knowledge</a></li>
  <li><a href="#about">Discussions</a></li>
</ul>
  </header>
  <div class="sidebar1">
  <nav>
    <ul>
      <li><a href="#">Link one</a></li>
      <li><a href="#">Link two</a></li>
    </ul>
    </nav>

    <aside>
      <p> Aliquam erat eu id est. Integer ac vestibulum metus. Integer ac vestibulum metus. Integer ac vestibulum metus. 
          Aliquam erat eu id est.Integer ac vestibulum metus. Aliquam erat eu id est.  
          Integer ac vestibulum metus. Aliquam erat eu id est.
        </p>
</aside>
  <!-- end .sidebar1 --></div>
  <article class="content">
<section>

<h2>Create a New Case</h2>

<h3>Please enter the case information</h3>

<apex:form >

<apex:messages id="error" styleClass="errorMsg" layout="table" style="margin-top:1em;"/>

<table>
            <tr>
                <td class="tdtop"><span class="formLabels">Asset</span></td>
                <td>    
                        <apex:selectList value="{!Case.Asset.Name}" size="7" >
                        <apex:selectOptions value="{!assetOptions}" />
                        </apex:selectList>
                </td>
            </tr>
            <tr>
                <td><span class="formLabels">Subject</span></td>
                <td><apex:inputField styleClass="subjectSize" value="{!Case.Subject}"/></td>
            </tr>
            <tr>
                <td><span class="formLabels">Priority</span></td>
                <td><apex:inputField value="{!Case.Priority}"/></td>
            </tr>    
            <tr>
                <td class="tdtop"><span class="formLabels">Description</span></td>
                <td><apex:inputField styleClass="descriptionSize" required="true" value="{!Case.Description}"/></td>
            </tr>
            <tr>
                <td><span class="formLabels">System Outage</span></td>
                <td><apex:inputField value="{!Case.Outage__c}"/></td>
            </tr>
            <tr>
                <td><span class="formLabels">Data Loss</span></td>
                <td><apex:inputField value="{!Case.Data_Loss__c}"/></td>
            </tr>
            <tr>
                <td></td>
                <td><apex:commandButton action="{!saveCase}" value="Send Your Case To Support"/></td>
            </tr>
        </table>

</apex:form>      


</section>
    <section> </section>
    <!-- end .content --></article>
  <footer>
    <p>Support Community</p>
    <ul>
      <li><a href="#">Privacy Policy</a></li>
      <li><a href="#">Terms of use</a></li>
      <li>Copyright © 2015 XYZ, Inc., All Rights Reserved</li>
    </ul>
  </footer>
  <!-- end .container -->
    </div>
    </body>
</html>
</apex:page>

Here is My current controller extension

     public class myWeb2CaseExtension {
    public Case webcase {get;set;}
    public SelectOption[] assetOptions {get;set;} 

    public myWeb2CaseExtension(ApexPages.StandardController stdController) {

        webcase = (Case) stdController.getRecord();

        User[] users = [SELECT ContactId FROM User WHERE Id = :UserInfo.getUserId()];
        User usr = users[0];

        Contact[] contacts = [SELECT Id, AccountId FROM Contact WHERE Id = :usr.ContactId];

        if (contacts.isEmpty()) {
            // handle when contacts is empty
        } else {
            Contact cont = contacts[0];
            Asset[] assets = [SELECT Name, ContactId, AccountId FROM Asset WHERE AccountId = :cont.AccountId];
            assetOptions = new SelectOption[] {};
            //if you want people to be able to choose no asset do this
            assetOptions.add(new SelectOption('None', 'None'));

            for (Asset asst: assets) {
                assetOptions.add(new SelectOption(asst.Id, asst.Name));                
            } 
        }
    }
    public PageReference saveCase() {
        try {
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.useDefaultRule = true;
            webcase.setOptions(dmlOpts);
            webcase.Origin = 'Web';
            insert(webcase);
        } catch (System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        PageReference p = Page.MyCases;
        p.setRedirect(true);
        return p;
    }
 }
14

Here is a rough sequence of queries you'd need to do this:

User[] users = [SELECT ContactId,IsPortalEnabled FROM User WHERE Id = :UserInfo.getUserId()];
User usr = users[0];

Contact[] contacts = [SELECT Id, AccountId FROM Contact WHERE Id = :usr.ContactId];
Contact cont = contacts[0];

Asset[] assets = [SELECT Name, ContactId, AccountId, Description, Name, Quantity, Price FROM Asset WHERE AccountId = :cont.AccountId];

Note, I see there are both ContactId and AccountId fields on Asset - make sure you aren't supposed to get the assets directly from contact.

From here, you need to build a SelectOptions list. You can use something like this:

//Declare this at the top of your controller with a getter 
SelectOption[] assetOptions {get;set;}

//after you have your Asset list, do this
assetOptions = new SelectOption[]{};
//if you want people to be able to choose no asset do this
assetOptions.add(new SelectOption('', ''));

for (Asset asst : assets) {
    assetOptions.add(asst.Id, asst.Name);
}

Finally, your select options on a page looks like:

<apex:selectList value="{!case.Your_Case_Asset_Field__c}" size="1" >
    <apex:selectOptions value="{!assetOptions}" />
</apex:selectList>

Note There is no error check code in this example. Make sure you add that.

  • 1
    Yes sorry, that was a bug - fixed. – Caspar Harmer Apr 15 '15 at 22:42
  • 1
    Hmm sorry another bug - make it Id instead of ContactId – Caspar Harmer Apr 15 '15 at 23:22
  • 1
    Yes, it's getting an empty list. There is no users[0]. UserInfo.getUserId() is not likely to return anything given that you are running as Anonymous. Login, get your user id and use that - ie, id='youruserid' -Oh and yes, fixed the other bug. – Caspar Harmer Apr 16 '15 at 3:40
  • 1
    Added putting this from Assets to SelectOptions – Caspar Harmer Apr 16 '15 at 18:23
  • 1
    Put it after this line: webcase = (Case)stdController.getRecord(); - ie, in the constructor is fine. It must be in a method and the constructor is good enough for now. – Caspar Harmer Apr 16 '15 at 19:00
2

It seems I will need to find out who my User is and get their Account or User id and use that to get the list of their Assets and then output that list into an apex:selectList possibly and then of course insert the chosen Asset back into the Users New Case.

Yes. You've broken down the problem, and those would be the steps to take. Have you tried searching these ?

1. Finding the logged in user

Have a look at the UserInfo class, there's a static method that will return the UserId of the logged in user (based on the execution context).

2. Get their Account

Community users have the user.ContactId populated, through that you can find the AccountId they belong to.

3. Etc

From thereon you were pretty much correct, and I'm going to assume you know how to deal with them or will be able to find sample code by google/documentation yourself :-).

  • Do I need to find the logged in user if I can just find their Account using user.ContactId as u mentioned? Does all of this need to be in the Controller extension and I can use the info via a variable in the Apex code on my VF page? – Jason Apr 15 '15 at 18:59
  • I am stuck. I need help getting to the point of having the list of Assets for the Users Account and I think I can handle it from there. I have not been able to write or find the code I need beyond a line that I was provided by someone else User MyUser = [select id from user where id = :UserInfo.getUserId()]; And I do not know if this is even what I need or what I need to use from it to get the info I am looking for. I am just lost and I realize what I need just not how to do it. – Jason Apr 15 '15 at 20:03
  • I am getting closer. This is what I have so far: User MyUser = [Select Contact.AccountId From User Where Id = :UserInfo.getUserId()]; – Jason Apr 15 '15 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.