ServiceAPI - Attach an action

Background

Today's question of the day from the forums is how to attach an action from the ServiceAPI.  Took me a few minutes to work out exactly what was going on here as the ServiceAPI inherits a little bit of the opacity of the SDK.

The Code

TrimClient trimClient = new TrimClient("http://david-pc/ServiceAPI");
trimClient.Credentials = System.Net.CredentialCache.DefaultCredentials;

Record request = new Record();
request.Uri = 9000000012;

request.ActionsToCall = new List<IMainObjectActionRequest>() { 
    new AttachAction() { 
        ActionToAttach = new ActionDefRef() { Uri = 9000000008 }, 
        NewAssignee = new LocationRef() { Uri=1 }
    }
};

try
{
    var response = trimClient.Post<RecordsResponse>(request);
    Console.WriteLine(response.Results[0].Uri);
}
catch (WebServiceException wex)
{
    Console.WriteLine(wex.ErrorMessage ?? wex.Message);
}

Some comments

RecordActionUri

There is a property called RecordActionUri.  This is not the Uri of the action to attach but a Record Action before (or after) you which this action should start.  The help from the SDK reads: 'Insert a new action into the list of actions, having it start before or after the nominated attached record action.'

ActionToAttach

When specifying the ActionToAttach you must specify the Uri of the action, it will not be found by name.

Written on September 16, 2015