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('');

Set the verb id and its display name.

  1. connector.setVerbId('');
  2. connector.setVerbDisplay('Read the course introduction');

Set the object id and description.

  1. connector.setObjectId('');
  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();
  3. contextActivity.setId('');
  4. contextActivity.setName('HR on-boarding course');
  5. contextActivity.setType('');
  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();
  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();
  3. connector.setActorName('xAPI Tester');
  4. connector.setActorMbox('');
  5. connector.setActorAccount('123456', '');
  6. //connector.setActorOpenId('');
  7. //connector.setActorMboxSha1Sum('1234567890123456789012345678901234567890');
  8. connector.setVerbId('');
  9. connector.setVerbDisplay('Read the course introduction');
  10. connector.setObjectId('');
  11. connector.setObjectName('Maths for beginners');
  12. connector.setObjectDescription('This course teaches the fundamentals of multiplication, addition and subtraction.');
  14. MetaxAPI.XapiStatementObject parentActivity = new MetaxAPI.XapiStatementObject();
  15. parentActivity.setId('');
  16. connector.addContextActivity('parent', parentActivity);
  18. MetaxAPI.XapiStatementObject categoryActivity = new MetaxAPI.XapiStatementObject();
  19. categoryActivity.setId('');
  20. categoryActivity.setName('team meeting');
  21. categoryActivity.setType('');
  22. connector.addContextActivity('category', categoryActivity);
  24. MetaxAPI.XapiStatementObject otherActivity1 = new MetaxAPI.XapiStatementObject();
  25. otherActivity1.setId('');
  26. connector.addContextActivity('other', otherActivity1);
  28. MetaxAPI.XapiStatementObject otherActivity2 = new MetaxAPI.XapiStatementObject();
  29. otherActivity2.setId('');
  30. connector.addContextActivity('other', otherActivity2);
  32. connector.setContextRegistration('ec531277-b57b-4c15-8d91-d292c5b2b8f7');
  34. MetaXapi.XapiStatementAgent contextInstructor = new MetaXapi.XapiStatementAgent();
  35. contextInstructor.setName('Andrew Downes');
  36. contextInstructor.setAccount('13936749', '');
  37. connector.setContextInstructor(contextInstructor);
  39. connector.setContextPlatform('Example virtual meeting software');
  40. connector.setContextLanguage('tlh');
  41. connector.setContextStatement('6690e6c9-3ef0-4ed3-8b37-7f3964730bee');
  43. connector.setResultSuccess(true);
  44. connector.setResultCompletion(true);
  45. connector.setResultResponse('We agreed on some example actions.');
  46. connector.setResultDuration('PT1H0M0S');
  47. connector.addResultExtension('', 'X:\\meetings\\minutes\\');
  49. connector.setScoreScaled(1);
  50. connector.setScoreRaw(1);
  51. connector.setScoreMin(1);
  52. connector.setScoreMax(1);
  54. connector.setVersion('1.0.3');
  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