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
AlbertoEmpauaAlbertoEmpaua 

How to call apex function from Visualforce in Salesforce1

Hi everybody,
I have the following situation, We have in our org a JS button in the layout that call a method in an apex class to send an Email. But in salesforce1 this doesnt works then what I did is create a VF with the same code as the button and calling this VF with a custom Action. Now in Salesforce1 I can see the action but the only thing that do this action is open a blank page with the name of the VF and the buttons cancel and save but doesn't execute the class.
The code in the VF page is:
<apex:page standardController="Invoice__c">
	<apex:includeScript value="/soap/ajax/15.0/connection.js"/>
	<apex:includeScript value="/soap/ajax/15.0/apex.js"/>
	<script>
	
		var result = sforce.apex.execute('sendInvoice', 'send',{invoiceId:'{!Invoice__c.Id}'}); 
	
		if(result == ''){ 
			window.location.reload(); 
	
		}else{ 
			alert(result); 
		}
	</script>
</apex:page>
and the class is:
global with sharing class sendInvoice {
	
	webservice static String send(ID invoiceId) {
		
		Invoice__c inv = [Select Id, Status__c From Invoice__c Where Id =:invoiceId];
		
		if(inv != null && inv.Status__c =='Approved'){
			try {
				//....More code here
				return '';
			} catch(Exception e) {
				System.debug('@@@@ Error: ' + e);
				return 'Unexpected error, please contact with your Administrator';
			}	
		}else{
			return 'Invoice status should be Approved';
		}
	}
    
}
In salesforce classic this works fine but in Salesforce1 I can't find a solution.
Anyone can help me with this??

Thanks

 
Best Answer chosen by AlbertoEmpaua
William TranWilliam Tran
Try creating an extension class and put the logic in that class, in the constructor as needed.

<apex:page standardController="Invoice__c" extensions="LogicExecutionClass">

You may need to change your code since you are already in APEX, so no need to use AJAX

Thx

All Answers

William TranWilliam Tran
Try creating an extension class and put the logic in that class, in the constructor as needed.

<apex:page standardController="Invoice__c" extensions="LogicExecutionClass">

You may need to change your code since you are already in APEX, so no need to use AJAX

Thx
This was selected as the best answer
AlbertoEmpauaAlbertoEmpaua
Thanks William now is working, I was focused in doing that with JS and I don't think in doing in that way.