0

I have a apex Class which calls external ERP system and create/updates records there. The apex Class is like below

public class createSegment {
    public class getAzureToken{
        public  String getToken(){
            String clientId = 'xxxx';
            String clientSecret = 'xxxx';
            String tenant_id = 'xxx';
            String resource = 'https://dev.dynamics.com';

            String reqbody = 'grant_type=client_credentials&client_id='+clientId+'&client_secret='+clientSecret+'&tenant_id='+tenant_id+'&resource='+resource;

            Http h = new Http();
            HttpRequest req = new HttpRequest();
            req.setBody(reqbody);
            req.setMethod('POST');
            req.setEndpoint('https://login.microsoftonline.com/xxxx.org/oauth2/token');
            HttpResponse res = h.send(req);

            deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
            String atoken = resp1.access_token;
            return atoken;
        }
    }

    public class JSON2Apex {
        public String dataAreaId;
        public String SegmentCode;

        public JSON2Apex parse(String json) {
            return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
        }
    }

    public Integer createSeg() {
        JSON2Apex js = new JSON2Apex();
        getAzureToken gt= new getAzureToken();

        js.dataAreaId = 'xxxxx';
        js.SegmentCode ='Test from SF-Test1';

        String jsonBody = json.serialize(js); 
        String bearerToken = gt.getToken();

        Http http1 = new Http();
        HttpRequest req1 = new HttpRequest();

        req1.setEndpoint('https://dev.dynamics.com/data/Acc');
        req1.setMethod('POST');
        req1.setTimeout(20000);
        req1.setHeader('Authorization','Bearer '+bearerToken);
        req1.setBody(jsonBody);
        req1.setHeader('Content-Type', 'application/json;charset=UTF-8');
        HttpResponse res1 = http1.send(req1);
        return res1.getStatusCode();  
    }
    public class deserializeResponse
        {
            public String token_type;
            public String expires_in;
            public String ext_expires_in;
            public String expires_on;
            public String not_before;
            public String resource;
            public String access_token;
        }
}

For POC I have just hard coded the values. This works perfectly fine when I tried to execute is through Anonymously. Now I am trying to call this Apex Class from the Trigger like

trigger createSegments on Account (after insert) {
createSegment h = new createSegment();
h.createSeg();       
}

When the trigger is executed I get Error: Invalid Data. Review all error messages below to correct your data. Apex trigger createSegments caused an unexpected exception, contact your administrator: createSegments : execution of AfterInsert caused by: System.CalloutException: Callout from triggers are currently not supported.: Class.createSegment.getAzureToken.getToken: line 16, column 1

I tried to use the @future (callout=true) looks it works only with static method.Can anyone please help me how to handle this issue.

2
  • 1
    You've basically answered your own question--you need a static future method to call.
    – sfdcfox
    May 26, 2020 at 18:11
  • 1
    If you search SFSE for "trigger callout" you will find hundreds of questions discussing patterns around managing callouts from Apex triggers. I've linked one example here.
    – David Reed
    May 26, 2020 at 18:15

0

Browse other questions tagged .