+ Start a Discussion
Pedro SallesPedro Salles 

How to test a trigger?

Hello,
I am creating a code for an apex class to be triggered by a trigger.
Apex class:
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());
        }   
    } 
}

Trigger:
trigger CalloutTrigger on Contract (after insert, after update) {  
   contractCallout.PostCallout();  
 }
My trigger has the following error:
​“Line: 1, Column: 2 Unexpected token 'trigger' “

Could someone help me to test this trigger? I did not find a way for this.

Thanks