+ Start a Discussion
KeithlarsKeithlars 

How to execute javascript from the onclick of a button

I have a VS page (below) that is populated with the contacts I've selected after clicking a custom button from a contact view.

 

On this page I have a lookup field to a custom object and I want to grap the value of this field and pass it to a javascript funtion (see below) when the user clicks the Next button on the form. 

 

I currently, have the javascript in a custom button on the contact object so I need to figure out where to put it and how to call it.

 

Thanks.

 

 

<apex:page standardController="Contact" extensions="contactExtension" recordSetVar="Contacts"> <!-- Do I add it as a static resource like below? If so,how do I call the function inside? -->

 

<script type="text/javascript" src="{!$Resource.addToOrgContact}"></script>

 

<apex:form > <apex:pageBlock id="thePageBlock"> <apex:pagemessages /> <apex:pageBlockSection title="Select Account Plan" id="accountPlan" columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Account Plan" for="ap"/> <apex:panelGrid columns="2"> <apex:outputText value="{!Contact.Account_Plan__c}" rendered="false"/> <apex:selectList id="ap" value="{!accountPlanID}" size="1" > <apex:selectOptions value="{!AccountPlanOptions}"/> </apex:selectList> </apex:panelGrid> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageBlockSection title="Selected Contacts"> <apex:pageBlockTable value="{!selected}" var="c" columnsWidth="300px, 300px, 100px"> <apex:column value="{!c.ID}" rendered="false" ></apex:column> <apex:column value="{!c.Firstname} {!c.Lastname}" headerValue="Name" ></apex:column> <apex:column value="{!c.Title}" headerValue="Title"></apex:column> <apex:column value="{!c.Region_Id__c}" headerValue="Region"></apex:column> </apex:pageBlockTable> </apex:pageBlockSection> <apex:pageBlockButtons location="both">

 

<!-- <apex:commandButton value="Next" action="{!addToOrgContact}"> <apex:param name="acctPlanID" value="{!this.acctPlanID}"/> </apex:commandButton>-->

 

<apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> </apex:page>

 

Javascript:

 

 

function addToOrgContact (acctPlanId) { {!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} var records= {!GETRECORDIDS($ObjectType.Contact)}; var newRecords = []; if (records[0] == null) { alert("Please select at least one record") } else { for (var n=0; n<records.length; n++) { var c = new sforce.SObject("Org_Contact_Analysis__c"); c.ContactID__c = records[n]; c.Account_Plan_ID__c = acctPlanId"; newRecords.push(c); } var errors = []; var result = sforce.connection.create(newRecords); if (result && result.length){ var numFailed = 0; var numSucceeded = 0; for (var i = 0; i < result.length; i++){ var res = result[i]; if (res && res.success == 'true'){ numSucceeded++; } else { var es = res.getArray("errors"); if (es.length > 0) { errors.push(es[0].message); } numFailed++; } } if (numFailed > 0){ alert("Failed: " + numFailed + "\nSucceeded: " + numSucceeded + " \n Due to: " + errors.join("\n")); } else { alert("Number of records added: " + numSucceeded); } } window.location.reload(); } }

 

 

 

 

 

gtuerkgtuerk

did you get an answer on this one?  I think you just use the onclick attribute of the commandButton.  I've got to do something similar in order to utilized the Flex/Ajax bridge...

 

onclick The JavaScript invoked if the onclick event occurs--that is, if the user clicks the command button.