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
Sandesh D GanjareSandesh D Ganjare 

take emp id from contact and dump there in master-detail field ?

I have custom field emp id in contact, the object leave appliction has master detail relationship on contact. i want to take emp id from contact and dump there in master-detail field of leave application "Employee ID"

Apex Controller :
public class myControllerExtension {
      
    public myControllerExtension(ApexPages.StandardController stdController) {
        //this.acct = (New_Application__c)stdController.getRecord();

    }
public decimal getEmpId() {
                User u = [select FirstName, Lastname from User where Id = :UserInfo.getUserId()];
                contact c = [select id, Employee_ID__c, Name from contact where (FirstName = :u.FirstName AND Lastname = :u.Lastname)];
                // return c.Employee_ID__c;
              return c.Employee_ID__c;
    }    
}

VF page :-
<apex:page standardController="New_Application__c" extensions="myControllerExtension">
 
   <apex:form >
   
        <apex:pageBlock title="Leave Application Details">
          <apex:pageBlockSection columns="1">               
              <apex:outputText label="Employee  Name  "> {!$User.FirstName} {!$User.LastName}</apex:outputText>
                       <apex:inputField value="{! New_Application__c.Employee_ID__c}" >{!EmpId}  </apex:inputField>
                       <apex:inputField value="{! New_Application__c.Subject__c}" />
               <apex:inputField value="{! New_Application__c.Application_Type__c}" />  
               <apex:inputField value="{!New_Application__c.Start_Date__c}" />
               <apex:inputField value="{!New_Application__c.End_Date__c}" />
               <apex:inputField value="{!New_Application__c.Reporting_PM__c}" />
            </apex:pageBlockSection>
             <CENTER>
            <apex:commandButton action="{!Save}" value="Save" />
            <apex:commandButton dir="LTR" action="{!cancel}" value="Cancel" /> 
            </CENTER>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>
Abhishek BansalAbhishek Bansal
Hi ,

Please update your controller code from below code :

public class myControllerExtension {
    
    private New_Application__c application;
    public myControllerExtension(ApexPages.StandardController stdController) {
        this.application = (New_Application__c)stdController.getRecord();
    }
    public PageReference save(){
    
        try{
            insert application;
            populateEmpId();
            return new PageReference('/'+application.id);
        }
        catch(Exception e){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, e.getMessage()));
            return null;
        }
    }
    public void populateEmpId() {
        User u = [select FirstName, Lastname from User where Id = :UserInfo.getUserId()];
        contact c = [select id, Employee_ID__c, Name from contact where (FirstName = :u.FirstName AND Lastname = :u.Lastname) LIMIT 1];
        c.Employee_ID__c = application.Employee_ID__c;
        update c;
    }    
}

Thanks,
Abhishek