Today's question - creating record relationships from the ServiceAPI

Background

In HPRM records may have a variety of different relationships, from 'Redaction of' to 'Related to'.  These relationships are managed in both the SDK and ServiceAPI via the child collection construct.  To add a relationship you add a new item to the ChildRelationships collection, to remove a relationship you remove an item from this collection.

C#

This code will create a new relationship using the .Net client for the ServiceAPI.

TrimClient trimClient = new TrimClient("http://MyHost]/ServiceAPI");
trimClient.Credentials = System.Net.CredentialCache.DefaultCredentials;

Record record = new Record() { Uri = 9000000001 };
record.ChildRelationships = new List<RecordRelationship>() { 
    new RecordRelationship() { 
        RelatedRecord = new RecordRef() { Uri = 9000000003}, 
        RelationType = RecordRelationshipType.IsRelatedTo
    }};

trimClient.Post<RecordsResponse>(record);

Javascript

Assuming you are using jQuery this code will create a record relationship from Javascript, the complete code can be found here.

$().ready(function () {
    $('#recordRelationship').submit(function () {
        var data = { "Uri": $("input[name=Uri]").val() }

        data["ChildRelationships"] = [{
            "RecordRelationshipRelatedRecord": $("input[name=RelatedUri]").val(),
            "RecordRelationshipRelationType": "IsRelatedTo"                    
        }];

        $.ajax({
            url: "Record",
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(data)

        })
        .done(function (response, statusText) {
            alert("success");
        })
        .fail(function (xhr) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.ResponseStatus.Message);
        });

        return false;
    });
});
Written on August 27, 2015