Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sorry for a simple question. I'm totally new to Salesforce.

I want to pass JavaScript array to Apex to save its data in the Salesforce custom object. I want to know the best approach to do it without JavaScript remoting. Following is the JavaScript code.

$("#saveCalendar").click(function () {
        getDatesBackup = document.getElementById('selectedDates').options;
        for(var k=0; k<getDatesBackup.length; k++){
            allSelectedDates.push(getDatesBackup[k].text);
            console.log("Dates Backup : "+getDatesBackup[k].text);
        }
        console.log(allSelectedDates);
    });
share|improve this question
1  
salesforce.stackexchange.com/questions/24666/… .... May help you....! –  Shailesh Patil Jan 13 at 8:32
    
@ShaileshPatil In that question the value is coming from the VF. In my case this is only between JS and Apex. What should be the Apex to retrieve that array from JS? –  Superman Jan 13 at 8:52
    
You have to define an Apex variable and set the Javascript value to it. How to do that is written in the topic mentioned by @ShaileshPatil –  mast0r Jan 13 at 8:59

1 Answer 1

You can use ActionFunction for this. Here is sample for your reference.

Controller:

public class ActionFunctionCLS {
  public Account actObj{get;set;}
   PageReference prf= null;
    public ActionFunctionCLS(){
      actObj = new Account();
    }

    public pagereference Saverec(){
   if(actobj.Name !=''){
    insert actobj;     
   }
   else{
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please Enter Name.');
     ApexPages.addMessage(myMsg);      
   }
   if(actobj.id !=null){      
      // Send the user to the detail page for the new account.
      prf = new PageReference('/'+actobj.id);
      prf.setRedirect(true);       
   }
   return prf;
    }
}

VF Page:

    <apex:page controller="ActionFunctionCLS" id="pg" >
  <script>
   function recSave(){
    var accountType = document.getElementById('pg:fm:pb:pbs:actType').value;
    alert('accountType -->'+accountType);
    if(accountType != 'Prospect'){
     alert('You Should Select Prospect to Save the Record');
     return false;
    }
    else{
     saveAccount(); //this is the function name which calls our action function from java Script.
     return true;
    }
   }     
  </script> 

 <apex:form id="fm">
  <apex:actionfunction name="saveAccount" action="{!Saverec}" />
   <apex:pageBlock id="pb">
     <apex:pagemessages ></apex:pagemessages>
     <apex:pageblockButtons >
      <apex:commandButton value="Save" onclick="recSave();return false;"/>    
     </apex:pageblockButtons>       
     <apex:pageblockSection id="pbs">
       <apex:inputField value="{!actobj.Name}" id="actName"/>
       <apex:inputField value="{!actobj.type}" id="actType"/>
     </apex:pageblockSection>
   </apex:pageBlock>
 </apex:form>
</apex:page>

Note that we are calling Saverec Action from JS using saveAccount Action Function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.