• Abhishek Singh 434
  • NEWBIE
  • 15 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies

As per my understanding ID of Action Plan Template is correct but still Process Builder is failing, I am using Action Template ID - 0PR6g000000LBr9GAG of 18 digits which is valid.

ERROR - 

User-added image
 

PROCESS BUILDER - 



Process Builder
User-added imageUser-added image

I have tried all possible ways to mock the apex callout but still couldn't get over this error - "Methods defined as TestMethod do not support Web service callouts" .. Please Help.

My Main Class - 
 
public class UpdateOFACResponse {
    
    @invocablemethod
    public static void makeCallout(List<Lead> leads)
    {
        if(!System.isFuture() && !System.isBatch())
        {
            saveResponse(leads[0].id);
        }
    }
    
    
    @future(Callout=true)
    public static void saveResponse(Id leadId)
    { 
        String Result;
        Integer Score;
        List<Lead> cName= [Select Company FROM Lead WHERE ID=:leadId limit 1];
        
        String companyName= String.valueOf(cName[0].Company);  
        String compName = EncodingUtil.urlEncode(companyName,'UTF-8').replace('+','%20') ;
        
        System.debug('Encoded compName>>>>'+ compName);
        
        String loginurl='https://search.ofac-api.com/v2?';
        String apikey='52c4b1bd-8bd8-450e-90e6-3ca9bf5bffaf';
        String format='json';
        String Alias ='false';
        String Name= compName;
        String MinScore='95';
        String Source='SDN,DPL';
        String type='Entity';
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(
            loginurl+
            'apiKey='+apikey+
            '&format='+format+
            '&includeAlias='+Alias+
            '&name='+Name+
            '&minScore='+MinScore+
            '&source='+Source+
            '&type='+type);
        
        request.setMethod('GET');
        System.debug('request>>'+request);
        HttpResponse response = http.send(request);
        
        System.debug('response>>'+response.getBody());
        
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) 
        {   
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            System.debug('results>>'+results);
            
            List<object> apiResponse = (List<Object>)results.get('matches');
             System.debug('apiResponse>>'+apiResponse);
            
            if(!apiResponse.isEmpty()){
             Result = (String)((Map<String,Object>)(((List<Object>)results.get('matches')).get(0))).get('name');
                
             Score = (Integer)((Map<String,Object>)(((List<Object>)results.get('matches')).get(0))).get('score'); 
                
            }
            //System.debug('Result-Name'+ Result);
            
            Id devRecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Onboarding').getRecordTypeId();
            if(devRecordTypeId!=NULL)
            {
                List<Lead> updateList = new  List<Lead>();
                try{ 
                    List<Lead> lstLead =[SELECT Id,OFAC_Response__c,OFAC_Score__c FROM Lead where RecordtypeId=:devRecordTypeId AND ID=:leadId];
                  
                    lstLead[0].OFAC_Response__c= Result;
                    lstLead[0].OFAC_Score__c = Score;
                    updateList.add(lstLead[0]);
                    
                    if(updateList.size()>0)
                    {
                        update updateList;
                    }
                     
                }
                catch(exception e)
                {
                    System.debug('Error occured'+ e.getMessage());
                }
            }
        }   
    }   
}

Test Class- 
 
@isTest(SeeAllData=false)
global class UpdateOFACResponse_Test {
    
    @TestSetup
    static void makeData(){
        RecordType rt = [select id,Name from RecordType where SobjectType='Lead' and Name='Partner Onboarding' Limit 1];
        
        Lead lead = new lead(
            FirstName = 'Test First Name',
            LastName = 'Sample Last Name',
            Phone = '555-555-5555',
            MobilePhone='+17072766457',
            Company = 'ananimforceTest',
            recordTypeId=rt.id
        );
        insert lead;     
    }
    
    @isTest
    static void testCallout() {
        
        List<Lead> lstLead = [SELECT Id, FirstName, LastName,Phone,MobilePhone FROM lead LIMIT 1];
        
        Test.startTest();
        //Set mock callout class.
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        // UpdateOFACResponse.makeCallout(lstLead);
        UpdateOFACResponse.saveResponse(lstLead[0].id);
        Test.StopTest();
        
    }
    
    global class MockHttpResponseGenerator implements HttpCalloutMock {
        // Implement this interface method
        global HTTPResponse respond(HTTPRequest req) {
            String fullJson = ('{error=false, matches=({additionalSanctions=(), addresses=({address1=Calle 50 Y 53 Marbella, address2=Edificio Plaza 2000, address3=Piso 7, city=Panama City, country=Panama, uid=0}), akas=(), citizenship=(), name=GLOBAL TECHNOLOGY IMPORT & EXPORT, S.A. (GTI), passports=(), programs=(SDNTK), score=96, sdnType=Entity, source=SDN, ...}), searchTerm=GLOBAL IMPORT LTD, sourcesUsed=({source=SDN}, {source=DPL})}');
            // Create a fake response
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody(fullJson);
            res.setStatusCode(200);
            return res;
        }
    } 
    
}

As per my understanding ID of Action Plan Template is correct but still Process Builder is failing, I am using Action Template ID - 0PR6g000000LBr9GAG of 18 digits which is valid.

ERROR - 

User-added image
 

PROCESS BUILDER - 



Process Builder
User-added imageUser-added image

I have tried all possible ways to mock the apex callout but still couldn't get over this error - "Methods defined as TestMethod do not support Web service callouts" .. Please Help.

My Main Class - 
 
public class UpdateOFACResponse {
    
    @invocablemethod
    public static void makeCallout(List<Lead> leads)
    {
        if(!System.isFuture() && !System.isBatch())
        {
            saveResponse(leads[0].id);
        }
    }
    
    
    @future(Callout=true)
    public static void saveResponse(Id leadId)
    { 
        String Result;
        Integer Score;
        List<Lead> cName= [Select Company FROM Lead WHERE ID=:leadId limit 1];
        
        String companyName= String.valueOf(cName[0].Company);  
        String compName = EncodingUtil.urlEncode(companyName,'UTF-8').replace('+','%20') ;
        
        System.debug('Encoded compName>>>>'+ compName);
        
        String loginurl='https://search.ofac-api.com/v2?';
        String apikey='52c4b1bd-8bd8-450e-90e6-3ca9bf5bffaf';
        String format='json';
        String Alias ='false';
        String Name= compName;
        String MinScore='95';
        String Source='SDN,DPL';
        String type='Entity';
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(
            loginurl+
            'apiKey='+apikey+
            '&format='+format+
            '&includeAlias='+Alias+
            '&name='+Name+
            '&minScore='+MinScore+
            '&source='+Source+
            '&type='+type);
        
        request.setMethod('GET');
        System.debug('request>>'+request);
        HttpResponse response = http.send(request);
        
        System.debug('response>>'+response.getBody());
        
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) 
        {   
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            System.debug('results>>'+results);
            
            List<object> apiResponse = (List<Object>)results.get('matches');
             System.debug('apiResponse>>'+apiResponse);
            
            if(!apiResponse.isEmpty()){
             Result = (String)((Map<String,Object>)(((List<Object>)results.get('matches')).get(0))).get('name');
                
             Score = (Integer)((Map<String,Object>)(((List<Object>)results.get('matches')).get(0))).get('score'); 
                
            }
            //System.debug('Result-Name'+ Result);
            
            Id devRecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Partner Onboarding').getRecordTypeId();
            if(devRecordTypeId!=NULL)
            {
                List<Lead> updateList = new  List<Lead>();
                try{ 
                    List<Lead> lstLead =[SELECT Id,OFAC_Response__c,OFAC_Score__c FROM Lead where RecordtypeId=:devRecordTypeId AND ID=:leadId];
                  
                    lstLead[0].OFAC_Response__c= Result;
                    lstLead[0].OFAC_Score__c = Score;
                    updateList.add(lstLead[0]);
                    
                    if(updateList.size()>0)
                    {
                        update updateList;
                    }
                     
                }
                catch(exception e)
                {
                    System.debug('Error occured'+ e.getMessage());
                }
            }
        }   
    }   
}

Test Class- 
 
@isTest(SeeAllData=false)
global class UpdateOFACResponse_Test {
    
    @TestSetup
    static void makeData(){
        RecordType rt = [select id,Name from RecordType where SobjectType='Lead' and Name='Partner Onboarding' Limit 1];
        
        Lead lead = new lead(
            FirstName = 'Test First Name',
            LastName = 'Sample Last Name',
            Phone = '555-555-5555',
            MobilePhone='+17072766457',
            Company = 'ananimforceTest',
            recordTypeId=rt.id
        );
        insert lead;     
    }
    
    @isTest
    static void testCallout() {
        
        List<Lead> lstLead = [SELECT Id, FirstName, LastName,Phone,MobilePhone FROM lead LIMIT 1];
        
        Test.startTest();
        //Set mock callout class.
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        // UpdateOFACResponse.makeCallout(lstLead);
        UpdateOFACResponse.saveResponse(lstLead[0].id);
        Test.StopTest();
        
    }
    
    global class MockHttpResponseGenerator implements HttpCalloutMock {
        // Implement this interface method
        global HTTPResponse respond(HTTPRequest req) {
            String fullJson = ('{error=false, matches=({additionalSanctions=(), addresses=({address1=Calle 50 Y 53 Marbella, address2=Edificio Plaza 2000, address3=Piso 7, city=Panama City, country=Panama, uid=0}), akas=(), citizenship=(), name=GLOBAL TECHNOLOGY IMPORT & EXPORT, S.A. (GTI), passports=(), programs=(SDNTK), score=96, sdnType=Entity, source=SDN, ...}), searchTerm=GLOBAL IMPORT LTD, sourcesUsed=({source=SDN}, {source=DPL})}');
            // Create a fake response
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody(fullJson);
            res.setStatusCode(200);
            return res;
        }
    } 
    
}