How do I get selected list values in Salesforce? - salesforce

I am passing my Visualforce page the following code to have users select an employment website to post a position on:
<apex:page standardController="Position__c" extensions="PositionExtension">
<apex:form >
<apex:pageBlock title="Create New Position">
<apex:pageBlockButtons >
<apex:commandButton title="Save" value="Save" action="{!save}"/>
<apex:commandButton title="Edit" value="Edit" action="{!edit}"/>
<apex:commandButton title="Cancel" value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="3" title="Basic Information">
<apex:pageBlockSectionItem >
Position Title
<apex:inputField value="{!Position__c.name}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Record Type
<apex:inputField value="{!Position__c.recordtypeId}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Open Date
<apex:inputField value="{!Position__c.Open_Date__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Close Date
<apex:inputField value="{!Position__c.Close_Date__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Hire By
<apex:inputField value="{!Position__c.Hire_By__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Job Type
<apex:inputField value="{!Position__c.Type__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Location
<apex:inputField value="{!Position__c.Location__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Travel Required?
<apex:inputField value="{!Position__c.Travel_Required__c}" required="false"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Status
<apex:inputField value="{!Position__c.Status__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Functional Area
<apex:inputField value="{!Position__c.Functional_Area__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Job Level
<apex:inputField value="{!Position__c.Job_Level__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Job Description
<apex:inputField value="{!Position__c.Job_Description__c}" required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Responsibilities
<apex:inputField value="{!Position__c.Responsibilities__c}"
required="true"/>
</apex:pageBlockSectionItem>
<br></br>
<apex:pageBlockSectionItem >
Min Pay
<apex:inputField value="{!Position__c.Min_Pay__c}"
required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Max Pay
<apex:inputField value="{!Position__c.Max_Pay__c}"
required="true"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Education and Skills" columns="3">
<apex:pageBlockSectionItem >
Educational Requirements
<apex:inputField value="{!Position__c.Educational_Requirements__c}"
required="true"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Skills Required
<apex:inputField value="{!Position__c.Skills_Required__c}" required="false"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Apex
<apex:inputField value="{!Position__c.Apex__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
C#
<apex:inputField value="{!Position__c.C_sharp__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Java
<apex:inputField value="{!Position__c.Java__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
JavaScript
<apex:inputField value="{!Position__c.JavaScript__c}"/>
</apex:pageBlockSectionItem>
<apex:selectList value="{!selected}" multiselect="true">
<apex:selectOptions value="{!websites}"/>
</apex:selectList>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:Form>
</apex:page>
And this is the code from the controller, where I'm attempting to retrieve the selected values and sort through to determine if the website was selected:
public class PositionExtension {
public Position__c pos{get; set;}
public List<Employment_Website__c> employ{get;set;}
Public List<Job_Posting__c> joblist = new List<Job_Posting__c>();
Public Set<String> selected{get;set;}
public PositionExtension (ApexPages.StandardController controller){
pos = (Position__c)Controller.getRecord();
}
//List<Job_Posting__c> postings = [SELECT Id, Employment_Website__c FROM Job_Posting__c WHERE Position__c = :pos.Id];
public List<Employment_Website__c> website{
get{return [SELECT Id, Name, web_address__c FROM Employment_Website__c];}
set{website = value;}
}
public List<SelectOption> getWebsites(){
List<SelectOption> options = new List<SelectOption>();
for (Employment_Website__c web : website){
options.add(new SelectOption(web.Id, web.name));
}
return options;
}
public Set<String> updateSelected(){
return selected;
}
public PageReference save(){
//List<Job_Posting__c> postings = [SELECT Id, Employment_Website__c FROM Job_Posting__c WHERE Position__c = :pos.Id];
upsert pos;
for(Employment_Website__c web : website){
system.debug(selected);
if(selected.contains(web.Id)){
Job_Posting__c posting = new Job_Posting__c();
posting.Employment_Website__c = web.Id;
posting.Position__c = pos.Id;
joblist.add(posting);
}
}
upsert joblist;
PageReference acctPage = new ApexPages.StandardController(pos).view();
acctPage.setRedirect(true);
return acctPage;
}
}
Any suggestions on how to get the selected values back into the controller to create a job posting? Let me know if I should post more of my Visualforce code. Thanks!

You need to convert data type for selected.
Current :
Public Set selected{get;set;}
Updated
Public List selected{get;set;}

Related

Retaining selected list item value when editing visualforce page

Hoping someone can help with this, I have apex code which generates a select list on a Visualforce page, this works fine and the value is saved, the issue I have is that when going back to edit the page the selected value is defaulting to the first value in the list whereas it should default to the value selected.
VF page
<apex:page standardController="Service__c" extensions="ServiceParameterExtension,ServiceBroadbandController">
<style type="text/css">
.commentSize { width: 90%}
</style>
<apex:sectionheader title="{!$ObjectType.Service__c.label} Edit" subtitle="{!IF(ISNULL(Service__c.Name), 'New Service',Service__c.Name)}"
/>
<apex:pagemessages id="editPageMessage"></apex:pagemessages>
<apex:form rendered="{!contains(Service__c.Type__c, 'Change')}">
<apex:pageblock mode="edit" title="{!$ObjectType.Service__c.label} Edit">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!Save}" />
<apex:commandButton value="Cancel" action="{!Cancel}" />
</apex:pageBlockButtons>
<apex:outputPanel >
<apex:pageBlockSection title="Information" showheader="true" columns="2" id="InformationChange">
<apex:inputField value="{!Service__c.Product__c}" />
<apex:inputField value="{!Service__c.Status__c}" />
<apex:inputField value="{!Service__c.Type__c}" />
<apex:inputField value="{!Service__c.Associated_MSA__c}" />
<apex:selectList value="{!changetypeselected}" label="Change Type" size="1">
<apex:selectOptions value="{!items}"/>
</apex:selectList>
<apex:inputField value="{!Service__c.C_I_Number__c}" label="OMS Order No." />
<apex:outputField value="{!Service__c.RecordTypeId}"/>
<apex:inputField value="{!Service__c.Ordered_Circuit_ID__c}" label="Openreach Ref." />
<apex:inputField value="{!Service__c.Product_Variant__c}" />
<apex:inputField value="{!Service__c.New_Account__c}" />
<apex:inputField value="{!Service__c.Static_IP__c}" label="Static IP Address required?"/>
<apex:inputField value="{!Service__c.Static_IP_2__c}" label="Static IP Address" />
<apex:inputField value="{!Service__c.Cancellation_Reason__c}" />
<apex:inputField value="{!Service__c.L2S__c}" />
<apex:inputField value="{!Service__c.Openreach_Cablelink__c}" />
<apex:inputField value="{!Service__c.eir_NGQ__c}" />
<apex:inputField value="{!Service__c.OMS_VIM_Circuit_ID__c}" label="OMS Circuit Ref." />
<apex:inputField value="{!Service__c.Completion_Date__c}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Order Details" showHeader="true" columns="2" id="OrderDetailsChange">
<apex:inputField value="{!Service__c.Quantity__c}" />
<apex:inputField value="{!Service__c.Account_Number__c}" />
<apex:outputLabel value=""/>
<apex:inputField value="{!Service__c.Existing_IPVPN_Circuit_ID__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Comments" showheader="true" columns="1" id="CommentsChange">
<apex:inputField value="{!Service__c.Comments__c}" styleClass="commentSize"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Contracting Part Details" showHeader="true" columns="2" id="ContractingPartDetailsChange">
<apex:inputField value="{!Service__c.Site_Contact_Name__c}" required="true"/>
<apex:inputField value="{!Service__c.Welcome_Pack_Email_new__c}" />
<apex:inputField value="{!Service__c.Site_Contact_Phone_No_2__c}" />
<apex:inputField value="{!Service__c.Welcome_Pack_Email_2_new__c}" />
<apex:inputField value="{!Service__c.Site_Address__c}" style="width: 60%;"/>
<apex:inputField value="{!Service__c.Welcome_Pack_Email_3_new__c}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Admin Area" showheader="true" columns="2" id="AdminAreaChange">
<apex:outputLabel value=""/>
<apex:inputField value="{!Service__c.Approved__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="System Information" showHeader="true" columns="1" id="SystemInformationChange">
<apex:outputField value="{!Service__c.RecordTypeId}"/>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageblock>
</apex:form>
</apex:page>
Apex Controller
public with sharing class ServiceBroadbandController {
public String changetypeselected;
Service__c thisRecord;
public ServiceBroadbandController(ApexPages.standardController controller) {
thisRecord = (Service__c)controller.getRecord();
thisRecord.Static_IP__c = 'No';
}
public PageReference Save(){
if (validations()) {
thisRecord.Change_Type__c = changetypeselected;
Database.UpsertResult saveResult = Database.upsert(thisRecord, false);
if(saveResult.isSuccess()){
PageReference savePage = new PageReference('/' + thisRecord.id);
savePage.setRedirect(true);
return savePage;
}else{
for(Database.Error err : saveResult.getErrors()){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, err.getMessage()));
}
}
}
return null;
}
public List<SelectOption> getItems(){
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('','--None--'));
options.add(new SelectOption('Add_static_IP','Add Static IP'));
options.add(new SelectOption('Remove_static_IP','Remove Static IP'));
options.add(new SelectOption('Change_SLA','Change SLA'));
options.add(new SelectOption('Change_Bandwidth','Change Bandwidth'));
return options;
}
public String getchangetypeselected(){
return changetypeselected;
}
public void setchangetypeselected(String changetypeselected){
this.changetypeselected=changetypeselected;
}
}
which picklist, <apex:selectList value="{!changetypeselected}" label="Change Type" size="1">?
Try putting this in controller: changetypeselected = thisRecord.thisRecord.Change_Type__c;
It might throw something about "field was retrieved without querying", in that case you could put addFields as 1st line of constructor.
But I suspect your code is overcomplicated and you can cut a lot of apex by changing the reference in VF to mention the field explicitly: <apex:selectList value="{!Service__c.Change_Type__c}" label="Change Type" size="1">

Referance a static resource via aura:attribute

I have created a new component and I want to be able to pass an image from a static resource and a custom label via aura:attribute. This is what I tried and it does not work. How can I make the image/text to show?
<aura:attribute name="profileImage" type="string" default="Standard_Profile" />
<aura:attribute name="categoryName" type="string" default="Standard_Name" />
<img src="{!$Resource + !v.profileImage}" alt="profile pic"/>
<h3>{!$Label.'categoryName'}</h3>
I am very new to Salesforce.
Map the values from your custom label and static resource as default values in the attributes and use it in the code
<aura:component implements="flexipage:AvailableForAllPageTypes">
<aura:attribute name="profileImage" type="string" default="{!$Resource.profileImage}" />
<aura:attribute name="categoryName" type="string" default="{!$Label.c.categoryName}" />
<!-- Stand-alone static resources image file-->
<img src="{!v.profileImage}"/>
<!-- custom label -->
<div>
{!v.categoryName}
</div>
</aura:component>

HTML is showing in the aura component

<aura:component implements="force:appHostable,lightning:isUrlAddressable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" controller="AccountController" access="global" >
<aura:attribute type="campaign[]" name="acctList"/>
<aura:attribute name="mycolumns" type="List"/>
<aura:handler name="init" value="{!this}" action="{!c.fetchAcc}"/>
<lightning:datatable data="{! v.acctList }"
columns="{! v.mycolumns }"
keyField="id"
hideCheckboxColumn="true"/>
</aura:component>
Could you use
<aura:unescapedHtml value=""/>

Adding user defined types in SOLR schema

Iam using SOLR as my search query database.
My current requirement is to post this Document on SOLR database.
namespace SOLRApp
{
public class Table1Document
{
public Table1Document()
{
MobileNos = new List<int>();
EducationalDetails = new List<EducationalDetails>();
}
[SolrUniqueKey("ID")]
public string ID { get; set; }
[SolrField("FirstName")]
public string FirstName { get; set; }
[SolrField("LastName")]
public string LastName{ get; set; }
[SolrField("MobileNos")]
public List<int> MobileNos { get; set; }
[SolrField("EducationalDetails")]
public List<EducationalDetails> EducationalDetails { get; set; }
}
public class EducationalDetails
{
public EducationalDetails()
{
Details = new List<Details>();
}
public List<Details> Details { get; set; }
}
public class Details
{
[SolrField("IntitutionName")]
public string IntitutionName { get; set; }
[SolrField("EnrollDate")]
public DateTime EnrollDate { get; set; }
[SolrField("PassoutDate")]
public DateTime PassoutDate { get; set; }
[SolrField("InstituteRating")]
public int InstituteRating { get; set; }
}
}
And for that I have added this schema file with field as follows.
<field name="ID" type="int" indexed="true" stored="true"/>
<field name="FirstName" type="string" indexed="true" stored="true"/>
<field name="LastName" type="string" indexed="true" stored="true" />
<field name="MobileNos" type="string" indexed="true" stored="true" multiValued="true" />
<field name="EducationalDetails" type="EducationalDetails" indexed="true" stored="true" multiValued="true">
<field name="Details" type="string" indexed="true" stored="true" multiValued="true">
<field name="IntitutionName" type="string" indexed="true" stored="true"/>
<field name="EnrollDate" type="date" indexed="true" stored="true"/>
<field name="PassoutDate" type="date" indexed="true" stored="true"/>
<field name="InstituteRating" type="string" indexed="true" stored="true"/>
</field>
</field>
I would like to know how can we add user defined datatypes in SOLR. Like in my example, 'EducationalDetails' is a user defined datatype (class). Can any one tell me how to add that in SOLR.
Thank you in advance.
You can create your own types with the <fieldType /> tag, for example:
<fieldType name="cstring" class="solr.TextField" sortMissingLast="true" omitNorms="true">
<analyzer>
<tokenizer class="solr.KeywordTokenizerFactory" />
<filter class="solr.TrimFilterFactory" updateOffsets="true" />
<filter class="solr.PatternReplaceFilterFactory" pattern="\s+$" replacement="" />
<filter class="solr.ASCIIFoldingFilterFactory" />
</analyzer>
</fieldType>
However, as you can notice it's nothing like you want. The stuff you want (subfields, ...) is not possible with Solr. Solr is a document based store, not an "object" store or how you want to see it.
The best solution I can come up with is to create multiple "types" of documents. You scheme should look like:
<field name="ID" type="int" indexed="true" stored="true"/>
<field name="FirstName" type="string" indexed="true" stored="true"/>
<field name="LastName" type="string" indexed="true" stored="true" />
<field name="MobileNos" type="string" indexed="true" stored="true" multiValued="true" />
<!-- Education details -->
<field name="IntitutionName" type="string" indexed="true" stored="true"/>
<field name="EnrollDate" type="date" indexed="true" stored="true"/>
<field name="PassoutDate" type="date" indexed="true" stored="true"/>
<field name="InstituteRating" type="string" indexed="true" stored="true"/>
<field name="DocumentID" type="int" indexed="true" stored="true"/>
<field name="Type" type="string" indexed="true" stored="true"/>
If you add a Table1Document to Solr, you only provide the ID, FirstName, LastName and MobileNos fields (and additionally you can use the Type field and add "Table1Document"
If you add an EducationDetails object, you only provide the InstitutionName, EnrollDate, PassoutDate and InstituteRating field. In the DocumentID field you put a reference to the ID of the Table1Document they should belong to. In the Type field you can add "EducationDetails".
If you now want to retrieve your Table1Document you use a query like:
type:Table1Document AND id:1
To get the EducationDetails that belong to it you can use a query like:
type:EducationDetails AND DocumentID:1
Afterwards you can still construct an object with the structure you like.

Hibernate mapping and merging

I have 2 classes like this :
Message(id, title, content)
MessageEmployee(id, messageId, employeeId, readFlag)
and 2 tables like this :
MESSAGE(mess_id, mess_title, mess_content)
MESSAGE_EMPLOYEE(mess_empl_id, mess_id, empl_id, read_fg)
Mapping files :
<hibernate-mapping package="core">
<class name="Message" table="MESSAGE">
<!-- class id -->
<id name="id" type="int" column="MESS_ID" length="11">
<generator class="native"/>
</id>
<property name="content" type="string" column="MESS_CONTENT" />
<property name="title" type="string" column="MESS_TITLE" />
</class>
</hibernate-mapping>
<hibernate-mapping package="core">
<class name="MessageEmployee" table="MESSAGE_EMPLOYEE">
<!-- class id -->
<id name="id" type="int" column="MESS_EMPL_ID" length="11">
<generator class="native"/>
</id>
<!-- employee -->
<many-to-one name="employee" class="core.Employee"
column="EMPL_ID" cascade="save-update,merge" lazy="false" />
<!-- message -->
<many-to-one name="message" class="core.Message"
column="MESS_ID" cascade="save-update,merge" lazy="false" />
<property name="readFlag" type="character" column="READ_FG" />
</class>
</hibernate-mapping>
Here is my problem :
let's say I already have a message in database, and I want to create a messageEmployee and save it.
Code snippet :
Message sent = new Message(content, title);
Employee e = employeeDao.loadEmployeeWithId(Integer.valueOf(to));
messageDao.merge(sent)
MessageEmployee m = new MessageEmployee(sent, e, null);
m.setReadFlag('N');
messageEmployeeDao.mergeMessageEmploye(m);
When I merge(messageEmployee), it creates a new message and a new messageEmployee in database, but I don't want it to create a new message.
I'm quite sure my mapping is wrong since I am no expert, so what could I change to get the behaviour I want ?
It will create a new Message object because you are not providing id in your message object sent. When you execute messageDao.merge(sent), the sent object doesn't have and id to merge with. It is pure transient object. Try loading it from database like Employee.

Resources