Send xAPI from Apex

Send xAPI from Apex

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

Example 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 Statement.

Globebyte.XapiStatement statement = new Globebyte.XapiStatement();

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

statement.xapiActor.setName('xAPI Tester');
statement.xapiActor.setMbox('tester@globebyte.com');

Set the verb id and its display name.

statement.xapiVerb.setId('http://adlnet.gov/expapi/verbs/experienced');
statement.xapiVerb.setDisplay('Read the course introduction');

Set the object id and name.

statement.xapiObject.setId('https://globebyte.com/pathway/course/hr/code/234');
statement.xapiObject.setName('HR on-boarding course');

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

statement.xapiContext.setActivityId('https://globebyte.com/pathway/course/hr/code/234');
statement.xapiContext.setActitivtyName('HR on-boarding course');
statement.xapiContext.setActivityType('https://globebyte.com/pathway/course');
statement.xapiContext.addActivity('category');

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.

Boolean isValid = statement.validate();

if (isValid) {
  System.debug('Sending statement...');
  try {    Boolean isSent = statement.sendSync();
    System.debug('Statement send returned ' + isSent);
  }
  catch (Exception error) {
    System.debug(LoggingLevel.ERROR, 'Exception occurred: ' + error.getMessage());
  }
} else {
  System.debug ('Statement not valid, did not send.');
}

Below is an example of a full statement.

Globebyte.XapiStatement statement = new Globebyte.XapiStatement();
 
statement.xapiActor.setName('xAPI Tester');
 
/*
 * Only one of Mbox, Account, OpenId or MboxSha1Sum can be set in a statement.
 */
statement.xapiActor.setMbox('tester@globebyte.com');
//statement.xapiActor.setAccount('123456', 'https://www.globebyte.com');
//statement.xapiActor.setOpenId('https://www.globebyte.com/user');
//statement.xapiActor.setMboxSha1Sum('1234567890123456789012345678901234567890');
 
statement.xapiVerb.setId('http://adlnet.gov/expapi/verbs/experienced');
statement.xapiVerb.setDisplay('Read the course introduction');
statement.xapiObject.setId('https://www.globebyte.com/content/write-xapi-statement');
statement.xapiObject.setName('Maths for beginners');
statement.xapiObject.setDescription('This course teaches the fundamentals of multiplication, addition and subtraction.');
 
/*
 * Following is an example of a simple context activity of type parent.
 */
statement.xapiContext.setActivityId('http://www.globebyte.com/meetings/series/267');
statement.xapiContext.addActivity('parent');
 
/*
 * Following is an example of a more complex context activity of type category.
 */
statement.xapiContext.setActivityId('http://www.globebyte.com/meetings/categories/teammeeting');
statement.xapiContext.setActivityName('team meeting');
statement.xapiContext.setActivityType('http://globebyte.com/expapi/activities/meetingcategory');
statement.xapiContext.addActivity('category');
 
/*
 * Following is an example of a context activity hierarchy of two activities of type other.
 */
statement.xapiContext.setActivityId('http://www.globebyte.com/meetings/occurances/34257');
statement.xapiContext.addActivity('other');
statement.xapiContext.setActivityId('http://www.globebyte.com/meetings/occurances/342572');
statement.xapiContext.addActivity('other');
 
statement.xapiContext.setRegistration('ec531277-b57b-4c15-8d91-d292c5b2b8f7');
statement.xapiContext.setInstructorName('Andrew Downes');
statement.xapiContext.setInstructorAccount('13936749', 'http://www.globebyte.com');
statement.xapiContext.setPlatform('Example virtual meeting software');
statement.xapiContext.setLanguage('tlh');
statement.xapiContext.setStatement('6690e6c9-3ef0-4ed3-8b37-7f3964730bee');
statement.xapiResult.setSuccess(true);
statement.xapiResult.setCompletion(true);
statement.xapiResult.setResponse('We agreed on some example actions.');
statement.xapiResult.setDuration('PT1H0M0S');
statement.xapiResult.addExtension('http://globebyte.com/profiles/meetings/resultextensions/minuteslocation', 'X:\\meetings\\minutes\\examplemeeting.one');
statement.xapiResult.setScoreScaled(1);
statement.xapiResult.setScoreRaw(1);
statement.xapiResult.setScoreMin(1);
statement.xapiResult.setScoreMax(1);
 
statement.setVersion('1.0.3');
 
Boolean isValid = statement.validate();
 
if (isValid) {
  System.debug('Sending statement...');
  try {
    /*
     * If using this code in an invocable method use sendAsync() otherwise use sendSync().
     */
    Boolean isSent = statement.sendSync();
    System.debug('Statement send returned ' + isSent);
    //statement.sendAsync();
  }
  catch (Exception error) {
    System.debug(LoggingLevel.ERROR, 'Exception occurred: ' + error.getMessage());
  }
} else {
  System.debug ('Statement not valid, did not send.');
}

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.

Last updated