+ Start a Discussion
David JoshuaDavid Joshua 

a problem with the OnClick JavaScript for this button or link was encountered : what did i do wrong ?

Hello, 

I've made a custom button with some JavaScript behind which calls an  API. 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

//alert('alert test');

var returnStatus = sforce.apex.execute("FranceOperations_RemoveStore", "resetSelectedFields", {id:"{!France_Operations__c.Id}"});

var returnSplited = returnStatus.toString().split("--");
if(returnSplited[0] != '0')
{
   alert(returnSplited[1]);
}

console.log("object published after modification "+"{!France_Operations__c.Id}");

location.reload();
And when I use it, here is the error that I get : 
a problem with the OnClick JavaScript for this button or link was 
encountered : 

{faultcode:'soapenv:Client', faultstring:'No service available for class 
'FranceOperation_RemoveStore'',}
The thing is the error state 'FranceOperation_RemoveStore' but the code behind my button calls a class named "FranceOperations_RemoveStore". There is a -s after 'operation'. I am 100% sure of that since I have checked it twice. Is there some kind of basic error I am missing ? It looks like I did not connect my button properly to my class and its method. Or maybe I am missing something else ?

If anybody has a better understanding of this then me, any advice or input would be greatly appreciated ! 
Thanks in advance !

 
Raj VakatiRaj Vakati
can you give me apex class "FranceOperation_RemoveStore"

You class should be GLobal class as below
 
global class FranceOperation_RemoveStore{  

    webservice static String resetSelectedFields(ID tid){
}
}

 
David JoshuaDavid Joshua
Hello Raj ! 

Here is my class : 
global class FranceOperations_RemoveStore {

	webservice static String resetSelectedFields(String id) {
// Get all the fields I need to change		
	France_Operations__c myOperation = [select id, My_first_field__c, My_second_field__c, My_other_fields__c from France_Operations__c where Id = :id ];

// Do some checks 
	if (condition) {
		do stuff
	} else {
		do stuf
	}
	
// Do some stuff
    my code    
    }
}

 
Raj VakatiRaj Vakati
can u give me complete code .?? Check debug logs once 
David JoshuaDavid Joshua
I don't really understand why but when I try to give you my complete awnser, this website gives me an 503 error. So I will try to you piece by piece. 

So those are the 2 classes I use : 
//class used by the button Remove From Vault
global class FranceOperations_RemoveStore {

    webservice static String resetSelectedFields(String id) {

        // I // recovering all my fields
        France_Operations__c myOperation = [select id, Mecanic_operation_LU__r.id, Mecanic_operation_LU__r.Name, Mecanic_operation_LU__r.Vault_Adgroup__c,Mecanic_operation_LU__r.Fixed_Date__c,
                            Voucher_Issuance_starting_date_fr__c, Mecanic_operation_LU__c, Store_Number_Vault__c, Vault_Adgroup__c, MCLU__c, 
                            MCLU1__c, MCLU2__c, MCLU3__c, MCLU4__c, MCLU5__c, MCLU6__c, MCLU7__c, MCLU8__c, 
                            MCLU11__c, MCLU12__c, MCLU13__c, MCLU14__c, MCLU15__c, MCLU16__c, MCLU17__c, MCLU18__c, 
                            Status__c, Retailer_fr__c, Order_date_fr__c, Command_Channel_fr__c, Store_City__c, Localization_fr__c, 
                            ID_Request_Number__c, ID_Request_Number_x__c, Eligibilit_VAULT__c, Adherent_email_fr__c, Operation_ID__c, Quarter__c, 
                            Voucher_issuance_ending_date_fr__c, Application_Fields_fr__c, Type_of_operation__c, Coupon_Type_fr__c, Operation_beneficiary_target_fr__c  from France_Operations__c where Id = :id ];

        // II // add some checks 
            //1 - Vault_Adgroup__c not null and begins with https
        if(myOperation.Vault_Adgroup__c != null && myOperation.Vault_Adgroup__c.startsWith('https'))
        {
            //return ('Return Adgroup from Vault - '+myOperation.Vault_Adgroup__c); // change return by debug
        } 
        else 
        {
            return ('ERROR. AdGroup is null or does not start with https');
        }
            // other checks to do
            //2 - Store SU? (cf FranceOperation)
            //3 - ...

        // III // my GET to recover my Adgroup from Vault
        FranceOperations_AdGroup currentAdGroup = FranceOperations_AdGroup.load(myOperation.Vault_Adgroup__c.replace('/#platform/adgroups/edit/','/adgroups/'));

        boolean res = currentAdGroup.removeStore(myOperation.Store_Number_Vault__c);
        // IV // my POST (inside function updateAG from class FranceOperations_AdGroup) 
        // first, update on our private tool. Then update/clear/null fields from SalesForce

        if(res)
        {
            res = currentAdGroup.updateAG();
            if(res)
                updateSFDC(myOperation); 
        }
        if(res)
            return '0--OK';
        else
            return '201--KO remove store'+ myOperation.Store_Number_Vault__c;
    }

    // my function to clear my selected fields on Salesforce
    public static void updateSFDC(France_Operations__c operation)
        {
                // exemple de champs à vider
                operation.Eligibilit_VAULT__c = null; // Variable does not exist : myOperation
                operation.MCLU__c = null; // Variable does not exist : MCLU__c
                operation.MCLU1__c = null; // Variable does not exist : myOperation
                // ajouter d'autres champs
                update operation; 
        
        }

}
David JoshuaDavid Joshua
and my 2nd class (public class FranceOperations_Adgroup) contains 2 method : (public boolean updateAG()) and (public boolean removeStore(String site_id))

updateAG is used to do my POST and removeStore is called to erase data while passing through my API. 

With that, I think you have it all. 

Basically, what I need to do is to click on that button. The button will call an API and datas will be updated on some server. Then when this update is done, fields in my sObject(s) in SalesForce will be cleared too. 
David JoshuaDavid Joshua

Here are the tracks I have followed to solve my issue : 

- with sharing / without sharing: not relevant here.

- global/public : not relevant either. The class called is already set on global. 

- namespace : though I thought that might be the reason of my error, my colleague told me we have no namespace so...

- package : the current track I am following. 

David JoshuaDavid Joshua
Ok, problem solved.