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
VKSINGHVKSINGH 

Updating custom field with http response

Hi All, need ur help,

I m firing a trigger by passing id of the object that extecute the http request n response.n my getting the response also.
but i have to refresh my detail page manually ,then that response value gets set to my customfield.
I want that..when i click save then data n response value both gets saved at that time.

My trigger code is this:

 

trigger test on OpportunityGAQ__c (after insert) {

 System.debug('Making future call to update account');
 for(OpportunityGAQ__c op:Trigger.New)
 {
  GAQUpdater.updateGAQ(op.Id, op.Name);    
    }
}

 

My Apex code is this :

 

public class GAQUpdater {
 
    public GAQUpdater(ApexPages.StandardController controller) {

    }


  //Future annotation to mark the method as async.
  @Future(callout=true)
  public static void updateGAQ(String id, String name) {
    public void save(){
    // system.debug('OPP:'+id);
    //construct an HTTP request
    HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
         
        string test = 'some string';
        req.setEndpoint('http://www.test.com.aspx');
        req.setMethod('POST');
        req.setBody(test);
     
        
        res = http.send(req);  
        System.debug(res.getBody());
   
         Dom.Document doc =(res.getBodyDocument());
        Dom.XMLNode envelope = doc.getRootElement();
        Dom.XmlNode[] temp = envelope.getChildElements();
       // String name = envelope.getChildElements('OrderID', null).getText();
             system.debug(temp.size());
          // print out specific elements
        // System.debug('OrderID: ' + name);
        
        if(temp.size()>0)
        {
            // Alternatively, loop through the child elements.
            // This prints out all the elements of the address
            for(Dom.XMLNode child : temp[1].getChildElements()) {
               
                if(child.getName()=='OrderID')
                {
                   // System.debug('G Ord Id: '+child.getText());
                  OpportunityGAQ__c op = new OpportunityGAQ__c(Id=id);  
                  op.Refrence_Number__c = child.getText();                              
                  update op;  
                }
                
                else if(child.getName()=='OrderStatusErrors'){
                    
                    for(Dom.XMLNode child2 : child.getChildElements()) {
                        
                        for(Dom.XMLNode child3 : child2.getChildElements()) {
                            
                            if(child3.getName()=='ErrorText'){
                                System.debug('Error Message: '+child3.getText());
                 }

         }

     }

  }

 }

}

}

}      

Every part of the Apex controller is fine cos m getting the response n also reading that response value(order no here)

but,the Red mark area is for updating the field,this is also working n update the field (to my custom reference field) but when i manually refresh the page.

 

I have created a custom object n have 2 field,name n id of field i am passing through trigger n in reference field for response to save. I am not using VF any Pages.

 

Pls help me to this.

 

 

Shailesh DeshpandeShailesh Deshpande
I dont think you can achieve it. The primary reason being, you are doing the callout in the future method which is asynchronous in nature, meaning that there is no gaurantee when your Http callout will be executed. I dont know if there is any alternative for you to achieve this. If using a vf page is fine for you, I think you might be able to work this out. However, the question remains how will you redirect the user to the VF page coz you cant override standard save button. So probably you could follow these steps: having a custom button(will be available only on detail page, not the edit page) and redirecting to the vf page. Make a callout and redirect back to the record.