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
Abhishek KhoslaAbhishek Khosla 

Custom Button: Call Apex Class with JavaScript from Button

Hi,

I am trying to call a Static method from Custom Button but getting the error " missing ) after argument List. Can somebody guide me how I can recitfy the same 

Here is my Apex code :
global class ProductGroupFamilyAddOrderTarget {
    
    webservice static void addOrderTarget(){
        List<Sales_Target__c> newSalesTarget = new List<Sales_Target__c>();
        
        List<Product_Group__c> productGrpFamilywithSeries =[SELECT Id,Name,Short_Name__c,
                                                            (SELECT Id,Parent__c,Short_Name__c FROM Children_Groups__r WHERE Group_Level__c='Series' AND isActive__c = True ) 
                                                            FROM Product_Group__c WHERE Group_Level__c='Family'AND isActive__c = True];
        
        SFDC_Fiscal_Year__c currentFiscalYear = [SELECT Id,Name FROM SFDC_Fiscal_Year__c WHERE Current__c= true];
        List<Region__c> region = [SELECT Id FROM Region__c WHERE Is_Active__c = true AND isRegion__c = true];
        List<Sales_Target__c> existOrderTarget =[SELECT Id,Product_Group__c,Region__c,SFDC_Fiscal_Year__c FROM Sales_Target__c WHERE Region__c IN :region AND SFDC_Fiscal_Year__c =: currentFiscalYear.Id];
        Map<String,Id> TargetOrder = new Map<String,Id>();
        if(existOrderTarget !=null && !existOrderTarget.isEmpty()){
            for(Sales_Target__c st : existOrderTarget){
                TargetOrder.put(String.valueOf(st.Product_Group__c)+String.valueOf(st.Region__c),st.Id);
            }
            
        }
        System.debug('Is there any Target Group Created by Error '+TargetOrder.values());
        if(productGrpFamilywithSeries != null && !productGrpFamilywithSeries.isEmpty()){
            for(Product_Group__c prdGrp : productGrpFamilywithSeries){
                if(prdGrp.Children_Groups__r !=null && !prdGrp.Children_Groups__r.isEmpty()){
                    for(Product_Group__c prdGrpChild : prdGrp.Children_Groups__r){
                        if(region !=null && !region.isEmpty()){
                            for(Region__c r : region){
                                if(!TargetOrder.containsKey(String.valueOf(prdGrpChild.id)+String.valueOf(r.Id))){
                                    Sales_Target__c newOrderTarget = new Sales_Target__c();
                                    newOrderTarget.Name = 'Newtarget2020';
                                    newOrderTarget.Target_Type__c ='Product Group';
                                    newOrderTarget.Product_Group__c = prdGrpChild.Id;
                                    newOrderTarget.Region__c = r.Id;
                                    newOrderTarget.SFDC_Fiscal_Year__c = currentFiscalYear.Id;
                                    newSalesTarget.add(newOrderTarget);
                                }
                            }
                        }
                    }
                }
            }
        }
        
        if(newSalesTarget !=null && !newSalesTarget.isEmpty()) insert newSalesTarget;
    }
    
}


And My Custom Button 

{!REQUIRESCRIPT("/soap/ajax/47.0/connection.js")}{!REQUIRESCRIPT("/soap/ajax/47.0/apex.js")}

var result = sforce.apex.execute("ProductGroupFamilyAddOrderTarget","addOrderTarget",{});
alert(result);
Soyab HussainSoyab Hussain
Hi Abhishek Khosla.

According to me, you are getting the error in apex controller please check your debug log after click that custom button. 

If the error in apex class, solve that then it will work fine.

Regards,
Soyab