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
Jack daniel 16Jack daniel 16 

Need help to call a web service using the future method.

Best Answer chosen by Jack daniel 16
Ajay K DubediAjay K Dubedi
Hi Jack,

To make a callout to external web service, You create an apex class with the future method marked with @future(callout=true)
Here is the example code:

public class SMSUtils {

    // Call async from triggers, etc, where callouts are not permitted.

    @future(callout=true)

    public static void sendSMSAsync(String fromNbr, String toNbr, String m) {

        String results = sendSMS(fromNbr, toNbr, m);

        System.debug(results);

    }

    // Call from controllers, etc, for immediate processing

    public static String sendSMS(String fromNbr, String toNbr, String m) {

        // Calling 'send' will result in a callout

        String results = SmsMessage.send(fromNbr, toNbr, m);

        insert new SMS_Log__c(to__c=toNbr, from__c=fromNbr, msg__c=results);

        return results;
    }
 }
For more information, please go to the below link:
https://www.pebibits.com/future-method-asynchronous-apex/ 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Ajay K DubediAjay K Dubedi
Hi Jack,

To make a callout to external web service, You create an apex class with the future method marked with @future(callout=true)
Here is the example code:

public class SMSUtils {

    // Call async from triggers, etc, where callouts are not permitted.

    @future(callout=true)

    public static void sendSMSAsync(String fromNbr, String toNbr, String m) {

        String results = sendSMS(fromNbr, toNbr, m);

        System.debug(results);

    }

    // Call from controllers, etc, for immediate processing

    public static String sendSMS(String fromNbr, String toNbr, String m) {

        // Calling 'send' will result in a callout

        String results = SmsMessage.send(fromNbr, toNbr, m);

        insert new SMS_Log__c(to__c=toNbr, from__c=fromNbr, msg__c=results);

        return results;
    }
 }
For more information, please go to the below link:
https://www.pebibits.com/future-method-asynchronous-apex/ 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
Jack daniel 16Jack daniel 16
Thanks Ajay.