function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Julio HernandezJulio Hernandez 

Apex:inputfield value

Hi All,

Hope everyone is doing well before the holidays.

I am having an issue trying to create a VF page to use as a Quick Action. I want to use Apex:inputfield to have a lookup field of our users so when our case managers are in a case they can use the quick action button to assign the case quickly to another user. My could works up until the part where I try to get the value of the Apex:inputfield, the user's name. Any suggestions or arrows pointing in the right direction will be greatly appreciated.
 
<apex:page docType="html-5.0" standardController="Case" title="Case Assign">

    <apex:remoteObjects >
        <apex:remoteObjectModel name="Case" fields="Id,OwnerId,Status"/>
    </apex:remoteObjects>
    <script src ="/soap/ajax/35.0/connection.js"></script>
    
    <apex:form id="form">
        <apex:inputField value="{!case.ownerID}" id="newOwner"/>          
    </apex:form>
    <div class="mypage">    
        <button onclick="assignOwnership()">Assign Ownership</button>    
    </div>
    
    <script>
        function assignOwnership() {
            var jq$ = jQuery.noConflict();           
            var newOwn = jQuery('[id$=newOwner]').val();
            alert(newOwn);
            var caseObj = new sforce.SObject("Case"); 
            caseObj.Id = "{!Case.Id}"; 
            caseObj.OwnerId = newOwn; 
            caseObj.Status = 'In Progress'; 
            
            var result = sforce.connection.update([caseObj]); 

            if (result[0].success=='false') { 
            alert(result[0].errors.message); 
            } else { 
            location.reload(true); 
            }
        }
        
        

     </script>
     
     
</apex:page>

 
pconpcon
This is actually pretty simple to fix but a little tougher to explain what's happening.  The reason is that the lookup stuff that happens in Salesforce is all stored in hidden input fields on the Visualforce page.  When you select the User / Queue the Name is put in the text box and the id, name and other data is addtionally stored in hidden fields.  The field you are looking for is the one that is appended with _lkid.  So if you change line 18 to be the following you should be good to go.
 
jQuery('[id$=newOwner_lkid]').val();
Julio HernandezJulio Hernandez
Hi,

I changed my code a bit because it wasnt working. I am able to pull the owner ID now but I can't seem to add it to the case Owner ID.
Here is my updated code.
 
<apex:page docType="html-5.0" standardController="Case" title="Case Assign" id="apexPage">

    <apex:remoteObjects >
        <apex:remoteObjectModel name="Case" fields="Id,OwnerId,Status"/>
    </apex:remoteObjects>   
    
    <script src ="/soap/ajax/35.0/connection.js"></script>
    
    <apex:form id="form">
        <apex:inputField value="{!case.ownerID}" id="newOwner">   
        </apex:inputField>       
    </apex:form>
    <div class="mypage">    
        <button onclick="assignOwnership('{!$Component.apexPage.form.newOwner}')">Assign Ownership</button> 
    </div>
    
    
    
    <script>
        function assignOwnership(elemid) {
        
            newOwn = document.getElementById(elemid + '_lkid').value;
            alert(newOwn);
            var caseObj = new sforce.SObject("Case"); 
            caseObj.Id = "{!Case.Id}"; 
            caseObj.OwnerId = document.getElementById(elemid + '_lkid').value; 
            caseObj.Status = 'In Progress'; 
            alert(caseObj.ownerId);
            var result = sforce.connection.update([caseObj]); 

            if (result[0].success=='false') { 
            alert(result[0].errors.message); 
            } else { 
            location.reload(true); 
            alert('hi');
            }

            
        }
        
        

        
        

     </script>
     
     
</apex:page>