+ Start a Discussion
Pedro SallesPedro Salles 

Problem with static can only be used on methods of a top level type error salesforce

Hi, 

I have a problem with my code:

public class contractCallout {@future (callout=true)
    public static void PostCallout(String Id) {        
        
        Contract result =  [SELECT Id from contract where Id = '8004D000000HAdIQAW'];
        
        JSONGenerator gen = JSON.createGenerator(true);    
        gen.writeStartObject();      
        gen.writeStringField('Id', result.Id);
        /*gen.writeStringField('Origem_Contrato__c',result.Origem_Contrato__c);
        gen.writeStringField('Cotacao__r.Contact.FirstName',result.Cotacao__r.Contact.FirstName);
        gen.writeStringField('Cotacao__r.Contact.LastName',result.Cotacao__r.Contact.LastName);    */    
        gen.writeEndObject();    
        String jsonS = gen.getAsString();
        System.debug('jsonMaterials'+jsonS);
        
        // Sening the http body with JSON 
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody(jsonS);
        HttpResponse response = http.send(request);
        // Parse the JSON response
        if (response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' +
                response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
        }   
    } 
}


when I am going to take a test, it presents the following error: static can only be used on methods of a top level type 

Can someone help me?

Thanks
Best Answer chosen by Pedro Salles
Raj VakatiRaj Vakati
You are code is looks good and working in my dev org .. 
how are you testing ?? Go to devloper console and execute this code 
contractCallout.PostCallout('80041000000RFXX');

or from the trigger 
contractCallout.PostCallout(Trigger.new[0]);

 

All Answers

Raj VakatiRaj Vakati
You are code is looks good and working in my dev org .. 
how are you testing ?? Go to devloper console and execute this code 
contractCallout.PostCallout('80041000000RFXX');

or from the trigger 
contractCallout.PostCallout(Trigger.new[0]);

 
This was selected as the best answer
Pedro SallesPedro Salles
Thanks for the help Raj V
I'm starting in the development within Salesforce now.
I needed to send this information through a trigger, but I do not know how to structure the code, would you know how to do this?