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
nimdharnimdhar 

Calling a web method from a visual force page javascript function

Hi,

I am calling a web method from a javascript function in a visual force page. But that function is not getting called. Below is the  code

This is in my visual force page calling the javascript function
<apex:outputlink onclick="setLeadLocation('{!$CurrentPageReference.parameters.leadid}','{!location.id}');"> Attach </apex:outputlink>

This is my javascript function which calls the webmethod
function setLeadLocation(leadid,locationid)
{
   sforce.apex.execute('LeadServices','assignLocationToLead', {leadId:leadid,locationId:locationid});
 }

This is my web method

global class LeadServices
{
 
Webservice static void assignLocationToLead(ID leadId, ID locationId) {
   
      
Lead lead = [select Id from Lead where Id=:leadId];
      
lead.Location__c = locationId;
       
update lead;
       
return;
}

Can anyone tell is this not possible from a visual force page?

Thanks,
Nimdhar
dchasmandchasman
You could certainly use the ajax toolkit (we do not automatically establish the sforce object's login like scontrols do - and we never will BTW - and I suspect that is source of your problem) but there is a much much better way to do this directly in Visualforce. Simply switch to using <apex:commandLink> bound to an action method in your c ontroller that contains the 3 lines of code you currently have in your webservice method.


Message Edited by dchasman on 04-30-2008 12:19 PM
nimdharnimdhar
Hi Doug,

Yes I tried the same with commandLink having an action method as follows

<apex:pageblocksection title="Locations" id="locationresults">
<apex:pageblocklist value="{!locationresults}" var="location" rendered="{!NOT(ISNULL(locationresults))}">
<apex:column headerValue="Name">
             <apex:outputlink value="/{!location.id}">{!location.name}</apex:outputlink>
 </apex:column>
  <apex:column value="{!location.Address__c}" />
   <apex:column value="{!location.City__c}" />
   <apex:column value="{!location.State__c}" />
   <apex:column value="{!location.ZipCode__c}" />
<apex:column>
     <apex:commandLink value="Attach" action="{!attach}" oncomplete="closewindow()">
         <apex:param assignTo="{!locationid}" value="{!location.id}"/>
    </apex:commandLink>
</apex:column>
</apex:pageblocklist>
</apex:pageblocksection>


public PageReference attach()
{
Lead lead = [select Id,Location__c,LastName from Lead where Id= :System.currentPageReference().getParameters().get('leadid')];
System.debug('Location Id from Visual Force:' + locationid);
lead.Location__c=locationid;
update lead;
return null;
}
But the problem I am having is that the locationresults is a list and the commandLink is sending the correct location id only for the first record displayed and for the rest of the records it is passing null to the controller




Message Edited by dchasman on 04-30-2008 01:43 PM
dchasmandchasman
Ah - I did not realise this was from another post. Did you try doing this w/out using assignTo, e.g:

<apex:column>
     <apex:commandLink value="Attach" action="{!attach}" oncomplete="closewindow()">
         <apex:param name="locationId" value="{!location.id}"/>
    </apex:commandLink>
</apex:column>

public void attach(){
Lead lead = [select Id,Location__c,LastName from Lead where Id= :System.currentPageReference().getParameters().get('leadid')];
String locationId = System.currentPageReference().getParameters().get('locationId');
lead.Location__c = locationid;
update lead;
}

Even better - we typically bind directly to an action method on the object in the list itself, e.g.:

<apex:column>
     <apex:commandLink value="Attach" action="{!location.attach}" oncomplete="closewindow()"/>
</apex:column>

where location is an apex class (not sure if it already is) that wraps your sobject and adds in the attach() behavior...


nimdharnimdhar
yes I tried the first way but it did not work either.I did not understand your second way .
 Even better - we typically bind directly to an action method on the object in the list itself, e.g.:
<apex:column>
     <apex:commandLink value="Attach" action="{!location.attach}" oncomplete="closewindow()"/>
</apex:column>

where location is an apex class (not sure if it already is) that wraps your sobject and adds in the attach() behavior...

How do you access the parameter id in the attach method? Can you please explain it?

Thanks,
Nimdhar