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
Олег СавинковОлег Савинков 

Pass the new inputfield value to javascript before writing to the database

Hello!
I have VF page.
Some code:
<apex:page standardController="CallReportHosp__c" extensions="CtrlCallHospEdit" id="CallsHospEditt">
<apex:sectionHeader title="Calls" subtitle="{!rep.Name}"/>
<apex:form id="myForm">
<script type="text/javascript">

function ConvertTofact() {  openPopup("/apex/ConvertHosp?id={!rep.Id}" ); }

window.addSelectedOrganisations = function(Organisations) {
            applyFilterByOrganisations(Organisations);}
</script>
<script>     
 function openLookup(BaseUrl,width,modified,searchstr)
        {
         console.log("{!Acc}");  console.log("{!rep.AccountID__r.BillingCity},{!rep.AccountID__r.BillingStreet}");  
           var receivedUrl=BaseUrl;
           var receivedwidth=width;
           var receivedmodified=modified;
           var receivedsearchstr=searchstr;
           
           var lookuptype=receivedUrl.substr(receivedUrl.length-3,3);
           if(receivedmodified=='1')
           {
               BaseUrl=BaseUrl+searchstr;
           }
           var urlArr=BaseUrl.split("&");
               //----If you are using this in inline editing then it would be var NxtSpltArr=urlArr[0].split("="); -----------
               var NxtSpltArr=urlArr[1].split("=");
               var txtfrmarr=NxtSpltArr[1];
           if(lookuptype=='001')
           {
             
           if(txtfrmarr.indexOf('organisation')> -1)
               {
               
                   BaseUrl="/apex/AccountsListforHosp?context=pharma_chain&list=noList&txt="+txtfrmarr;
                   BaseUrl=BaseUrl + "&frm=" + escapeUTF("{!$Component.myForm}");
                   BaseUrl=BaseUrl + "&lksearch=" + searchstr;

               }
           
               
           }
            if(lookuptype=='003')
           {
              
           
           if(txtfrmarr.indexOf('contact')> -1)
               {
                   BaseUrl="/apex/ContactsListforHosp?context=pharma_chain&list=noList&org="+"{!Acc}"+"&txt="+txtfrmarr;
                   BaseUrl=BaseUrl + "&frm=" + escapeUTF("{!$Component.myForm}");
                   BaseUrl=BaseUrl + "&lksearch=" + searchstr;
               }
               
           }
           openPopup(BaseUrl,"lookup",950,480,"width=1420,height=480,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no",true);
           
        }
</script>




   
    <apex:actionfunction name="applyFilterByOrganisations" action="{!applyFilterByOrganisations}" rerender="organisation" status="asLoadingSelectedOrganisations">
            <apex:param name="selectedOrganisationsStr" value="" assignto="{!selectedOrganisationsStr}" />
        </apex:actionfunction>
        
<apex:outputLink title="" value="/apex/ConvertHosp?id={!rep.Id}" style="font-weight:bold">Convert to fact</apex:outputLink>
        <apex:pageBlock mode="edit" id="pblock" >
            <apex:pageMessages id="message" />
            <apex:pageBlockButtons location="top">                                
                <apex:actionStatus id="mySaveStatus1">
                <apex:facet name="stop">
                <apex:commandButton value="Save" action="{!save}" rerender="pblock" status="mySaveStatus1"/>
                </apex:facet>
                <apex:facet name="start">
                <apex:commandButton value="Save" status="mySaveStatus1" disabled="true"/>
                </apex:facet>
                </apex:actionStatus>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            
            
<apex:outputText value="{!rep.Id}"/>
             
<!--            <apex:outputPanel rendered="{!IF($CurrentPage.parameters.theType = 'app', false, true)}" >-->
            <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Organisation" for="organisation"/>
                    <apex:inputfield value="{!rep.AccountID__c}" id="organisation"  required="true">
                         <apex:actionSupport event="onchange"  reRender="pblock" status="status" action="{!SelectService}" />
                    </apex:inputfield>
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Address"/>
                    <apex:outputText value="{!rep.AccountID__r.BillingCity},{!rep.AccountID__r.BillingStreet}" id="address" >
                    </apex:outputText>
                    
                </apex:pageBlockSectionItem>
 
                <apex:pageBlockSectionItem >


                <apex:outputLabel value="Contact" for="contacts"/>
                <apex:inputfield value="{!rep.ContactId__c}" id="contact" required="true" /> 



                </apex:pageBlockSectionItem>

               
                
.......              some code here .......
         
            
            
 </apex:pageBlockTable>
 </apex:outputPanel>
 </apex:pageBlockSection>          

        </apex:pageBlock>
        
        
    </apex:form>
    
</apex:page>
This page open from other page and i editing in this page field 'organisation'. Then I must pass new value of this field to javascript to have contacts from this organizations. And this is code of controller:
public with sharing class CtrlCallHospEdit implements ICallReportCtrl {
   public Boolean booleanValue {get;set;}
   public Boolean HaveAssCalls {get;set;}

    public  CallReportHosp__c rep{get;set;}
    public  Id Acc{get;set;}

    public List<CallReportHosp__c> AssCalls{get; set;}

    public Boolean isChina {get;set;}
    public Account rep2;
    private final User userInfoCheck;    
    
    public Set<Id> selectedOrganisationsIds  { get; set; }
    public String selectedOrganisationsStr { get; set; }



    private String userCountry;             // contains the country from the suer record to control visibility of sample management fields
    private Integer numberOfPromotedProducts;   // set in custom settings
      
    
    public Id singleDoctorId;
    public Id CallHospId;
    public Id userId;
    public Id CompanyId;
    public Id ContactId;
    public Id Parent;

    public String PlannedC;
    public String parameterValue;


    public CtrlCallHospEdit(ApexPages.StandardController controller) {
       isChina = UserInfo.getDefaultCurrency() == 'RUB' ? true : false;
       parameterValue = ApexPages.currentPage().getParameters().get('id');
       rep = [Select Id, Name, OwnerID, Date__c, ContactId__r.Name, CallTime__c, CallType__c, AccountID__r.BillingCity, AccountID__r.BillingStreet, ContactId__c, Field1_del__c, Duration__c, AccountID__c, AccountID__r.Name, Planned__c, ParentCallHOSP__c, Speciality_Hosp__c FROM CallReportHosp__c where id=:parameterValue];
       PlannedC=rep.Planned__c;   
       Parent=rep.ParentCallHOSP__c;   
       this.Acc=rep.AccountID__c; 
       getAssCalls();
//        userInfoCheck = [Select Id, Custom_Object_Country__c, CurrencyIsoCode, Is_Exempted_in_Call_Report_Validation__c from User where Id = :userId];
    }


some code here

public void SelectService(){ 
 this.Acc=rep.AccountID__c; System.debug('accvalue= '+Acc);
 rep2=[Select BillingCity, BillingStreet  FROM Account where id=:Acc];
 rep.AccountID__r.BillingCity=rep2.BillingCity;
 rep.AccountID__r.BillingStreet=rep2.BillingStreet;  
    }   

public PageReference save(){
        
        try {
            update rep;
        }catch (Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '"Something went wrong !"'));
        }
        PageReference pageRef = Page.CallsHospList;
        pageRef.setRedirect(true);
        return pageRef;
    }
    
 public PageReference cancel() {
        PageReference pageRef = Page.CallsHospList;
        pageRef.setRedirect(true);
        return pageRef;        
    }


}
I had old value in VF page, i see new value in field 'organisation', my method SelectService() working, i see new address ( address for new value of field 'organisaton'). But when i pass variable 'Acc' to js, i get old value of organization!
How i can pass NEW value to js ( but i can't do save() metod, because user may be not saving new values) to see contacts for new organizatin?