• Oleg Nikitchuk
  • NEWBIE
  • 25 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 6
    Replies
Hi all, 

I am writing a class with several methods, I have completed some while struggling with others.

1) One method should return Database.SaveResultId with input parameters(dealId, Map<String, Object> fieldNameToValue. It should update deal with values in a map. Can't figure this out, maybe someone can share a pattern.

2) Second method is void, it has(Id carId) as input parameter. The logic behind it: Car got broken. Update all open deals for this car with Status Lost. Car's IsAvailableForRent set false.

Please, advise. 
Hi all,

I have a class CarRentServices, in this class there are 2 methods:
1) Method is pretty simple, it returns picklist values based on user choise, standard functionality for combobox.
2) sendEndDate() method should receive a list of records and set End_Date__c to today. I tried to apply some logic but I don't think it is right.

Here's my code:
public with sharing class CarRentServices {
public List<Deal__c> setEndDate(){ return [SELECT Id, End_Date__c FROM Deal__c WHERE End_Date__c=: System.today()];
}
@AuraEnabled
public static List<Deal__c> filterFinished(String status){ return [SELECT Name,Status__c,Sales_Manager_Email__c,Start_Date__c,End_Date__c FROM Deal__c WHERE Status__c=:status];
}
}

Please, advise!
Hey everyone,

Need your help here! I am trying to make a formula that displays photo of car type(i have 3 car types: sedan, minivan, hatchback) if Type__c selected and isValidForRent is true. In other case it should sshow default text instead, smth like "no car available".

So far, I came up with this: IF(
    AND(
        NOT(ISBLANK(Type__c)),
        isValidForRent = true
    ),
    IMAGE(image_url, alternate_text),
    "No car available"
)

but this solution doesn't work. 

Please help me!
Hi everyone,

I need to create a validation rule: Deal__c.Status__c record can be created only with Status__c = Open.
Also, I need to make a formula: formula should display small photo of car type if Type__c selected and isValidForRent is true. In other case show default text instead - no car available.

I really struggle with this. The way I thought it should be made with valid rule is to use ISNEW() and with formula, I tried: 
IF(
    AND(
        NOT(ISBLANK(Type__c)),
        isValidForRent = true
    ),
    IMAGE(image_url, alternate_text),
    "No car available"
)
but it didn't work

Please, help!
Hello community,

I am trying to work with pagination. Facing some problems. What I have right now:
paginationAccount.html:
<template>
<lightning-card title="Pagination Test" icon-name="custom:custom63">
<table class="slds-table slds-table_cell-buffer slds-table_bordered">
<thead>
<tr>
<th>
<div>Id</div>
</th>
<th>
<div>Name</div>
</th>
<th>
<div>Phone</div>
</th>
</tr>
</thead>
<tbody>
<template for:each={accounts} for:item="account">
<tr key={account.Id}>
<td key={account.Id}>
{account.Id}
</td>
<td key={account.Name}>
{account.Name}
</td>
<td key={account.Phone}>
{account.Phone}
</td>
</tr>
</template>
</tbody>
</table>
<br>
<div style="text-align:center;">
<c-my-test-paginator onprevious={handlePrev} onnext={handleNext}></c-my-test-paginator>
</div>
</lightning-card>
</template>

paginationAcount.js
import { LightningElement,track,wire } from 'lwc';
import fetchAccountRecord from '@salesforce/apex/PaginationAccountController.fetchAccountRecord';
import TotalAccounts from '@salesforce/apex/PaginationAccountController.TotalAccounts';
export default class PaginationAccount extends LightningElement {
@track accounts;
@track error;
@track offset=0;
@track Prevoffset=0;
limit = 8;
@wire(fetchAccountRecord, { offset: '$offset', l: '$limit' }) wiredAccounts({ error, data }) {
if (data) {
this.accounts = data;
if(this.accounts.length == 0){
this.offset = this.Prevoffset;
}
} else if (error) {
this.error = error;
this.accounts = undefined;
}
}
constructor(){
super();
TotalAccounts().then(count =>{
if(count){
this.totalRecords= count;
fetchAccountRecord().then(data =>{
this.accounts = data;
})
}
})
}
handlePrev(_event){
if(this.offset - this.limit >=0){
this.Prevoffset=this.offset;
this.offset = this.offset - this.limit;
}
}
handleNext(_event){
this.Prevoffset=this.offset;
this.offset = this.offset + this.limit;
}
}

Apex:
public with sharing class PaginationAccountController {
@AuraEnabled(cacheable=true)
public static List<Account> fetchAccountRecord(Integer offset, Integer l){
return [SELECT Id, Name, Phone FROM Account LIMIT :l OFFSET :offset ];
}
@AuraEnabled(cacheable=true)
public static Integer TotalAccounts(){
return [SELECT COUNT() FROM Account];
}
}

paginator.html:
<template>
<lightning-button variant="brand" label="Prev" icon-name="utility:chevronleft" title="Base action"
onclick={handlePrev} class="slds-m-left_x-small"></lightning-button>
<lightning-button variant="brand" label="Next" icon-name="utility:chevronright" title="Base action"
onclick={handleNext} class="slds-m-left_x-small"></lightning-button>
</template>

paginator.js:
import { LightningElement } from 'lwc';
export default class MyTestPaginator extends LightningElement {
handlePrev(_event) {
debugger;
this.dispatchEvent(new CustomEvent('previous'));
}
handleNext(_event) {
this.dispatchEvent(new CustomEvent('next'));
}
}



First of all, my code looks not optimized. I have two buttons "previous" and "next" to switch in between the pages. I also want to have page numbers "1,2,3..." so that I can jump in pages more than just with "previous" or "next". I was thinking how to implement it? Probably, using a list or something like that.

Please, advice.
Hi all, 

I am not understanding why 'All' option is not appearning in my combobox dropdown list. It should be listed in here:User-added imagebut for some reason it's not there. 

HTML:
<template> <div class="slds-box slds-theme_default"> <div class="slds-text-color_inverse slds-text-heading_large" style="padding:0.5rem;background:#16325c"> Positions </div> <div style="width:200px; padding:0.5rem;"> <template if:true={statusPicklistValues.data}> <lightning-combobox name="filter" label="Status" value={selectedFilter} variant="label-hidden " options={statusPicklistValues.data.values} onchange={handleChange} placeholder="Select Status to filter"> </lightning-combobox> </template> </div> <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped"> <thead> <tr class="slds-line-height_reset"> <th class="" scope="col">Position Name</th> <th class="" scope="col">Status</th> <th class="" scope="col">Opening Date</th> <th class="" scope="col">Closing Date</th> <th class="" scope="col">Minimum Salary</th> <th class="" scope="col">Maximum Salary</th> </tr> </thead> <tbody> <template if:true={positions}> <template for:each={positions} for:item="pos" for:index="index"> <tr key={pos.Id}> <td>{pos.Name}</td> <td>{pos.Status__c}</td> <!--<td> <template if:true={statusPicklist.data}> <lightning-combobox label="" value={pos.Status__c} options={PositionPicklist.data.values} onchange={handleChangeStatus} data-index={index}> </lightning-combobox> </template></td>--> <td>{pos.Opening_date__c}</td> <td>{pos.Closing_date__c}</td> <td>{pos.Salary_min__c}</td> <td>{pos.Salary_max__c}</td> </tr> </template> </template> </tbody> </table> </div> </template>

JS:
import { LightningElement, wire,track} from 'lwc'; import filterPositionsByStatus from '@salesforce/apex/positionStatusLwcController.filterPositionsByStatus'; import {getPicklistValues} from "lightning/uiObjectInfoApi"; import {getObjectInfo} from "lightning/uiObjectInfoApi"; import POSITION_OBJECT from "@salesforce/schema/Position__c"; import STATUS_FIELD from "@salesforce/schema/Position__c.Status__c"; export default class positionStatusLwc extends LightningElement { error; @track selectedFilter = 'All'; @track data = []; @track positions; @wire(getObjectInfo, {objectApiName: POSITION_OBJECT}) objectInfo; @wire(getPicklistValues, { recordTypeId: '$objectInfo.data.defaultRecordTypeId', fieldApiName: STATUS_FIELD}) statusPicklistValues; handleChange(event){ this.selectedFilter = event.detail.value; this.getFilteredPositions(); } connectedCallback() { this.getFilteredPositions(); } getFilteredPositions(){ filterPositionsByStatus({ selectedFilter: this.selectedFilter }) .then((data) => { if (data) { this.positions = data; } }) .catch((error) => { console.log(error); }); } }

Apex:
public with sharing class positionStatusLwcController { @AuraEnabled(Cacheable=false) public static List<Position__c> filterPositionsByStatus(String selectedFilter){ if (selectedFilter == 'All') { return [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_min__c,Salary_max__c FROM Position__c WHERE Status__c != NULL]; } else { return [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_min__c,Salary_max__c FROM Position__c WHERE Status__c = :selectedFilter]; } } }
Hi All!

I am trying to get the options without hard-coding them. Right now, I have a filter for Position__c, this filter is Status__c and there are 3 possible variants for status: all, opened, closed. While I've made it possible to make it work, but what if a user decides to add more statuses. That will make me code it again, so i want to write a method that will allow user add it automatically.

Here's my code: 
HTML:
<template> <div class="slds-box slds-theme_default"> <div class="slds-text-color_inverse slds-text-heading_large" style="padding:0.5rem;background:#16325c"> Positions </div> <div style="width:200px; padding:0.5rem;"> <lightning-combobox name="filter" label="Status" value={selectedFilter} variant="label-hidden" options={options} onchange={handleChange} placeholder="Select Status to filter"></lightning-combobox> </div> <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped"> <thead> <tr class="slds-line-height_reset"> <th class="" scope="col">Position Name</th> <th class="" scope="col">Status</th> <th class="" scope="col">Opening Date</th> <th class="" scope="col">Closing Date</th> <th class="" scope="col">Minimum Salary</th> <th class="" scope="col">Maximum Salary</th> </tr> </thead> <tbody> <template for:each={positions} for:item="pos" for:index="index"> <tr key={pos.Id}> <td>{pos.Name}</td> <td> <template if:true={statusPicklist.data}> <lightning-combobox label="" value={pos.Status__c} options={statusPicklist.data.values} onchange={handleChangeStatus} > </lightning-combobox> </template></td> <td>{pos.Opening_date__c}</td> <td>{pos.Closing_date__c}</td> <td>{pos.Salary_max__c}</td> <td>{pos.Salary_min__c}</td> </tr> </template> </tbody> </table> </div> </template>

JS:
import { LightningElement, wire,track,api } from 'lwc'; import getPositions from '@salesforce/apex/positionStatusLwcController.getPositions'; import filterByStatus from '@salesforce/apex/positionStatusLwcController.getPositions' import POSITION_OBJECT from '@salesforce/schema/Position__c'; import STATUS_FIELD from '@salesforce/schema/Position__c.Status__c'; import {getPicklistValues,getObjectInfo} from "lightning/uiObjectInfoApi"; export default class PersonList extends LightningElement { positions; selectedFilter = 'All'; ///////////////////////////////////////////////// //Status filter func ///////////////////////////////////////////////// get options() { return [ { label: 'All', value: 'All' }, { label: 'Opened', value: 'Opened' }, { label: 'Closed', value: 'Closed' } ]; } handleChange( event ) { this.selectedFilter = event.detail.value; if ( this.selectedFilter === 'All' ) this.positions = this.initialPositions; else this.filter(); } @wire( getPositions ) wiredAccount( { error, data } ) { if (data) { this.positions = data; this.initialPositions = data; this.error = undefined; } else if ( error ) { console.log('Error is ' + JSON.stringify(error)); this.initialPositions = undefined; this.positions = undefined; } } filter() { if ( this.selectedFilter ) { this.positions = this.initialPositions; if ( this.positions ) { let poss = []; for ( let pos of this.positions ) { if ( pos.Status__c === this.selectedFilter ) { poss.push( pos ); } } this.positions = poss; } } else { this.positions = this.initialPositions; } } ///////////////////////////////////////////////// //Change status on field status for each position ///////////////////////////////////////////////// @wire(getObjectInfo, { objectApiName: POSITION_OBJECT }) accountMetadata; @wire(getPicklistValues, { recordTypeId: '$accountMetadata.data.defaultRecordTypeId', fieldApiName: STATUS_FIELD } ) statusPicklist; handleChangeStatus(event) { this.value = event.detail.value; } }

APEX:
public with sharing class positionStatusLwcController { @AuraEnabled( Cacheable=true ) public static List <Position__c> getPositions() { return [ SELECT Id,Name,Status__c,Opening_date__c,Closing_date__c,Salary_min__c,Salary_max__c FROM Position__c LIMIT 10 ]; } }

So, I was thinking about this:
get options() { return [ { label: 'All', value: 'All' }, { label: 'Opened', value: 'Opened' }, { label: 'Closed', value: 'Closed' } ]; }
I want to do this without label, value hardcoding. IS there a way? I guess i should make a filter method in Apex that will SELECT values for 'All' status and for other statuses.

Please, help. I am only a beginner.
Hey guys and girls,

I am still struggling with my test class. I have written a test class for my custom controller. Let me share the code first.
VF page: 
<apex:page controller="MyPositionsController" showHeader="true" sidebar="true">
<apex:form>
<apex:pageBlock title="Positions list">
<apex:outputLabel value="View: "></apex:outputLabel>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" action="{!processRequests}" rerender="positions_table"/>
<apex:selectOptions value="{!items}"/>
</apex:selectList>
<apex:pageBlockTable value="{!positions}" var="pos" rows="20" id="positions_table">
<apex:column value="{!pos.Name}"></apex:column>
<apex:column headerValue="Status">
<apex:inputField value="{!pos.Status__c}"></apex:inputField>
</apex:column>
<apex:column value="{!pos.Opening_date__c}"></apex:column>
<apex:column value="{!pos.Closing_date__c}"></apex:column>
<apex:column value="{!pos.Salary_max__c}"></apex:column>
<apex:column value="{!pos.Salary_min__c}"></apex:column> -->
</apex:pageBlockTable>
<apex:pageMessages id="showmsg"></apex:pageMessages>
<apex:pageBlockButtons location="top">
<apex:commandButton value="Save" action="{!save}" reRender="showmsg"></apex:commandButton>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex custom controller:

public with sharing class MyPositionsController {
public List<Position__c> positions {get;set;}
public String filterId {get;set;}
public MyPositionsController(){
init();
}
public void init(){
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c ORDER BY Status__c LIMIT 25];
}
public void processRequests(){
if (filterId == 'All') {
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c != null ORDER BY Name LIMIT 25];
} else{
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c = :filterId ORDER BY Name LIMIT 25];
}
}
public List<SelectOption> getItems(){
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Position__c.Status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
options.add(new SelectOption('All','All'));
for (Schema.PicklistEntry f : ple) {
options.add(new SelectOption(f.getLabel(),f.getValue()));
}
return options;
}
public void save(){
update positions;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Successfully saved!');
ApexPages.addMessage(myMsg);
init();
}
}

Test class:

@isTest
public with sharing class MyPositionsController_tests {
@testSetup
static void setup(){
Position__c pos = new Position__c();
pos.Status__c = 'Status';
pos.Opening_date__c = Date.today();
pos.Closing_date__c = Date.today().addDays(30);
pos.Salary_max__c = 5000;
pos.Salary_min__c = 500;
Position__c pos1 = new Position__c();
pos1.Status__c = 'Status';
pos1.Opening_date__c = Date.today();
pos.Closing_date__c = Date.today().addDays(60);
pos.Salary_max__c = 10000;
pos.Salary_min__c = 300;
insert pos;
insert pos1;
}
@isTest static void testMyPositionsController(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
Test.stopTest();
System.assertEquals('Status', pos.Status__c);
}
@isTest static void testProcessRequestsOpenPositions(){
/* Position__c pos1 = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Position__c pos2 = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1]; */
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.filterId = 'Opened';
posController.processRequests();
Test.stopTest();
System.assertEquals('Opened', posController.filterId);
}
/* @isTest static void testProcessRequestsOpenPositions(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.processRequests();
Test.stopTest();
System.assertEquals(null, posController.filterId);
} */
@isTest static void testGetItems(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.getItems();
Test.stopTest();
System.assertEquals('Status', pos.Status__c);
}
@isTest static void testSaveButton(){
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.save();
Test.stopTest();
}
}
-------------------------------------------------------------------------------------------------
So in my test class, it seems like I am not testing the correct data. For example:
1) testMyPositionsController() - here I created a controller, but it seems that i am not testing it. i am checking the position that was created earlier, but instead, i needto check if that position was written in the controller field. I have a List of positions in my controller, I have to check if my position is in that list. 
2) testGetItems() - seems like again I am not testing it correctly. I am testing the position.My method returns a list of select options, so i guess i need to test this method of select options. I don't know how to do that. 
3) testSaveButton() - same here, wrong test logic. I have a controller, which has a List with positions. i guess i should change some fields in these positions and then call save() method. then i need to get my positions from DB and check of DB updated. Also need to test if myssage was shown after the save() button.

Please advice!
Hi everyone!

I am new to SF and Apex. Started from administration and now struggling with the coding. I had some little knowledge of HTML,CSS,JS and Java but honestly it's not much. 

So, I need to write tests for my Apex custom controller. i'll show what I have, this is my VF page:
User-added image
It shows positions and their status. I can change status with the status button and save them. Also, I can use filter drop down list. It's not much of functionallity there. So, right now I have a task to write tests for my Apex custom controller. Honestly, I have no idea how to do it as I've never writter tests before. Need some help or ideas here. 

Here's my VF page code:
<apex:page controller="MyPositionsController" showHeader="true" sidebar="true">
<apex:form>
<apex:pageBlock title="Positions list">
<apex:outputLabel value="View: "></apex:outputLabel>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" action="{!processRequests}" rerender="positions_table"/>
<apex:selectOptions value="{!items}"/>
</apex:selectList>
<apex:pageBlockTable value="{!results}" var="pos" rows="20" id="positions_table">
<apex:column value="{!pos.Name}"></apex:column>
<apex:column headerValue="Status">
<apex:inputField value="{!pos.Status__c}"></apex:inputField>
</apex:column>
<apex:column value="{!pos.Opening_date__c}"></apex:column>
<apex:column value="{!pos.Closing_date__c}"></apex:column>
<apex:column value="{!pos.Salary_max__c}"></apex:column>
<apex:column value="{!pos.Salary_min__c}"></apex:column> -->
</apex:pageBlockTable>
<apex:pageMessages id="showmsg"></apex:pageMessages>
<apex:pageBlockButtons location="top">
<apex:commandButton value="Save" action="{!save}" reRender="showmsg"></apex:commandButton>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex class:
public with sharing class MyPositionsController {
public List<Position__c> results {get;set;}
public String filterId {get;set;}
public MyPositionsController(){
init();
}
public void init(){
results = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c ORDER BY Status__c LIMIT 25];
}
public SelectOption[] processRequests(){
if (filterId == 'All') {
results = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c != null ORDER BY Name LIMIT 25];
} else{
results = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c = :filterId ORDER BY Name LIMIT 25];
}
return null;
}
public List<SelectOption> getItems(){
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Position__c.Status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
options.add(new SelectOption('All','All'));
for (Schema.PicklistEntry f : ple) {
options.add(new SelectOption(f.getLabel(),f.getValue()));
}
return options;
}
public void save(){
update results;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Successfully saved!');
ApexPages.addMessage(myMsg);
init();
}
}

As far as I understand from documentation, I need at least 75% test coverage. But I want to test all methods there as well as constructor method(I guess with constroctor, I need to use List.size method or something like that in order to test it). I don't want the solution, a hint or a few hint will do the work.

Thanks in advance!
Hi all! This is my first post on this community forum, I am an absolute beginner and currently studying Salesforce development by my own.

I am trying to create a Visualforce page with Positions and the ability to filter by Position status ( Opened, Closed). The Status field should be editable and have a Save button.

Like this:
User-added image
This is my code VF:
<apex:page controller="PositionCustomController" showHeader="true" sidebar="true">
<apex:form>
<apex:pageBlock title="Positions list">
<apex:pageBlockTable value="{!positions}" var="pos">
<apex:inputField value="{!pos.Status__c}">
<apex:commandButton action="{! save}" value="Save"></apex:commandButton>
</apex:inputField>
<apex:column value="{!pos.Name}"></apex:column>
<apex:column value="{!pos.Status__c}"></apex:column>
<apex:column value="{!pos.Opening_date__c}"></apex:column>
<apex:column value="{!pos.Closing_date__c}"></apex:column>
<apex:column value="{!pos.Salary_max__c}"></apex:column>
<apex:column value="{!pos.Salary_min__c}"></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex:
public with sharing class PositionCustomController{
public List<Position__c> getPositions(){
List<Position__c> results = Database.query(
'SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c from Position__c' );
return results;
}
}

I know this code is shitty, but I am struggling and need help, it is super discouraging at the moment. Thanks!
Hey guys and girls,

I am still struggling with my test class. I have written a test class for my custom controller. Let me share the code first.
VF page: 
<apex:page controller="MyPositionsController" showHeader="true" sidebar="true">
<apex:form>
<apex:pageBlock title="Positions list">
<apex:outputLabel value="View: "></apex:outputLabel>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" action="{!processRequests}" rerender="positions_table"/>
<apex:selectOptions value="{!items}"/>
</apex:selectList>
<apex:pageBlockTable value="{!positions}" var="pos" rows="20" id="positions_table">
<apex:column value="{!pos.Name}"></apex:column>
<apex:column headerValue="Status">
<apex:inputField value="{!pos.Status__c}"></apex:inputField>
</apex:column>
<apex:column value="{!pos.Opening_date__c}"></apex:column>
<apex:column value="{!pos.Closing_date__c}"></apex:column>
<apex:column value="{!pos.Salary_max__c}"></apex:column>
<apex:column value="{!pos.Salary_min__c}"></apex:column> -->
</apex:pageBlockTable>
<apex:pageMessages id="showmsg"></apex:pageMessages>
<apex:pageBlockButtons location="top">
<apex:commandButton value="Save" action="{!save}" reRender="showmsg"></apex:commandButton>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex custom controller:

public with sharing class MyPositionsController {
public List<Position__c> positions {get;set;}
public String filterId {get;set;}
public MyPositionsController(){
init();
}
public void init(){
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c ORDER BY Status__c LIMIT 25];
}
public void processRequests(){
if (filterId == 'All') {
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c != null ORDER BY Name LIMIT 25];
} else{
positions = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c = :filterId ORDER BY Name LIMIT 25];
}
}
public List<SelectOption> getItems(){
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Position__c.Status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
options.add(new SelectOption('All','All'));
for (Schema.PicklistEntry f : ple) {
options.add(new SelectOption(f.getLabel(),f.getValue()));
}
return options;
}
public void save(){
update positions;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Successfully saved!');
ApexPages.addMessage(myMsg);
init();
}
}

Test class:

@isTest
public with sharing class MyPositionsController_tests {
@testSetup
static void setup(){
Position__c pos = new Position__c();
pos.Status__c = 'Status';
pos.Opening_date__c = Date.today();
pos.Closing_date__c = Date.today().addDays(30);
pos.Salary_max__c = 5000;
pos.Salary_min__c = 500;
Position__c pos1 = new Position__c();
pos1.Status__c = 'Status';
pos1.Opening_date__c = Date.today();
pos.Closing_date__c = Date.today().addDays(60);
pos.Salary_max__c = 10000;
pos.Salary_min__c = 300;
insert pos;
insert pos1;
}
@isTest static void testMyPositionsController(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
Test.stopTest();
System.assertEquals('Status', pos.Status__c);
}
@isTest static void testProcessRequestsOpenPositions(){
/* Position__c pos1 = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Position__c pos2 = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1]; */
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.filterId = 'Opened';
posController.processRequests();
Test.stopTest();
System.assertEquals('Opened', posController.filterId);
}
/* @isTest static void testProcessRequestsOpenPositions(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.processRequests();
Test.stopTest();
System.assertEquals(null, posController.filterId);
} */
@isTest static void testGetItems(){
Position__c pos = [SELECT Id, Status__c, Opening_date__c, Closing_date__c, Salary_max__c, Salary_min__c FROM Position__c LIMIT 1];
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.getItems();
Test.stopTest();
System.assertEquals('Status', pos.Status__c);
}
@isTest static void testSaveButton(){
Test.startTest();
MyPositionsController posController = new MyPositionsController();
posController.save();
Test.stopTest();
}
}
-------------------------------------------------------------------------------------------------
So in my test class, it seems like I am not testing the correct data. For example:
1) testMyPositionsController() - here I created a controller, but it seems that i am not testing it. i am checking the position that was created earlier, but instead, i needto check if that position was written in the controller field. I have a List of positions in my controller, I have to check if my position is in that list. 
2) testGetItems() - seems like again I am not testing it correctly. I am testing the position.My method returns a list of select options, so i guess i need to test this method of select options. I don't know how to do that. 
3) testSaveButton() - same here, wrong test logic. I have a controller, which has a List with positions. i guess i should change some fields in these positions and then call save() method. then i need to get my positions from DB and check of DB updated. Also need to test if myssage was shown after the save() button.

Please advice!
Hi everyone!

I am new to SF and Apex. Started from administration and now struggling with the coding. I had some little knowledge of HTML,CSS,JS and Java but honestly it's not much. 

So, I need to write tests for my Apex custom controller. i'll show what I have, this is my VF page:
User-added image
It shows positions and their status. I can change status with the status button and save them. Also, I can use filter drop down list. It's not much of functionallity there. So, right now I have a task to write tests for my Apex custom controller. Honestly, I have no idea how to do it as I've never writter tests before. Need some help or ideas here. 

Here's my VF page code:
<apex:page controller="MyPositionsController" showHeader="true" sidebar="true">
<apex:form>
<apex:pageBlock title="Positions list">
<apex:outputLabel value="View: "></apex:outputLabel>
<apex:selectList value="{!filterId}" size="1">
<apex:actionSupport event="onchange" action="{!processRequests}" rerender="positions_table"/>
<apex:selectOptions value="{!items}"/>
</apex:selectList>
<apex:pageBlockTable value="{!results}" var="pos" rows="20" id="positions_table">
<apex:column value="{!pos.Name}"></apex:column>
<apex:column headerValue="Status">
<apex:inputField value="{!pos.Status__c}"></apex:inputField>
</apex:column>
<apex:column value="{!pos.Opening_date__c}"></apex:column>
<apex:column value="{!pos.Closing_date__c}"></apex:column>
<apex:column value="{!pos.Salary_max__c}"></apex:column>
<apex:column value="{!pos.Salary_min__c}"></apex:column> -->
</apex:pageBlockTable>
<apex:pageMessages id="showmsg"></apex:pageMessages>
<apex:pageBlockButtons location="top">
<apex:commandButton value="Save" action="{!save}" reRender="showmsg"></apex:commandButton>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex class:
public with sharing class MyPositionsController {
public List<Position__c> results {get;set;}
public String filterId {get;set;}
public MyPositionsController(){
init();
}
public void init(){
results = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c ORDER BY Status__c LIMIT 25];
}
public SelectOption[] processRequests(){
if (filterId == 'All') {
results = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c != null ORDER BY Name LIMIT 25];
} else{
results = [SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c FROM Position__c WHERE Status__c = :filterId ORDER BY Name LIMIT 25];
}
return null;
}
public List<SelectOption> getItems(){
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Position__c.Status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
options.add(new SelectOption('All','All'));
for (Schema.PicklistEntry f : ple) {
options.add(new SelectOption(f.getLabel(),f.getValue()));
}
return options;
}
public void save(){
update results;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Successfully saved!');
ApexPages.addMessage(myMsg);
init();
}
}

As far as I understand from documentation, I need at least 75% test coverage. But I want to test all methods there as well as constructor method(I guess with constroctor, I need to use List.size method or something like that in order to test it). I don't want the solution, a hint or a few hint will do the work.

Thanks in advance!
Hi all! This is my first post on this community forum, I am an absolute beginner and currently studying Salesforce development by my own.

I am trying to create a Visualforce page with Positions and the ability to filter by Position status ( Opened, Closed). The Status field should be editable and have a Save button.

Like this:
User-added image
This is my code VF:
<apex:page controller="PositionCustomController" showHeader="true" sidebar="true">
<apex:form>
<apex:pageBlock title="Positions list">
<apex:pageBlockTable value="{!positions}" var="pos">
<apex:inputField value="{!pos.Status__c}">
<apex:commandButton action="{! save}" value="Save"></apex:commandButton>
</apex:inputField>
<apex:column value="{!pos.Name}"></apex:column>
<apex:column value="{!pos.Status__c}"></apex:column>
<apex:column value="{!pos.Opening_date__c}"></apex:column>
<apex:column value="{!pos.Closing_date__c}"></apex:column>
<apex:column value="{!pos.Salary_max__c}"></apex:column>
<apex:column value="{!pos.Salary_min__c}"></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex:
public with sharing class PositionCustomController{
public List<Position__c> getPositions(){
List<Position__c> results = Database.query(
'SELECT Name,Status__c,Opening_date__c,Closing_date__c,Salary_max__c,Salary_min__c from Position__c' );
return results;
}
}

I know this code is shitty, but I am struggling and need help, it is super discouraging at the moment. Thanks!
Hi all, 
           I am beginer of salesforce.Please any one help me  the following code implementation. how to Write Apex class to update all the opportunities close date as today()?

Thanks in advance