Is there a way to write a server event that adds relationships by creating aml

I have a form that I need to add relationships to initiatives and I want to create those relationships when the user saves the form. The Bold aml is what i want to add. 

"<Item type="Action Plan" typeId="22E5726BF58743B4AEEA018086F2AD6E" id="A5966C1DA4D5447A9A937FBED748233E">
<id keyed_name="error" type="Action Plan">A5966C1DA4D5447A9A937FBED748233E</id>
<strategic_plan_id keyed_name="Strategic Plan Practice 2" type="Strategic Plan">
<Item type="Strategic Plan" typeId="31F5929A1A0C43808AE760F82478A422" id="F585C86B37434C67A661F3FE5828B8EB">
<id keyed_name="Strategic Plan Practice 2" type="Strategic Plan">F585C86B37434C67A661F3FE5828B8EB</id>
<Relationships>
<Item type="SP Target" typeId="FEB9F11C4D5242E9924C6B918C74140F" id="F5821FFE9C81477B99F08C2BD5FF9222">
<id keyed_name="F5821FFE9C81477B99F08C2BD5FF9222" type="SP Target">F5821FFE9C81477B99F08C2BD5FF9222</id>
<source_id keyed_name="Strategic Plan Practice 2" type="Strategic Plan">F585C86B37434C67A661F3FE5828B8EB</source_id>
<target_text>Target 1</target_text>
</Item>
</Relationships>
</Item>
</strategic_plan_id>
<Relationships>
<Item type="AP Initiative" typeId="B0570AFDC1534AC8B279177F1E88E086" id="9D5EA7DE0A6744CB8C0BF8A1509E20FA">
<id keyed_name="9D5EA7DE0A6744CB8C0BF8A1509E20FA" type="AP Initiative">9D5EA7DE0A6744CB8C0BF8A1509E20FA</id>
<initiative_text>Initiative 1</initiative_text>
<source_id keyed_name="error" type="Action Plan">A5966C1DA4D5447A9A937FBED748233E</source_id>
<Relationships>
<Item type="AP Initiative Target" action="add">
<related_id>F5821FFE9C81477B99F08C2BD5FF9222</related_id>
</Item>
</Relationships>
</Item>
</Relationships>
</Item>"

Here is the code I have so far

Innovator inn = this.getInnovator();
var sp_target_id = inn.getID('SP Target');
var target_string = '<Relationships>';
target_string += '<Item type="AP Initiative Target" action="add">';
target_string += '<related_id>'"'+sp_target_id+'"'</related_id>';
target_string += '</Item>';
target_string += '</Relationships>';

var res1 = innov.applyAML('<AML>' + target_string + '</AML>');
if (res1.isError()) {
aras.AlertError(res1.getErrorString());
return 'Error';
}

  • Hi, 

    Yes - it's possible - but I think there is something missing from your AML.

    Here is example:

    <Item type="Part BOM" action="add">
      <related_id>
        <Item type="Part" action="get">
          <id>6411401035053</id>
        </Item>
      </related_id>
      <source_id>
        <Item type="Part" action="get">
          <id>6411401035050</id>
        </Item>
      </source_id>
    </Item>

    So you need to have:

    * Relationship type - in my example Part BOM - and I think in your AML it should be 'AP Initiative Target' (and action=''add' since you are creating this _relationship_.

    * source item - and action='get' tells that this item is already in the Aras (if not, you might want to create it with action='add')

    * related_id - the child of the relationship (and again, action='get' is actually search which returns (at least in this case) only one item)

    You can test your AML with Nash (it comes with Aras installation) can be found: http://[your server]/InnovatorServer/client/scripts/nash.aspx - if you used the default URL:s when installing.

    Hope this helps, 

    //Jukka.

  • assuming 'this' is the item you wish to save, I would suggest you try something similar to:

    //Get the related_id
    var sp_target_id = this.getRelationship("SP Target").getRelatedItemID(); //assumes 1 relationship item of type "SP Target"

    //create the relationship Item
    var target_string = "<Relationships>";
    target_string += "<Item type='AP Initiative Target' action='add'>";
    target_string += "<related_id>" +sp_target_id+ "</related_id>";
    target_string += "</Item>";
    target_string += "</Relationships>";

    //Load the AML into a new Item
    Var targ = this.newItem();
    targ.loadAML(target_string)

    //Add the relationship to the context items AML
    this.addRelationship(targ);

    Note: I have not tested this, but it shows the approach I would take if I was to do something similar.

    Many thanks,
    Martin