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
Snehal Gaware 15Snehal Gaware 15 

autofilled contact name on vf page by logged in community user's name

Hi All,

I have a vf page where i have a field Contact name on case which is a lookup to contact. I want when the user logged in as community user it should autopopulate his/her name in that contact field.

vf-page:
<apex:page standardController="Case" extensions="AutoPopulateExample">
   
    <apex:form id="form">
    	<apex:pageBlock >
          
            <apex:pageBlockButtons location="top">
              <apex:commandButton value="Save" action="{!Save}"/>
              
              <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons> 
            <apex:actionRegion >
           <apex:pageBlockSection title="Basic Details" columns="2">
               <apex:inputField label="Contact Name" value="{!Case.ContactId}" >
               	<apex:actionSupport event="onchange" action="{!autoCal}" reRender="form"/>
               </apex:inputField>
               <apex:inputField label="Requestor First Name" value="{!Case.Requestor_First_Name__c}"/>
               <apex:inputField label="Account Name" value="{!Case.AccountId}"/>
               <apex:inputField label="Requestor Last Name" value="{!Case.Requestor_Last_Name__c}"/>
               <apex:inputField label="Status" value="{!Case.Status}"/>
               <apex:inputField label="Requestor Email" value="{!Case.Requester_Email__c}"/>
               <apex:inputField label="Subject" value="{!Case.Subject}"/>
               <apex:inputField label="Requestor Contact Number" value="{!Case.Requester_Contact_Number__c}"/>
               <apex:inputField label="Product" value="{!Case.Products__c}"/>
               <apex:inputField label="Business Impact" value="{!Case.Business_Impact__c}"/>
               <apex:inputField label="Components" value="{!Case.Componet__c}"/>
               <apex:inputField label="Business Urgency" value="{!Case.Business_Urgency__c}"/>
               <apex:inputField label="On Behalf of" value="{!Case.On_Behalf_of__c}"/>
               <apex:inputField label="Environment" value="{!Case.Environment__c}"/>
               <apex:inputField label="Description" value="{!Case.Description__c}"/>
           </apex:pageBlockSection>
            </apex:actionRegion>
            </apex:pageBlock>
       
             <!--<apex:commandButton value="Upload files" action="{!save}" reRender="theSection"/> -->
            <apex:outputPanel id="theSection" rendered="true">
            <apex:pageblock >
                 <apex:pageBlockSection title="Uploading the Attachment" collapsible="false" dir="LTR" columns="1">
        <div id="upload" class="upload">                                   
            <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>                            
        </div> 
         </apex:pageBlockSection>      
   		 </apex:pageBlock>
            </apex:outputPanel>       
     </apex:form>
</apex:page>

 
Best Answer chosen by Snehal Gaware 15
PrasathPrasath
Hi Snehal Gaware,
 
Yes, It will not autoFill because you are calling the autoCal() methond from VF Page when user changing the values,

Try this below code this will works fine.
 
//Get the Currently logged in community user contact id
    String cntId = [SELECT Id, ContactId, Name FROM User WHERE Id =: UserInfo.getUserId()].ContactId;
    caseObject.ContactId = cntId;
if(cntId != null){
//calling autofilling method
 autoCal();
}
Let me know if you have any questions, Mark this as a best answer if it solves your Problem.

Regards,
Prasath

 

All Answers

PrasathPrasath
Hi Snehal Gaware,

Try the below code
public AutoPopulateExample(ApexPages.StandardController controller){
    con = new Contact();
    caseObject = new case();
    caseObject = (Case)controller.getRecord();
    //Get the Currently logged in community user contact id
    String cntId = [SELECT Id, ContactId, Name FROM User WHERE Id =: UserInfo.getUserId()].ContactId;
    caseObject.ContactId = cntId;
    attachmentObject = new Attachment();
}

Let me know if you have any questions, Mark this as a best answer if it solves your Problem.

Regards,
Prasath
Snehal Gaware 15Snehal Gaware 15
Hi Prasath,
I have tried the above code. It is populating logged in user name, but fields related to it are not aupopulating now.
Before trying above code it was working fine, when i manually selecting contact name in field the related fields(Account, requester first name ,last name, phone) are autopopulating before saving the record.
PrasathPrasath
Hi Snehal Gaware,
 
Yes, It will not autoFill because you are calling the autoCal() methond from VF Page when user changing the values,

Try this below code this will works fine.
 
//Get the Currently logged in community user contact id
    String cntId = [SELECT Id, ContactId, Name FROM User WHERE Id =: UserInfo.getUserId()].ContactId;
    caseObject.ContactId = cntId;
if(cntId != null){
//calling autofilling method
 autoCal();
}
Let me know if you have any questions, Mark this as a best answer if it solves your Problem.

Regards,
Prasath

 
This was selected as the best answer
Snehal Gaware 15Snehal Gaware 15
Thanks Prasath