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
John ClevelandJohn Cleveland 

Help updating record with HTTP Request callout from Trigger

I have a trigger that calls a class that makes an http callout.  I want to update the record that invoked the trigger with a value in the response from the http callout.  I'm getting a response back but need help getting it saved in the Number Stage field on the Order object.  Any help would be greatly appreciated.

 

Here's the trigger that calls the class:

 

This is the class that's being called:

 

 

AmanAman

 

Make your HTTP callout function

 

Public Static Integer postNumberCheck(string Number,id OrderId)

{

----- Your code

 

----

 

return numberReturn;

 

}

 

In  TRIGGER

trigger NumberCallout on ORDER__c (before insert) {
   
  for (ORDER__c order: Trigger.new) {
           
    If(order.Number__c!= '00000')
       {
      order.Number__c= checkNumberStage.postNumberCheck(order.Number__c,order.id);
       }
   }
}

AmanAman

 

Make your HTTP callout function

 

Public Static Integer postNumberCheck(string Number,id OrderId)

{

----- Your code

 

----

 

return numberReturn;

 

}

 

In  TRIGGER

trigger NumberCallout on ORDER__c (before insert) {
   
  for (ORDER__c order: Trigger.new) {
           
    If(order.Number__c!= '00000')
       {
      order.Number_Stage__c= checkNumberStage.postNumberCheck(order.Number__c,order.id);
       }
   }
}

John ClevelandJohn Cleveland

It's making me use void due to the @future callout?

SFFSFF

Yes - you cannot return anything from an @future call. Think about it - there's nothing on the other end waiting to receive that return value.

 

I also notice that you don't actually have an Order object in the postNumberCheck() method to update. Try this:

 

//parse out the response here
String numberReturn = 'false';
Dom.Document doc = res.getBodyDocument();
Dom.XMLNode node =  doc.getRootElement();
for(Dom.XMLNode child : node.getChildren())
{
   numberReturn = child.getChildElement('Ref',null).getChildElement('NUMBER',null).getText();
   system.debug(numberReturn);
}
if (orderId != null && numberReturn != null)
{
   Order__c o = new Order(Id = orderId, Number_Stage__c = numberReturn);
   update o;
}

 Good luck!