I am trying to invoke External Service using Apex from LWC, but I am receiving an error:
System.InvalidParameterValueException: Invalid output parameter type
Background:
- Using BankService External Service from Salesforce example Link
- In debug logs (LWC-Apex-External Service), I can view the response provided:
09:01:30.1 (691071203)|METHOD_ENTRY|[306]||ExternalService.BankService.getAccount(ExternalService.BankService.getAccount_Request)
09:01:31.912 (1912325812)|NAMED_CREDENTIAL_REQUEST|NamedCallout[Named Credential Id=0XA5P0000000850, Named Credential Name=Testing1, Endpoint=https://th-external-services.herokuapp.com/accounts/KevinWong, Method=GET, External Credential Type=EXTERNAL, HTTP Header Authorization=Method: Not set - Credential: Not set, Content-Type=text/plain; charset=UTF-8, Request Size bytes=-1, Retry on 401=True]
09:01:31.914 (1914580270)|NAMED_CREDENTIAL_RESPONSE|NamedCallout[Named Credential Id=0XA5P0000000850, Named Credential Name=Testing1, Status Code=200, Content-Type=application/json;charset=UTF-8, Response Size bytes=68, Overall Callout Time ms=833, Connect Time ms=5
09:01:31.936 (1936431294)|EXTERNAL_SERVICE_REQUEST|ExternalServiceSchemaType:OpenApi|ExternalServiceName:BankService|SystemVersion:3|Action:GET|InvocationType:Apex|InputParameters:{"accountName":"\"KevinWong\""}
09:01:31.936 (1937830818)|EXTERNAL_SERVICE_RESPONSE|ExternalServiceSchemaType:OpenApi|ExternalServiceName:BankService|SystemVersion:3|Action:GET|InvocationType:Apex|OutputParameters:{"200":"{\"id\":\"118\",\"name\":\"KevinWong\",\"type\":\"Insurer\",\"availableBal\":\"$0\"}","responseCode":"200"}|HttpResponseStatus:200
09:01:31.986 (1986415025)|EXCEPTION_THROWN|[10]|System.InvalidParameterValueException: Invalid output parameter type ObjectDataType{ServiceComponent{name=accountDetails, variant=null, version='(-1.-1.-1)', description=null, createdDate=2022-09-30 01:06:41, createdById=0052P000000b7f5, lastModifiedDate=2022-09-30 01:06:41, lastModifiedById=0052P000000b7f5, id=1Xo5P000000030q}, properties=[id,name,type,availableBal]} of parameter 200 for external service Apex operation getAccount and service ExternalService.BankService
09:01:31.986 (1986634722)|HEAP_ALLOCATE|[10]|Bytes:439
09:01:31.986 (1986694786)|METHOD_EXIT|[306]||ExternalService.BankService.getAccount(ExternalService.BankService.getAccount_Request)
09:01:31.986 (2014347599)|FATAL_ERROR|System.InvalidParameterValueException: Invalid output parameter type ObjectDataType{ServiceComponent{name=accountDetails, variant=null, version='(-1.-1.-1)', description=null, createdDate=2022-09-30 01:06:41, createdById=0052P000000b7f5, lastModifiedDate=2022-09-30 01:06:41, lastModifiedById=0052P000000b7f5, id=1Xo5P000000030q}, properties=[id,name,type,availableBal]} of parameter 200 for external service Apex operation getAccount and service ExternalService.BankService
(System Code)
- In debug logs (DeveloperConsole-Apex-External Service), the same code works fine:
08:47:53.2 (56388902)|METHOD_ENTRY|[9]||ExternalService.BankService.getAccount(ExternalService.BankService.getAccount_Request)
08:47:53.333 (333897089)|NAMED_CREDENTIAL_REQUEST|NamedCallout[Named Credential Id=0XA5P0000000850, Named Credential Name=Testing1, Endpoint=https://th-external-services.herokuapp.com/accounts/KevinWong, Method=GET, External Credential Type=EXTERNAL, HTTP Header Authorization=Method: Not set - Credential: Not set, Content-Type=text/plain; charset=UTF-8, Request Size bytes=-1, Retry on 401=True]
08:47:53.335 (335525050)|NAMED_CREDENTIAL_RESPONSE|NamedCallout[Named Credential Id=0XA5P0000000850, Named Credential Name=Testing1, Status Code=200, Content-Type=application/json;charset=UTF-8, Response Size bytes=68, Overall Callout Time ms=208, Connect Time ms=2
08:47:53.336 (336603029)|EXTERNAL_SERVICE_REQUEST|ExternalServiceSchemaType:OpenApi|ExternalServiceName:BankService|SystemVersion:3|Action:GET|InvocationType:Apex|InputParameters:{"accountName":"\"KevinWong\""}
08:47:53.336 (336653836)|EXTERNAL_SERVICE_RESPONSE|ExternalServiceSchemaType:OpenApi|ExternalServiceName:BankService|SystemVersion:3|Action:GET|InvocationType:Apex|OutputParameters:{"200":"{\"id\":\"118\",\"name\":\"KevinWong\",\"type\":\"Insurer\",\"availableBal\":\"$0\"}","responseCode":"200"}|HttpResponseStatus:200
08:47:53.2 (377787565)|HEAP_ALLOCATE|[10]|Bytes:120
08:47:53.2 (377832308)|HEAP_ALLOCATE|[10]|Bytes:5
08:47:53.2 (378348615)|HEAP_ALLOCATE|[10]|Bytes:148
08:47:53.2 (378427295)|HEAP_ALLOCATE|[10]|Bytes:8
08:47:53.2 (379503555)|METHOD_EXIT|[9]||ExternalService.BankService.getAccount(ExternalService.BankService.getAccount_Request)
- Code Snippet:
ExternalService.bankService service = new ExternalService.bankService();
ExternalService.bankService.getAccount_Request request = new ExternalService.bankService.getAccount_Request();
request.accountName = 'KevinWong';
try {
ExternalService.bankService.getAccount_Response response = service.getAccount(
request
);
System.debug('200 response id' + response.Code200.id);
System.debug(
'200 response balance' + response.Code200.availableBal
);
return JSON.serialize(response);
} catch (ExternalService.bankService.getAccount_ResponseException exc) {
System.debug('404 Response' + exc.Code404.errorMessage);
return 'Error: ' + exc.Code404.errorMessage;
}
- In case of POST Service error received from LWC-Apex-ExternalService is Invalid Input Parameters and the same works fine for DeveloperConsoel-Apex-ExternalService.
Interestingly the same code works fine when invoked using developer console. Appreciate any help or pointers to fix this issue?