-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
3Questions
-
4Replies
Trouble with PageReference testing
I have a very simple extension to a custom object that I use for three reasons: 1, so make sure that the user's custom profile is being utilized, 2, the classification is set on this specific VF page and 3, the command buttons are specific to the VF page being used.
My Controller:
This is my Test Controller which is giving me a "System.NullPointerException: Attempted to upsert a null list" error
Class.EntryExtension.saveID: line 26, column 1 Class.TestEntryExtension.test: line 24, column 1
Obviously, I would want the other page references to be tested as well, but I can't even seem to get the one to work...
I greatly appreciate any help I can get!!
My Controller:
public class EntryExtension { public Journal_Entry__c entry{get; set;} public EntryExtension (ApexPages.StandardController stdController){ List<Contact> users = [Select JournalProfileID__c From Contact Where Id In (Select ContactId From User Where Id = :UserInfo.getUserId()) limit 1]; if (!users.isEmpty()) { entry = (Journal_Entry__c)stdController.getRecord(); entry.Journal_Profile__c = users.get(0).JournalProfileID__c; entry.Classification__c = 'TestClass'; } Id recordId = stdController.getId(); if(recordId == null){ entry.ShowTrueFalse__c = 'ShowTrue'; entry.ShowTrueFalse2__c = 'ShowFalse'; } } public PageReference saveID(){ upsert entry; PageReference reRend = new PageReference('/TheEntry'); reRend.getParameters().put('id',entry.Id); reRend.setRedirect(true); return reRend; } public PageReference newSaveSelf(){ upsert entry; PageReference reRend = new PageReference('/SelfInterpretation'); reRend.getParameters().put('journalEntry',entry.Id); reRend.setRedirect(true); return reRend; } public PageReference saveSelf(){ String journalEntryID = ApexPages.currentPage().getParameters().get('id'); List<Journal_Entry__c> checkVis = [Select Self_Interp__c, SelfID__c From Journal_Entry__c Where Id = :journalEntryID limit 1]; String finalCheck = checkVis.get(0).SelfID__c; upsert entry; PageReference reRend = new PageReference('/SelfInterpretation'); reRend.getParameters().put('id',finalCheck); reRend.setRedirect(true); return reRend; } public PageReference SubmitForInterp(){ upsert entry; PageReference reRend = new PageReference('/SaleData'); reRend.getParameters().put('journalEntry',entry.Id); reRend.setRedirect(true); return reRend; } }
This is my Test Controller which is giving me a "System.NullPointerException: Attempted to upsert a null list" error
Class.EntryExtension.saveID: line 26, column 1 Class.TestEntryExtension.test: line 24, column 1
Obviously, I would want the other page references to be tested as well, but I can't even seem to get the one to work...
@isTest (seealldata=true) public class TestEntryExtension{ public static testMethod void test() { Journal_Entry__c entry = new Journal_Entry__c(); List<Contact> users = [Select JournalProfileID__c From Contact Where FirstName = 'Test' limit 1]; entry.Journal_Profile__c = users.get(0).JournalProfileID__c; entry.Classification__c = 'TestClass'; entry.Name = 'Test Tests'; entry.ShowTrueFalse__c = 'ShowTrue'; entry.ShowTrueFalse2__c = 'ShowFalse'; insert entry; PageReference pref = Page.TheEntry; pref.getParameters().put('id', entry.id); Test.setCurrentPage(pref); ApexPAges.StandardController sc = new ApexPages.StandardController(entry); EntryExtension testController = new EntryExtension(sc); PageReference result = testController.saveID(); System.assertNotEquals(null, result); // testController.newSaveSelf(); // testController.saveSelf(); // testController.SubmitForInterp(); system.assert(testController.entry.Id != null); apexPages.Currentpage().getParameters().put('Id',entry.id); } }
I greatly appreciate any help I can get!!
-
- Logan Smith 10
- May 16, 2016
- Like
- 0
Why can't I get focus to stay on input field using javascript or apex:actionFunction
I have a page that I use to search Entries in a Journal in our Salesforce Communities visualforce page. The system works just fine, except that I have been asked to make it search upon keyup. This was obviously easy, but presented a problem that I can't seem to figure out! If you type the text quickly, you can type whatever you want, but if there is even the slightest pause, the search happens and focus is lost from the input field. I kinda expected focus to be lost, but what I didn't anticipate, was not being able to use JS or apex:actionFunction to regain focus. I have tried JS to give focus upon load of the page, but it wont, I have tried creating a simple button that gives focus to the inputfield, but it wont. If you click on the input field, it will hold focus until the action is performed, but otherwise, it will not "take focus". Would you please look at my code and tell me where I'm going wrong?
The visualforce page:
My Controller:
I suspect that the problem with all of this has something to do with the rerendering after each onkeyup, but that wouldn't explain why other functions such as on page load and apex:actionFunction don't work...
I would greatly appreciate any insight!
The visualforce page:
<apex:page showHeader="false" standardStylesheets="false" controller="SOSLController" cache="false" action="{!soslLoad_method}" docType="html-5.0" applyHtmlTag="false"> <head> <meta charset="utf-8"/> <title>My Journal</title> <meta name="viewport" content="width=device-width, initial-scale=1"/> <meta name="generator" content="Webflow"/> <link href="{!URLFOR($Resource.Webflow_CSS, 'css/normalize.css')}" rel="stylesheet"/> <link href="{!URLFOR($Resource.Webflow_CSS, 'css/webflow.css')}" rel="stylesheet"/> <link href="{!URLFOR($Resource.Webflow_CSS, 'css/eyes2cNoLinks.css')}" rel="stylesheet"/> <script type="text/javascript" src="{!URLFOR($Resource.Webflow_JS, 'js/modernizr.js')}"></script> <link rel="shortcut icon" type="image/x-icon" href="https://s3.amazonaws.com/eyes2c/Images/favicon-32x32.png"/> <link rel="apple-touch-icon" href="https://s3.amazonaws.com/eyes2c/Images/Eyes2CLogo.png"/> <script src="https://ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js"></script> <script> WebFont.load({ google: { families: ["Droid Serif:400,700","Roboto:300,regular,500","Roboto Slab:regular"] } }); </script> </head> <!--This is what I use to toggle through detailed and simple version of Table--> <script type="text/javascript"> function hideshow(which){ if (!document.getElementById) return if (which.style.display=="none") which.style.display="block" else which.style.display="none" } </script> <body id="TheTop" class="BG"> <apex:form > <!--The focus part of this does not work--> <apex:actionFunction name="saveUpdates" focus="myAnchor" action="{!soslDemo_method}"/> <script> saveUpdates(); </script> <script> function getfocus() { document.getElementById("myAnchor").focus(); } </script> <!--I made this button to test the function, and it does not work--> <input type="button" onclick="getfocus()" value="Get focus"/> <!--This is the input field that loses focus--> <apex:inputText id="myAnchor" tabindex="1" styleClass="inputField" onkeyup="saveUpdates()" value="{!searchStr}" /> <!--I have two of these for toggling between detailed and simple versions of table--> <div id="but1" style="display: block" class="ck-button" onclick="javascript:hideshow(document.getElementById('but1')); javascript:hideshow(document.getElementById('but2')); javascript:hideshow(document.getElementById('adiv1')); javascript:hideshow(document.getElementById('adiv2'))"><label><input style="display: none" type="checkbox" value="1" onclick="javascript:hideshow(document.getElementById('but1')); javascript:hideshow(document.getElementById('but2')); javascript:hideshow(document.getElementById('adiv1')); javascript:hideshow(document.getElementById('adiv2'))">Browse</input></label> </div> <apex:actionStatus id="actStatusId"> <apex:facet name="start" > <img src="/img/loading.gif"/> </apex:facet> </apex:actionStatus> </apex:form> <apex:outputPanel title="" id="error"> <apex:pageMessages ></apex:pageMessages> </apex:outputPanel> <div id="adiv1" style="display: block"> <apex:repeat value="{!entlist}" id="entr" var="ent" rendered="true"> <!--All of my table data Simple View--> </apex:repeat> </div> <div id="adiv2" style="display: none"> <apex:repeat value="{!entlist}" id="entr1" var="ent" rendered="true"> <!--All of my table data for Detailed View--> </apex:repeat> </div> <footer> <script type="text/javascript"> function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } } function setFocus() { document.getElementById('{!$Component.myAnchor}').focus(); } addLoadEvent(setFocus); </script> </footer> </body> </apex:page>
My Controller:
Public with sharing class SOSLController{ Public List<Journal_Entry__c> entList {get;set;} Public String searchStr{get;set;} Public void SOSLController(){ } Public void soslLoad_method(){ List<List <sObject>> searchList = [FIND 'All' IN ALL FIELDS RETURNING Journal_Entry__c (Id,Name,Color__c,ShowTrueFalse__c,ShowTrueFalse2__c,Context__c,Entry_Series__c,SelfID__c,Related_to_Previous_Dream__c,Scene_One__c,Scene_Two__c,Self_Interp__c,CreatedDate,DateText__c,Submitted_for_Interp_Count__c,Classification__c,Created_Self_Interp__c,Haven__c,UserID__c WHERE UserID__c = :UserInfo.getUserId() ORDER BY CreatedDate Desc)]; entList = ((List<Journal_Entry__c>)searchList[0]); } Public void soslDemo_method(){ if(searchStr.length() < 2) { List<List <sObject>> searchList = [FIND 'All' IN ALL FIELDS RETURNING Journal_Entry__c (Id,Name,Color__c,ShowTrueFalse__c,ShowTrueFalse2__c,Context__c,Entry_Series__c,SelfID__c,Related_to_Previous_Dream__c,Scene_One__c,Scene_Two__c,Self_Interp__c,CreatedDate,DateText__c,Submitted_for_Interp_Count__c,Classification__c,Created_Self_Interp__c,Haven__c,UserID__c WHERE UserID__c = :UserInfo.getUserId() ORDER BY CreatedDate Desc)]; entList = ((List<Journal_Entry__c>)searchList[0]); } else { String searchStr1 = '*'+searchStr+'*'; List<List <sObject>> searchList = [FIND :searchStr1 IN ALL FIELDS RETURNING Journal_Entry__c (Id,Name,Color__c,ShowTrueFalse__c,ShowTrueFalse2__c,Context__c,Entry_Series__c,SelfID__c,Related_to_Previous_Dream__c,Scene_One__c,Scene_Two__c,Self_Interp__c,CreatedDate,DateText__c,Submitted_for_Interp_Count__c,Classification__c,Created_Self_Interp__c,Haven__c,UserID__c WHERE UserID__c = :UserInfo.getUserId() ORDER BY CreatedDate Desc)]; entList = ((List<Journal_Entry__c>)searchList[0]); if(entList.size() == 0){ apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sory, no results returned with matching string..')); } } } }
I suspect that the problem with all of this has something to do with the rerendering after each onkeyup, but that wouldn't explain why other functions such as on page load and apex:actionFunction don't work...
I would greatly appreciate any insight!
-
- Logan Smith 10
- April 07, 2016
- Like
- 0
Trouble with Test Method for SOSL Search
Hello, I am having trouble working out how to get my SOSL search page to test with a solid percentage. The reason I am using SOSL is because of the Long Text Areas that I am searching through.
Here is my Controller:
And this is what I have so far with my Test...
You will notice that I give the userID string a user Id that works in my VF pages for the test I'm trying to do, but in the Controller, this Id is captured from the active user.
I understand that SOSL has a special syntax to be used when Testing, based on this page "Adding SOSL Queries to Unit Tests" and you will see that I have attempted to use this, but I am obviously failing misserably.
I would greatly appreciate any help I can get!
Here is my Controller:
Public with sharing class SOSLController{ Public List<Journal_Entry__c> entList {get;set;} Public String searchStr{get;set;} Public SOSLController(){ } Public void soslDemo_method(){ String userID = UserInfo.getUserId(); entList = New List<Journal_Entry__c>(); if(searchStr.length() > 1){ String searchStr1 = '*'+searchStr+'*'; String searchQuery = 'FIND \'' + searchStr1 + '\' IN ALL FIELDS RETURNING Journal_Entry__c (Id,Name,DateText__c,Submitted_for_Interp_Count__c,Created_Self_Interp__c,Haven__c,UserID__c WHERE UserID__c LIKE :userID)'; List<List <sObject>> searchList = search.query(searchQuery); entList = ((List<Journal_Entry__c>)searchList[0]); if(entList.size() == 0){ apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sory, no results returned with matching string..')); return; } } else{ apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least two characters..')); return; } } }
And this is what I have so far with my Test...
You will notice that I give the userID string a user Id that works in my VF pages for the test I'm trying to do, but in the Controller, this Id is captured from the active user.
@isTest public class TestSOSLController{ public static testMethod void testSoslFixedResults() { PageReference myVfPage = Page.MyJournal; Test.setCurrentPage(myVfPage); SOSLController testCont = new SOSLController(); String userID = '00555000001R62HAAS'; Id [] fixedSearchResults= new Id[1]; fixedSearchResults[0] = 'a0955000001ODaU'; Test.setFixedSearchResults(fixedSearchResults); List<List<SObject>> searchList = [FIND 'test' IN ALL FIELDS RETURNING Journal_Entry__c(Id,Name,DateText__c,Submitted_for_Interp_Count__c,Created_Self_Interp__c,Haven__c,UserID__c WHERE UserID__c LIKE :userID LIMIT 1)]; system.assert(searchList != null); } }
I understand that SOSL has a special syntax to be used when Testing, based on this page "Adding SOSL Queries to Unit Tests" and you will see that I have attempted to use this, but I am obviously failing misserably.
I would greatly appreciate any help I can get!
-
- Logan Smith 10
- March 24, 2016
- Like
- 0
Why is Cordova failing when I try to get started with Hybrid Development?
Hi!
I'm currently trying to work my way through the Trailhead "Get Started with Hybrid Development", but am having problems when I attempt to "forcedroid create".
When it gets to "Installing "cordova-plugin-whitelist" for android" it fails with:
This error is not imediately fatal, but after a time I see:
Whereas, this last bit is fatal.
FWIW,
OS: Windows XP SP3
Java: 1.8.0_121
Node: 5.12.0
Npm: 4.2.0
Cordovoa: 6.5.0
ANDROID_HOME: C:\devtools\Android\sdk
ANDROID_SDK_HOME: C:\devtools\Android\sdk
JAVA_HOME: C:\devtools\Java\jdk1.8.0_121
JDK_HOME: %JAVA_HOME%
JRE_HOME: %JAVA_HOME%\jre
CLASSPATH: .;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
User PATH: %JAVA_HOME%\bin;C:\Documents and Settings\Brian Kessler\Application Data\npm
System PATH: C:\Documents and Settings\All Users\Application Data\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Pinnacle\Shared Files\InstantCDDVD\;C:\WINXPSP3\system32\WindowsPowerShell\v1.0;C:\Program Files\Skype\Phone\;C:\Program Files\Kensington\TrackballWorks;C:\devtools\Git\cmd;C:\devtools\Git\GitExtensions\;C:\devtools\nodejs\;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools
Any ideas what is wrong or how to fix this?
I'm currently trying to work my way through the Trailhead "Get Started with Hybrid Development", but am having problems when I attempt to "forcedroid create".
When it gets to "Installing "cordova-plugin-whitelist" for android" it fails with:
Failed to install 'cordova-plugin-whitelist':TypeError: Path must be a string. Received undefined at assertPath (path.js:7:11) at Object.join (path.js:466:7) at D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\lib\check_reqs.js:177:42 at _fulfilled (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:834:54) at self.promiseDispatch.done (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:863:30) at Promise.promise.promiseDispatch (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:796:13) at D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:857:14 at runSingle (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:137:13) at flush (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:125:13) at _combinedTickCallback (internal/process/next_tick.js:67:7) Failed to restore plugin "cordova-plugin-whitelist" from config.xml. You might need to try adding it again. Error: TypeError: Path must be a string. Received undefined
This error is not imediately fatal, but after a time I see:
Installing "cordova-plugin-whitelist" for android Failed to install 'cordova-plugin-whitelist':TypeError: Path must be a string. Received undefined at assertPath (path.js:7:11) at Object.join (path.js:466:7) at D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\lib\check_reqs.js:177:42 at _fulfilled (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:834:54) at self.promiseDispatch.done (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:863:30) at Promise.promise.promiseDispatch (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:796:13) at D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:857:14 at runSingle (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:137:13) at flush (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:125:13) at _combinedTickCallback (internal/process/next_tick.js:67:7) Failed to install 'com.salesforce':TypeError: Path must be a string. Received undefined at assertPath (path.js:7:11) at Object.join (path.js:466:7) at D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\lib\check_reqs.js:177:42 at _fulfilled (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:834:54) at self.promiseDispatch.done (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:863:30) at Promise.promise.promiseDispatch (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:796:13) at D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:857:14 at runSingle (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:137:13) at flush (D:\tutorials\AndroidStudioProjects\THA\platforms\android\cordova\node_modules\q\q.js:125:13) at _combinedTickCallback (internal/process/next_tick.js:67:7) D:\tutorials\AndroidStudioProjects forcedroid create failed Command failed: cordova plugin add https://github.com/forcedotcom/SalesforceMobileSDK-CordovaPlugin#v5.0.0 --force Error: Path must be a string. Received undefined
Whereas, this last bit is fatal.
FWIW,
OS: Windows XP SP3
Java: 1.8.0_121
Node: 5.12.0
Npm: 4.2.0
Cordovoa: 6.5.0
ANDROID_HOME: C:\devtools\Android\sdk
ANDROID_SDK_HOME: C:\devtools\Android\sdk
JAVA_HOME: C:\devtools\Java\jdk1.8.0_121
JDK_HOME: %JAVA_HOME%
JRE_HOME: %JAVA_HOME%\jre
CLASSPATH: .;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
User PATH: %JAVA_HOME%\bin;C:\Documents and Settings\Brian Kessler\Application Data\npm
System PATH: C:\Documents and Settings\All Users\Application Data\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Pinnacle\Shared Files\InstantCDDVD\;C:\WINXPSP3\system32\WindowsPowerShell\v1.0;C:\Program Files\Skype\Phone\;C:\Program Files\Kensington\TrackballWorks;C:\devtools\Git\cmd;C:\devtools\Git\GitExtensions\;C:\devtools\nodejs\;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools
Any ideas what is wrong or how to fix this?
- Brian Kessler
- February 10, 2017
- Like
- 0
Trouble with PageReference testing
I have a very simple extension to a custom object that I use for three reasons: 1, so make sure that the user's custom profile is being utilized, 2, the classification is set on this specific VF page and 3, the command buttons are specific to the VF page being used.
My Controller:
This is my Test Controller which is giving me a "System.NullPointerException: Attempted to upsert a null list" error
Class.EntryExtension.saveID: line 26, column 1 Class.TestEntryExtension.test: line 24, column 1
Obviously, I would want the other page references to be tested as well, but I can't even seem to get the one to work...
I greatly appreciate any help I can get!!
My Controller:
public class EntryExtension { public Journal_Entry__c entry{get; set;} public EntryExtension (ApexPages.StandardController stdController){ List<Contact> users = [Select JournalProfileID__c From Contact Where Id In (Select ContactId From User Where Id = :UserInfo.getUserId()) limit 1]; if (!users.isEmpty()) { entry = (Journal_Entry__c)stdController.getRecord(); entry.Journal_Profile__c = users.get(0).JournalProfileID__c; entry.Classification__c = 'TestClass'; } Id recordId = stdController.getId(); if(recordId == null){ entry.ShowTrueFalse__c = 'ShowTrue'; entry.ShowTrueFalse2__c = 'ShowFalse'; } } public PageReference saveID(){ upsert entry; PageReference reRend = new PageReference('/TheEntry'); reRend.getParameters().put('id',entry.Id); reRend.setRedirect(true); return reRend; } public PageReference newSaveSelf(){ upsert entry; PageReference reRend = new PageReference('/SelfInterpretation'); reRend.getParameters().put('journalEntry',entry.Id); reRend.setRedirect(true); return reRend; } public PageReference saveSelf(){ String journalEntryID = ApexPages.currentPage().getParameters().get('id'); List<Journal_Entry__c> checkVis = [Select Self_Interp__c, SelfID__c From Journal_Entry__c Where Id = :journalEntryID limit 1]; String finalCheck = checkVis.get(0).SelfID__c; upsert entry; PageReference reRend = new PageReference('/SelfInterpretation'); reRend.getParameters().put('id',finalCheck); reRend.setRedirect(true); return reRend; } public PageReference SubmitForInterp(){ upsert entry; PageReference reRend = new PageReference('/SaleData'); reRend.getParameters().put('journalEntry',entry.Id); reRend.setRedirect(true); return reRend; } }
This is my Test Controller which is giving me a "System.NullPointerException: Attempted to upsert a null list" error
Class.EntryExtension.saveID: line 26, column 1 Class.TestEntryExtension.test: line 24, column 1
Obviously, I would want the other page references to be tested as well, but I can't even seem to get the one to work...
@isTest (seealldata=true) public class TestEntryExtension{ public static testMethod void test() { Journal_Entry__c entry = new Journal_Entry__c(); List<Contact> users = [Select JournalProfileID__c From Contact Where FirstName = 'Test' limit 1]; entry.Journal_Profile__c = users.get(0).JournalProfileID__c; entry.Classification__c = 'TestClass'; entry.Name = 'Test Tests'; entry.ShowTrueFalse__c = 'ShowTrue'; entry.ShowTrueFalse2__c = 'ShowFalse'; insert entry; PageReference pref = Page.TheEntry; pref.getParameters().put('id', entry.id); Test.setCurrentPage(pref); ApexPAges.StandardController sc = new ApexPages.StandardController(entry); EntryExtension testController = new EntryExtension(sc); PageReference result = testController.saveID(); System.assertNotEquals(null, result); // testController.newSaveSelf(); // testController.saveSelf(); // testController.SubmitForInterp(); system.assert(testController.entry.Id != null); apexPages.Currentpage().getParameters().put('Id',entry.id); } }
I greatly appreciate any help I can get!!
- Logan Smith 10
- May 16, 2016
- Like
- 0
Why can't I get focus to stay on input field using javascript or apex:actionFunction
I have a page that I use to search Entries in a Journal in our Salesforce Communities visualforce page. The system works just fine, except that I have been asked to make it search upon keyup. This was obviously easy, but presented a problem that I can't seem to figure out! If you type the text quickly, you can type whatever you want, but if there is even the slightest pause, the search happens and focus is lost from the input field. I kinda expected focus to be lost, but what I didn't anticipate, was not being able to use JS or apex:actionFunction to regain focus. I have tried JS to give focus upon load of the page, but it wont, I have tried creating a simple button that gives focus to the inputfield, but it wont. If you click on the input field, it will hold focus until the action is performed, but otherwise, it will not "take focus". Would you please look at my code and tell me where I'm going wrong?
The visualforce page:
My Controller:
I suspect that the problem with all of this has something to do with the rerendering after each onkeyup, but that wouldn't explain why other functions such as on page load and apex:actionFunction don't work...
I would greatly appreciate any insight!
The visualforce page:
<apex:page showHeader="false" standardStylesheets="false" controller="SOSLController" cache="false" action="{!soslLoad_method}" docType="html-5.0" applyHtmlTag="false"> <head> <meta charset="utf-8"/> <title>My Journal</title> <meta name="viewport" content="width=device-width, initial-scale=1"/> <meta name="generator" content="Webflow"/> <link href="{!URLFOR($Resource.Webflow_CSS, 'css/normalize.css')}" rel="stylesheet"/> <link href="{!URLFOR($Resource.Webflow_CSS, 'css/webflow.css')}" rel="stylesheet"/> <link href="{!URLFOR($Resource.Webflow_CSS, 'css/eyes2cNoLinks.css')}" rel="stylesheet"/> <script type="text/javascript" src="{!URLFOR($Resource.Webflow_JS, 'js/modernizr.js')}"></script> <link rel="shortcut icon" type="image/x-icon" href="https://s3.amazonaws.com/eyes2c/Images/favicon-32x32.png"/> <link rel="apple-touch-icon" href="https://s3.amazonaws.com/eyes2c/Images/Eyes2CLogo.png"/> <script src="https://ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js"></script> <script> WebFont.load({ google: { families: ["Droid Serif:400,700","Roboto:300,regular,500","Roboto Slab:regular"] } }); </script> </head> <!--This is what I use to toggle through detailed and simple version of Table--> <script type="text/javascript"> function hideshow(which){ if (!document.getElementById) return if (which.style.display=="none") which.style.display="block" else which.style.display="none" } </script> <body id="TheTop" class="BG"> <apex:form > <!--The focus part of this does not work--> <apex:actionFunction name="saveUpdates" focus="myAnchor" action="{!soslDemo_method}"/> <script> saveUpdates(); </script> <script> function getfocus() { document.getElementById("myAnchor").focus(); } </script> <!--I made this button to test the function, and it does not work--> <input type="button" onclick="getfocus()" value="Get focus"/> <!--This is the input field that loses focus--> <apex:inputText id="myAnchor" tabindex="1" styleClass="inputField" onkeyup="saveUpdates()" value="{!searchStr}" /> <!--I have two of these for toggling between detailed and simple versions of table--> <div id="but1" style="display: block" class="ck-button" onclick="javascript:hideshow(document.getElementById('but1')); javascript:hideshow(document.getElementById('but2')); javascript:hideshow(document.getElementById('adiv1')); javascript:hideshow(document.getElementById('adiv2'))"><label><input style="display: none" type="checkbox" value="1" onclick="javascript:hideshow(document.getElementById('but1')); javascript:hideshow(document.getElementById('but2')); javascript:hideshow(document.getElementById('adiv1')); javascript:hideshow(document.getElementById('adiv2'))">Browse</input></label> </div> <apex:actionStatus id="actStatusId"> <apex:facet name="start" > <img src="/img/loading.gif"/> </apex:facet> </apex:actionStatus> </apex:form> <apex:outputPanel title="" id="error"> <apex:pageMessages ></apex:pageMessages> </apex:outputPanel> <div id="adiv1" style="display: block"> <apex:repeat value="{!entlist}" id="entr" var="ent" rendered="true"> <!--All of my table data Simple View--> </apex:repeat> </div> <div id="adiv2" style="display: none"> <apex:repeat value="{!entlist}" id="entr1" var="ent" rendered="true"> <!--All of my table data for Detailed View--> </apex:repeat> </div> <footer> <script type="text/javascript"> function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); } } } function setFocus() { document.getElementById('{!$Component.myAnchor}').focus(); } addLoadEvent(setFocus); </script> </footer> </body> </apex:page>
My Controller:
Public with sharing class SOSLController{ Public List<Journal_Entry__c> entList {get;set;} Public String searchStr{get;set;} Public void SOSLController(){ } Public void soslLoad_method(){ List<List <sObject>> searchList = [FIND 'All' IN ALL FIELDS RETURNING Journal_Entry__c (Id,Name,Color__c,ShowTrueFalse__c,ShowTrueFalse2__c,Context__c,Entry_Series__c,SelfID__c,Related_to_Previous_Dream__c,Scene_One__c,Scene_Two__c,Self_Interp__c,CreatedDate,DateText__c,Submitted_for_Interp_Count__c,Classification__c,Created_Self_Interp__c,Haven__c,UserID__c WHERE UserID__c = :UserInfo.getUserId() ORDER BY CreatedDate Desc)]; entList = ((List<Journal_Entry__c>)searchList[0]); } Public void soslDemo_method(){ if(searchStr.length() < 2) { List<List <sObject>> searchList = [FIND 'All' IN ALL FIELDS RETURNING Journal_Entry__c (Id,Name,Color__c,ShowTrueFalse__c,ShowTrueFalse2__c,Context__c,Entry_Series__c,SelfID__c,Related_to_Previous_Dream__c,Scene_One__c,Scene_Two__c,Self_Interp__c,CreatedDate,DateText__c,Submitted_for_Interp_Count__c,Classification__c,Created_Self_Interp__c,Haven__c,UserID__c WHERE UserID__c = :UserInfo.getUserId() ORDER BY CreatedDate Desc)]; entList = ((List<Journal_Entry__c>)searchList[0]); } else { String searchStr1 = '*'+searchStr+'*'; List<List <sObject>> searchList = [FIND :searchStr1 IN ALL FIELDS RETURNING Journal_Entry__c (Id,Name,Color__c,ShowTrueFalse__c,ShowTrueFalse2__c,Context__c,Entry_Series__c,SelfID__c,Related_to_Previous_Dream__c,Scene_One__c,Scene_Two__c,Self_Interp__c,CreatedDate,DateText__c,Submitted_for_Interp_Count__c,Classification__c,Created_Self_Interp__c,Haven__c,UserID__c WHERE UserID__c = :UserInfo.getUserId() ORDER BY CreatedDate Desc)]; entList = ((List<Journal_Entry__c>)searchList[0]); if(entList.size() == 0){ apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sory, no results returned with matching string..')); } } } }
I suspect that the problem with all of this has something to do with the rerendering after each onkeyup, but that wouldn't explain why other functions such as on page load and apex:actionFunction don't work...
I would greatly appreciate any insight!
- Logan Smith 10
- April 07, 2016
- Like
- 0