Send xAPI from Apex

The xAPI connector class enables the assembly and transmission of complex xAPI statements to your LRS. You can find the class definition here.

Example General Experience Statement

In this example, we create and transmit an xAPI statement using specific values. In your implementation, you will extract information from your Salesforce data schema. The locale that was specified during setup will be used by the statement fields that require it.

Instantiate an xAPI connector.

  1. MetaxAPI.XapiConnector connector = new MetaxAPI.XapiConnector();

Set the actor’s name and use MBox for user identification. For other user identification cases, you can use setActorOpenId, setActorAccount, and setActorMboxSha1Sum.

  1. connector.setActorName('xAPI Tester');
  2. connector.setActorMbox('tester@globebyte.com');

Set the verb id and its display name.

  1. connector.setVerbId('http://adlnet.gov/expapi/verbs/experienced');
  2. connector.setVerbDisplay('Read the course introduction');

Set the object id and description.

  1. connector.setObjectId('https://globebyte.com/pathway/course/hr/code/234');
  2. connector.setObjectDescription('HR on-boarding course');

Add a context hierarchy. For more complex context structures, including extensions and statement references, refer to the full class documentation.


  1. MetaXapi.XapiStatementObject contextActivity = new MetaXapi.XapiStatementObject();
  2.  
  3. contextActivity.setId('https://globebyte.com/pathway/course/hr/code/234');
  4. contextActivity.setName('HR on-boarding course');
  5. contextActivity.setType('https://globebyte.com/pathway/course');
  6.  
  7. connector.addContextActivity('category', contextActivity);

Finally, validate the statement before sending it synchronously. If you are not concerned about the outcome, you can use sendAsync. sendSync will return a boolean (true/false) to indicate whether the send was successful or not. If it returns false, the send was not successful, in which case an error message will have been written to the apex debug log, you can access the log using these instructions.

  1. Boolean isValid = connector.validate();
  2.  
  3. if (isValid) {
  4.   System.debug('Sending statement...');
  5.   try {
  6.     Boolean isSent = connector.sendSync();
  7.     System.debug('Statement send returned ' + isSent);
  8.   }
  9.   catch (Exception error) {
  10.     System.debug(LoggingLevel.ERROR, 'Exception occurred: ' + error.getMessage());
  11.   }
  12. } else {
  13.   System.debug ('Statement not valid, did not send.');
  14. }

Below is an example of a full statement.

  1. MetaxAPI.XapiConnector connector = new MetaxAPI.XapiConnector();
  2.  
  3. connector.setActorName('xAPI Tester');
  4. connector.setActorMbox('tester@globebyte.com');
  5. connector.setActorAccount('123456', 'https://www.globebyte.com');
  6. //connector.setActorOpenId('https://www.globebyte.com/user');
  7. //connector.setActorMboxSha1Sum('1234567890123456789012345678901234567890');
  8. connector.setVerbId('http://adlnet.gov/expapi/verbs/experienced');
  9. connector.setVerbDisplay('Read the course introduction');
  10. connector.setObjectId('https://www.globebyte.com/content/write-xapi-statement');
  11. connector.setObjectName('Maths for beginners');
  12. connector.setObjectDescription('This course teaches the fundamentals of multiplication, addition and subtraction.');
  13.  
  14. MetaxAPI.XapiStatementObject parentActivity = new MetaxAPI.XapiStatementObject();
  15. parentActivity.setId('http://www.globebyte.com/meetings/series/267');
  16. connector.addContextActivity('parent', parentActivity);
  17.  
  18. MetaxAPI.XapiStatementObject categoryActivity = new MetaxAPI.XapiStatementObject();
  19. categoryActivity.setId('http://www.globebyte.com/meetings/categories/teammeeting');
  20. categoryActivity.setName('team meeting');
  21. categoryActivity.setType('http://globebyte.com/expapi/activities/meetingcategory');
  22. connector.addContextActivity('category', categoryActivity);
  23.  
  24. MetaxAPI.XapiStatementObject otherActivity1 = new MetaxAPI.XapiStatementObject();
  25. otherActivity1.setId('http://www.globebyte.com/meetings/occurances/34257');
  26. connector.addContextActivity('other', otherActivity1);
  27.  
  28. MetaxAPI.XapiStatementObject otherActivity2 = new MetaxAPI.XapiStatementObject();
  29. otherActivity2.setId('http://www.globebyte.com/meetings/occurances/342572');
  30. connector.addContextActivity('other', otherActivity2);
  31.  
  32. connector.setContextRegistration('ec531277-b57b-4c15-8d91-d292c5b2b8f7');
  33.  
  34. MetaXapi.XapiStatementAgent contextInstructor = new MetaXapi.XapiStatementAgent();
  35. contextInstructor.setName('Andrew Downes');
  36. contextInstructor.setAccount('13936749', 'http://www.globebyte.com');
  37. connector.setContextInstructor(contextInstructor);
  38.  
  39. connector.setContextPlatform('Example virtual meeting software');
  40. connector.setContextLanguage('tlh');
  41. connector.setContextStatement('6690e6c9-3ef0-4ed3-8b37-7f3964730bee');
  42.  
  43. connector.setResultSuccess(true);
  44. connector.setResultCompletion(true);
  45. connector.setResultResponse('We agreed on some example actions.');
  46. connector.setResultDuration('PT1H0M0S');
  47. connector.addResultExtension('http://globebyte.com/profiles/meetings/resultextensions/minuteslocation', 'X:\\meetings\\minutes\\examplemeeting.one');
  48.  
  49. connector.setScoreScaled(1);
  50. connector.setScoreRaw(1);
  51. connector.setScoreMin(1);
  52. connector.setScoreMax(1);
  53.  
  54. connector.setVersion('1.0.3');
  55.  
  56. Boolean isValid = connector.validate();

Sending Outcomes

The class supports reporting or taking action based on assessment submissions or measured admission process outcomes by using methods like setResultCompletion or setResultResponse, as well as handling scores.

Authority and Platform

Processing xAPI data for reporting purposes can be intricate. You can utilize setAuthority and setVersion to aid in filtering inbound learning telemetry processing.

More Information

For more information and implementation details, browse the documentation below:

Setting up Globebyte xAPI Send xAPI from a flow Form action fields Send xAPI from Apex Logging and defaults xAPI class connector reference