0

Calling a REST API apex class from apex trigger and seeing below errors.

Response:

System.HttpResponse[Status=Internal Server Error, StatusCode=500] Response Body: {"status":"error", "message":"List has no rows for assignment to SObject"}

Response Status Code: 500

Apex Trigger publishing platform event:

trigger TeamMemberTrigger on fHCM2__Team_Member__c (after insert) { List<TeamMemberEvent__e> eventList = new List<TeamMemberEvent__e>(); system.debug('Trigger.New===>'+Trigger.New); for (fHCM2__Team_Member__c tm : Trigger.New) { TeamMemberEvent__e eventInstance = new TeamMemberEvent__e( TeamMemberId__c = tm.Id ); eventList.add(eventInstance); } system.debug('eventList===>'+eventList); if (!eventList.isEmpty()) { EventBus.publish(eventList); } } 

Apex Trigger calling queueable apex

trigger TeamMemberEventTrigger on TeamMemberEvent__e (after insert) { System.debug('TeamMemberEventTrigger invoked'); List<Id> teamMemberIds = new List<Id>(); for (TeamMemberEvent__e event : Trigger.New) { teamMemberIds.add(event.TeamMemberId__c); } System.enqueueJob(new PostTeamMemberToCertinia(teamMemberIds)); } 

Apex Class :

public class PostTeamMemberToCertinia implements Queueable, Database.AllowsCallouts { private List<Id> teamMemberIds; public PostTeamMemberToCertinia(List<Id> ids) { this.teamMemberIds = ids; } List<fHCM2__Team_Member__c> teamMembers = new List<fHCM2__Team_Member__c>(); teamMembers = [SELECT fHCM2__Unique_Id__c, fHCM2__First_Name__c, fHCM2__Surname__c, fHCM2__Email__c FROM fHCM2__Team_Member__c WHERE Id IN :teamMemberIds]; // Create a list of wrapper objects List<TeamMemberWrapper> tmWrappers = new List<TeamMemberWrapper>(); for (fHCM2__Team_Member__c teamMember : teamMembers) { TeamMemberWrapper tmWrapper = new TeamMemberWrapper( teamMember.fHCM2__Unique_Id__c, teamMember.fHCM2__First_Name__c, teamMember.fHCM2__Surname__c, teamMember.fHCM2__Email__c ); tmWrappers.add(tmWrapper); } // Convert the list of wrapper objects to JSON String requestBody = JSON.serialize(tmWrappers); // Create the HTTP request HttpRequest req = new HttpRequest(); req.setEndpoint('callout:CertiniaNamedCred/services/apexrest/ReceiveTeamMember/'); //services/apexrest/ReceiveTeamMember req.setMethod('POST'); req.setHeader('Content-Type', 'application/json'); req.setBody(requestBody); // Send the request Http http = new Http(); HttpResponse res = new HttpResponse(); try { res = http.send(req); System.debug('Response ===> ' + res); System.debug('Response Status: ' + res.getStatus()); System.debug('Response Body: ' + res.getBody()); System.debug('Response Status Code: ' + res.getStatusCode()); System.debug('Response Header Keys: ' + res.getHeaderKeys()); } catch(Exception e) { System.debug('Error: ' + e.getMessage()); System.debug('Error Line Number: ' + e.getLineNumber()); System.debug('Error Stack Trace String: ' + e.getStackTraceString()); System.debug('Error Type Name: ' + e.getTypeName()); System.debug('Error Cause: ' + e.getCause()); } } public class TeamMemberWrapper { public String tmUniqueId; public String tmFirstName; public String tmSurname; public String tmEmail; public TeamMemberWrapper(String uId, String fName, String sName, String emailAddr) { tmUniqueId = uId; tmFirstName = fName; tmSurname = sName; tmEmail = emailAddr; } } } 
5
  • I suggest the fault lies in some other code on your org (or the target external service) since there doesn't seem to be code that would cause this error in what you posted. BTW, you are generating unnecessary garbage by initializing your lists before then assigning to them. Note, too, that calling a future from a trigger is a fragile solution. Read this article for more. Commented Aug 18, 2024 at 15:18
  • Hello Phil, Thank you. Will work further on the class. Without trigger, How do I invoke the class upon creation of a record? Commented Aug 18, 2024 at 15:26
  • @PhilW Modified my approach to 1) Apex trigger on custom object publishes platform event 2) Apex Trigger on platform event calls queueable apex 3) Queueable apex with Database.AllowsCallouts includes all the main logic. Still not working. The problem is second trigger that should call queueable apex is not firing. I am new to platform events, can you please tell me how to debug platform events? Commented Aug 19, 2024 at 10:55
  • This will avoid fragility and is a good thing to do (though since PEs do not have guaranteed delivery I personally wouldn't store the IDs to process in the way you have in the events; again covered in the article). Note, however, that it won't fix the original error since this is coming from some code you've not included in your question. Commented Aug 19, 2024 at 11:00
  • Debugging platform events simply needs you to enable debug for the "Automated Process" user - this is a special user that the processing runs as. Commented Aug 19, 2024 at 11:00

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.