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
Div SnkDiv Snk 

need help with apex callout

Hi, I am  making a callout to an external site to populate certain fields on the Account object based on the response of the the external site. There is a custom button on the Account object which is working on javascript and calls an apex controller which makes the callout.

The format of the apex class is :

global class ApexCallout {
    webservice static String xyz(Id accountId){
        Account accountDetails = [SELECT Id, Number1__c, Country1__c FROM Account WHERE Id = :accountId];
        // Request logic here
        // Response logic here; the response comes without any issue
        accountDetails.Registered_Name__c = response.name;
        accountDetails.Registered_Address__c = response.address;
        accountDetails.Request_Date__c = response.requestDate;
        System.debug(response);  
       return String.valueOf(response.valid);  
            
    }
    }

Valid is a boolean here.

The response of this callout is coming without any issue. Registered name, Registered Address and Request Date are the fields on the Account object which should be populated based on the response as soon as I click on that custom button on the Account detail page.
However, in my case the button is working, the response is coming but these fields are not getting populated on the detail page. Can someone please let me know what I am missing here.
Best Answer chosen by Div Snk
Dilip_VDilip_V
Hello,

I think you are not updating the account.
 
global class ApexCallout {
    webservice static String xyz(Id accountId){
        Account accountDetails = [SELECT Id, Number1__c, Country1__c FROM Account WHERE Id = :accountId];
        // Request logic here
        // Response logic here; the response comes without any issue
        accountDetails.Registered_Name__c = response.name;
        accountDetails.Registered_Address__c = response.address;
        accountDetails.Request_Date__c = response.requestDate;
Update accountDetails;//Updates account
        System.debug(response);  
       return String.valueOf(response.valid);  
            
    }
    }

Let us know if you have any issues.
Mark it as best answer if it works.

Thanks.

All Answers

Dilip_VDilip_V
Hello,

I think you are not updating the account.
 
global class ApexCallout {
    webservice static String xyz(Id accountId){
        Account accountDetails = [SELECT Id, Number1__c, Country1__c FROM Account WHERE Id = :accountId];
        // Request logic here
        // Response logic here; the response comes without any issue
        accountDetails.Registered_Name__c = response.name;
        accountDetails.Registered_Address__c = response.address;
        accountDetails.Request_Date__c = response.requestDate;
Update accountDetails;//Updates account
        System.debug(response);  
       return String.valueOf(response.valid);  
            
    }
    }

Let us know if you have any issues.
Mark it as best answer if it works.

Thanks.
This was selected as the best answer
Div SnkDiv Snk
Thank you, the issue is resolved now :)