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
Pedro SallesPedro Salles 

How to make a trigger call a apex class to send data via post

Hello,

I'm new to salesforce and need to develop an apex class that captures contract data and sends it to a partner Webservice. It was recommended to use callout with triggers, but in the materials I found, it does not have much content on how to do it.

Can anybody help me?

Thank you
Raj VakatiRaj Vakati
Try like this 

Create an Apex class with callouts  .. update the URL and BODY and auth details and parse the response 

 
public class AccountUpdater {

  //Future annotation to mark the method as async.
  @Future(callout=true)
  public static void updateAccount(String id, String name) {
 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('{"name":"mighty moose"}');
        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());
        }
    	
    //check the response
    if (response.getStatusCode() == 200) {

      //update account
    
    } else {
      System.debug('Callout failed: ' + res);
    } 
  }
}



Invoke it from the trigger 
 
trigger descriptionUpdater on Account (after insert) {

  System.debug('Making future call to update account');
  for (Account acc : Trigger.New) {
    

    AccountUpdater.updateAccount(acc.Id, acc.Name);
  }

}

Refer this link

https://trailhead.salesforce.com/en/modules/apex_integration_services/units/apex_integration_rest_callouts
https://salesforcelearningpoint.wordpress.com/2016/11/08/future-method-invocation-from-apex-trigger/​
Pedro SallesPedro Salles
Hello Raj V, thank you for the help you have given me in the questions i asked.

Following your tips, i set up my apex class and my trigger:

Apex:
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();  
 }
But I had some doubts:

How to pass the contract ID through Trigger? This Id i will use in my query condition.
How can I test my trigger? When I try to test through the Developer Console it gives me the following error message:
"Line: 1, Column: 2
Unexpected token 'trigger' "

I'm new to Salesforce development and i do not find many references to this kind of service.
I already did the Trailhead, but it limits me in some points.
Could you help me with some kind of material about it?

Thanks a lot for the help.