0

I'm getting the Error stated in the title above in my code. The method is run in a flow that passes a newly created opportunity id to it. Here's the code:

global class NewOppUTS {
@InvocableMethod(label='Get UTS Number' description='Retrieves the UTS number from the Account information' category='Account')
global static List<String> main(List<Id> ids) {

    String num;
    UTSIntegration uts = new UTSIntegration();

    Opportunity opp = new Opportunity();
    Account a = new Account();
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    HttpResponse response;
    request.setEndpoint('rotaryairlock--rapilot2.lightning.force.com/services/data/v48.0/sobjects/opportunity/'+ids[0]);
    request.setHeader('Content-Type', 'application/json; charset=UTF-8');
    request.setHeader('Accept', 'application/json');
    request.setMethod('GET');
    try {
        response = http.send(request);
        if (response.getStatusCode() == 200 || response.getStatusCode() == 201) {
            System.debug(response.getBody());
            Map<String, Object> results = new Map<String, Object>();
            results = (Map<String, Object>) json.deserializeUntyped(response.getBody());
            opp.id = (id) results.get('Id');
            opp.AccountId = (id) results.get('AccountId');
        }
    }
    catch (exception e) {
        System.debug('ERROR: '+e.getMessage());
    }

    http = new Http();
    request = new HttpRequest();
    response = new HttpResponse();
    request.setEndpoint('rotaryairlock--rapilot2.lightning.force.com/services/data/v48.0/sobjects/account/'+opp.AccountId);
    request.setHeader('Content-Type', 'application/json; charset=UTF-8');
    request.setHeader('Accept', 'application/json');
    request.setMethod('GET');
    try {
        response = http.send(request);
        if (response.getStatusCode() == 200 || response.getStatusCode() == 201) {
            System.debug(response.getBody());
            Map<String, Object> results = new Map<String, Object>();
            results = (Map<String, Object>) json.deserializeUntyped(response.getBody());
            a.id = (id) results.get('Id');
            a.ShippingStreet = (String) results.get('ShippingStreet');
            a.ShippingCity = (String) results.get('ShippingCity');
            a.ShippingState = (String) results.get('ShippingState');
            a.ShippingPostalCode = (String) results.get('ShippingPostalCode');
            a.ShippingCountry = (String) results.get('ShippingCountry');
            a.Name = (String) results.get('Name');
            a.Shipping__c = (String) results.get('Shipping__c');
        }
    }
    catch (exception e) {
        System.debug('ERROR: '+e.getMessage());
    }

    uts.newSession();
    uts.postShipment();

    http = new Http();
    request = new HttpRequest();
    response = new HttpResponse();
    request.setEndpoint('rotaryairlock--rapilot2.lightning.force.com/services/data/v48.0/sobjects/opportunity/'+opp.id);
    request.setBody('{"UTS_Inbound_BOL__c":"'+uts.sinBolNumber+'","UTS_Outbound_Tracking__c":"'+uts.sinUtsId+'"}');
    request.setHeader('Content-Type', 'application/json; charset=UTF-8');
    request.setHeader('Accept', 'application/json');
    request.setMethod('POST');
    try {
        response = http.send(request);
        if (response.getStatusCode() == 201 || response.getStatusCode() == 200) {
            System.debug('Successfully updated the record');
        }
        else {
            System.debug('Record was not updated');
        }
    }
    catch (exception e) {
        System.debug('An exception occured when trying to connect to salesforce');
        System.debug('ERROR: '+e.getMessage());
    }

    uts.endSession();

    num = uts.sinBolNumber;
    List<String> numbers = new List<String>();
    numbers.add(num);

    return numbers;
}}

The UtsIntegration is a class I had previous built that connects to their api service and retrieves shipping information (like the address). Uts.newsession, Uts.postshipment, and Uts.endsession are making a web callout each as well. I had this same error before When I tried to update the opportunity via dml. I've seen online that you can't have a dml statement and web callout in the same method so I switched the update opp to a rest callout, but I am still getting that same error.

4

0

Browse other questions tagged .