+ Start a Discussion
Alex LamoraAlex Lamora 

Error Occurred: An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

public class AuthCallout {

    @InvocableMethod
    public static void basicAuthCallout(){
    
    HttpRequest req = new HttpRequest();
    Http http = new Http();
    HTTPResponse res = http.send(req);

     //Endpoint and Method
    req.setEndpoint('https://test.com/api/Patients');
    req.setMethod('POST');
       
    //key and secret to be given by test 
    String accessid = '';
	String secretkey = '';
    string instanceid = '';
    
    //required header values
    req.setheader('Accept','application/vnd.test+json;version=1');
    req.setHeader('Content-Type','application/json'); 
	String getTime = string.valueOf(Datetime.Now());
        
        
        		  
        		  Map<string,object> Postdata =new Map<string,object>();
        		  Map<string,object> Document =new Map<string,object>();
        		  Map<String, object> patientdata = new Map<String, object>();
        		  Map<string,object> insurancedata = new Map<string,Object>();
        for(opportunity client: [SELECT accountid, first_name__c, Last_Name__c, Date_Of_Birth1__c, Client_SSN1__c, Gender__c, Client_Email__c, Client_Phone__c, Other_Pone__c, Mailing_City__c, Mailing_State__c, Mailing_Street__c, Mailing_Zip_Postal_Code__c, Insurance_Company1__c, Insurance_Phone_Number__c, Subscriber_Name__c, Subscriber_DOB__c, INS_ID__c, INS_Group__c, Client_Relationship_to_Primary__c, Primary_Policy_Holder_Employer__c from opportunity]){
              
                  insurancedata.put('insurance_company',client.Insurance_Company1__c);
                  insurancedata.put('insurance_phone',client.Insurance_Phone_Number__c);
                  insurancedata.put('subscriber_last_name',client.Subscriber_Name__c);
                  insurancedata.put('subscriber_DOB',client.Subscriber_DOB__c);
                  insurancedata.put('policy_no',client.INS_ID__c);
                  insurancedata.put('group_ID',client.INS_Group__c);
                  insurancedata.put('subscriber_relationship',client.Client_Relationship_to_Primary__c);
                  insurancedata.put('subscriber_employer',client.Primary_Policy_Holder_Employer__c);
                  patientdata.put('sending_app_patient_id',client.AccountId);
                  patientdata.put('first_name',client.first_name__c);
                  patientdata.put('last_name',client.Last_Name__c);
                  patientdata.put('ssn',client.Client_SSN1__c);
                  patientdata.put('gender',client.Gender__c);
   				  patientdata.put('email',client.Client_Email__c);
                  patientdata.put('phone',client.Client_Phone__c);
                  patientdata.put('alternate_phone',client.Other_Pone__c);
                  patientdata.put('address_city',client.Mailing_City__c);
                  patientdata.put('address_state',client.Mailing_State__c);
                  patientdata.put('address_street',client.Mailing_Street__c);
                  patientdata.put('address_zip',client.Mailing_Zip_Postal_Code__c);
                  patientdata.put('dob',client.Date_Of_Birth1__c);
                  patientdata.put('insurance_attributes',insurancedata);
                  document.put('data',patientdata);
                  document.put('recipient_id',instanceid);
                  document.put('sending_app_name','Salesforce');
                  postdata.put('document', Document);
        }
        
       
                  //seralize json string
                  string postdataser= JSON.serialize(postdata);
   				  req.setBody(postdataser);
                  
        			//encode MD5
        			String requestInput = postdataser;
					Blob requestBlob = Blob.valueOf(requestInput);
					Blob hash = Crypto.generateDigest('MD5', requestBlob);
					String requestSignature = EncodingUtil.convertToHex(hash);
      				req.setHeader('Content-MD5', requestsignature);
        
					//canonical string
       				 string canonicalstring =  'application/json' +'\n'+ requestsignature +'\n'+ '/api/patients' +'\n'+ getTime;
            		 string canonicalsecretkey = canonicalstring + secretkey;
        
            		//encode signature
            		Blob cankeyBlob = Blob.valueOf(canonicalsecretkey);
            		Blob hashSHA1 = Crypto.generateDigest('SHA1',cankeyblob);
        			String hashBase64SHA1 = EncodingUtil.base64encode (hashSHA1);
        
        
        			//authorization header
        			req.setHeader('Authorization', 'APIAUTH' + accessid +':'+ hashBase64SHA1);
        
 	   HTTPResponse res = http.send(req);
                    
                    }
                    }
This error occurs when I change the stage which invokes process builder flow to trigger the POST.

Please advise.