+ Start a Discussion
buggs sfdcbuggs sfdc 

call a webservice from visualforce page custom button

HI,

Here am embeding a visualforce page on a standard table Account,so when some user selects the required information on inline visualforce page and clicks on the custom button "Check",that particular information has to be triggered to a webservice?
for this am trying to create some RestAPI with Post Action.
but some how my apex code is not working,can any one please look into it.Here is the code please go through it.
 
<apex:page standardController="Account">
    <style>
    <apex:outputText id="myId" />
    .alignfontright {
    text-align:right;
    }
    </style>  
<apex:form >

<apex:pageBlock title="Email Subscriptions">
<apex:panelGrid width="350px">
 <apex:pageBlockSection >


 <apex:outputText value="SPSS_ID" style="font-weight:800" />
<apex:inputText /> 
         <apex:outputText value="BNP Name" style="font-weight:800"/>
         <apex:inputText />
         <apex:outputText value="info"/>
         <apex:inputCheckbox />
         <apex:outputText value="ESI"/>
         <apex:inputCheckbox />
         <apex:outputText value="Quote"/>
         <apex:inputCheckbox />
        <apex:outputText value="PBNN"/>
         <apex:inputCheckbox />
         <apex:outputText value="EMI"/>
         <apex:inputCheckbox />
          <apex:outputText value="totaluncheck"/>
         <apex:inputCheckbox />

&nbsp;<apex:commandButton action="{!unisub}" value="check" style="font-weight:800"/>

     </apex:pageBlockSection>
</apex:panelGrid>
</apex:pageBlock>
</apex:form>
</apex:page>
 
@RestResource(urlMapping='/subscrip/*')
global with sharing class subscrip
{
 private Account A1;
 public EmailOptOut(ApexPages.StandardController stdController) {
        this.A1 = (Account)stdController.getRecord();
     }
  @Httppost
  global static Account unisub()
  {
      String Name;
      String SPSS_ID;
      string info;
      string ESI;
      string Quote;
      string PBNN;
      string EMI;
      string totaluncheck;
  
  
  Account acc = new Account();
    List<Account> accounts = [Select Name,SPSS_ID__C From Account Where SPSS_ID__C = : SPSS_ID];
    
              
        insert acc;
    
    return acc;
    
    
  }
}

 
Amit Singh 1Amit Singh 1
Hi,

I noticed that you are not using "subscrip" as extensions in your VF page and in Apex Class constructor is not valid you have used "EmailOptPut" but it must be "subscrip". Below are the update code for your reference and try again.

VF page code:
<apex:page standardController="Account" extensions="subscrip">
    <style>
    <apex:outputText id="myId" />
    .alignfontright {
    text-align:right;
    }
    </style>  
<apex:form >

<apex:pageBlock title="Email Subscriptions">
<apex:panelGrid width="350px">
 <apex:pageBlockSection >


 <apex:outputText value="SPSS_ID" style="font-weight:800" />
<apex:inputText /> 
         <apex:outputText value="BNP Name" style="font-weight:800"/>
         <apex:inputText />
         <apex:outputText value="info"/>
         <apex:inputCheckbox />
         <apex:outputText value="ESI"/>
         <apex:inputCheckbox />
         <apex:outputText value="Quote"/>
         <apex:inputCheckbox />
        <apex:outputText value="PBNN"/>
         <apex:inputCheckbox />
         <apex:outputText value="EMI"/>
         <apex:inputCheckbox />
          <apex:outputText value="totaluncheck"/>
         <apex:inputCheckbox />

&nbsp;<apex:commandButton action="{!unisub}" value="check" style="font-weight:800"/>

     </apex:pageBlockSection>
</apex:panelGrid>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class Code:
@RestResource(urlMapping='/subscrip/*')
global with sharing class subscrip
{
 private Account A1;
 public subscrip(ApexPages.StandardController stdController) {
        this.A1 = (Account)stdController.getRecord();
     }
  @Httppost
  global static Account unisub()
  {
      String Name;
      String SPSS_ID;
      string info;
      string ESI;
      string Quote;
      string PBNN;
      string EMI;
      string totaluncheck;
  
  
  Account acc = new Account();
    List<Account> accounts = [Select Name From Account LIMIT 10];
    
              
        insert acc;
    
    return acc;
    
    
  }
}

Another workaround is that you can create a new Apex class which will be used as extensions in VF page and a method which will call by command button and from that method you can call your REST API. 
 
Let me know if this resolves your problem.

Thanks,
Amit
 
buggs sfdcbuggs sfdc
HI Amit,

can i have your email address,somehow am not able to post my code here,am get some maintanence error,you can drop me a testmail at catchdevat@gmail.com,or reply me with your emailid.

Thanks!
Amit Singh 1Amit Singh 1

Hi,

It Seems that problem is with <apex:commandButton/> tab Use <apex:commandLink/> instead. Below is the code:

<apex:commandLink value="Save Changes" style="font-weight:800; text-decoration:none;" action="Emailsubget"  styleClass="btn">
    <apex:param name="Surveys" assignTo="{!Surveys}" value="Surveys" />
</apex:commandLink>
Also, you are assigning "Surveys" hard code value to "Surveys" controller variable. Is this right? I believe you need dynamic values.

try the above code and let me know if this helps.

Thanks,
Amit Singh.
buggs sfdcbuggs sfdc
yes i need dynamic values,how can i do that,if its something on standard or custom table we can query it,but here am not able to find it out,i googled a lot,can you please modify my code and make it work,with Account.Name even?
Amit Singh 1Amit Singh 1

Hi,

Try the below code. In the given code value passed to the controller will be "Account Name".

<apex:commandLink value="Save Changes" style="font-weight:800; text-decoration:none;" action="Emailsubget"  styleClass="btn">
    <apex:param name="Surveys" assignTo="{!Surveys}" value="{!Account.Name}" />
</apex:commandLink>

Thanks,
Amit Singh.
buggs sfdcbuggs sfdc
sorry no luck,we need to send the survey dynamic value to the webservice thats my request.
Amit Singh 1Amit Singh 1
Which values you need to send to the controller from VF page so that I could help you to sort out your problem. Also, check your debug log for making sure if the value is being passed or not.
 
Also, I have sent a test email to you on the given id.
 
Thanks,
Amit Singh.

 
buggs sfdcbuggs sfdc
I sent you a clear mail,you received it,please have a look.
Michael Hedrick 2Michael Hedrick 2
Hello,
I know this is an old Post but I am trying to accomplish the same functionality as what is discussed in this question.
Does this syntax work? <apex:page standardController="Account" extensions="subscrip">
 When I try this syntax states unknow constructor and wants to create a Controller.
Any suggestions would be appreciated.
Michael