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
Geoffrey J FlynnGeoffrey J Flynn 

Javascript button to call method

Hi everyone,
I'm trying to learn on the fly here, my knowledge is more around triggers.

I'm trying to create a List button to automatically create three records, basically I'm trying to create my own grocery list and just automate a few items.

Class and Method:
Global class addGroceries {
    WebService static void addSomeGroceries(){
        List<Groceries__c> groc = new List<Groceries__c>();
        Groceries__c g1 = new Groceries__c(Name='Garlic', Category__c='1 - Fruit and Vegetables');
        groc.add(g1);
        
        Groceries__c g2 = new Groceries__c(Name='Onions', Category__c='1 - Fruit and Vegetables');
        groc.add(g2);
        
        Groceries__c g3 = new Groceries__c(Name='Pears', Category__c='1 - Fruit and Vegetables');
        groc.add(g3);
        insert groc;            
    }
}

Onclick Javascript button:
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
sforce.apex.execute("addGroceries","addSomeGroceries",{});
window.location.reload();

Error message
A problem with the OnClick JavaScript for this button or link was encountered:

args not specified

Everything I look up seems to reference passing parameters as part of the button, but I don't have anything to pass here.  
I'm assuming that it's something obvious

Thanks very much



 
PrakashbPrakashb
Hi Geoffrey,

Are you still facing this issue?? I recreated the same code and did not face any issue.

Thanks
Prakash 
Shashikant SharmaShashikant Sharma
Go to your class in brwoser and check if it has a package name. Put the package name before the class name like

sforce.apex.execute("packageName/addGroceries","addSomeGroceries",{});

 
Geoffrey J FlynnGeoffrey J Flynn
I was still having an issue.  Forgive my question, but is the Package Name the Namespace Prefix?  If so then no, it's not part of a package.

I converted it to a VF page instead and didn't have an issue.

Class and Button that do work:
public with sharing class addGroceries2 {
    public addGroceries2(ApexPages.StandardSetController stdController){}
    
    public  PageReference doSomething() {
        List<Groceries__c> groc = new List<Groceries__c>();
        Groceries__c g1 = new Groceries__c(Name='Garlic', Category__c='1 - Fruit and Vegetables');
        groc.add(g1);
        
        Groceries__c g2 = new Groceries__c(Name='Onions', Category__c='1 - Fruit and Vegetables');
        groc.add(g2);
        
        Groceries__c g3 = new Groceries__c(Name='Pears', Category__c='1 - Fruit and Vegetables');
        groc.add(g3);
        insert groc;
        
        return new ApexPages.Action('{!list}').invoke();
    }

}



Visualforce Page I'm calling from button instead
<apex:page standardController="Groceries__c" extensions="addGroceries2" recordSetVar="Test Records" action="{!doSomething}"/>

Still would love to know what I was doing wrong if the comparison helps anyone enough to get to the bottom of it
Shashikant SharmaShashikant Sharma
When I tried your code at my end it worked fine. One suggestion that I could give in your webservice method could be that your webservice method should return something instead of returning void.
Geoffrey J FlynnGeoffrey J Flynn
This may be a silly question, but is there any setting I would have to turn on to use Webservice calls?  I'm coming from the declarative side so in my mind there just must be a setting somewhere that I need to flip for things to work for you guys