This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

DEVELOPERS FORUM - Getting a property value for a related item

jbiggs - Monday, June 23, 2014 1:46 PM:

I just went through the Developing Solutions class, and am attempting to implement some of the things I learned there. I'm quite new to this level of development, so please bear with me.

I want to get the id of the identity tied to a particular user. I wrote the following code in AML first, with the intent to build the query in a VB method:

 

 

<AML>
  <Item type="User" action="get" where="[User].id='xxxxxxxxxxxxxxxxxxxxxx'">
    <Relationships>
      <Item type="Alias" action="get">
        <related_id>
          <Item type="Identity" action="get" select="id"></Item>
        </related_id>
      </Item>
    </Relationships>
  </Item>
</AML>

This returns the user along with the related identity. I then create the same query in VB:

Dim userID As String = Me.getInnovator().getUserId()
Dim myItem As Item = Me.newItem("User", "get")
myItem.setAttribute("where", "[User].id='" + userID + "'")
Dim relationship As Item = Me.newItem("Alias", "get")
Dim identity As Item = Me.newItem("Identity", "get")
identity.setAttribute("select", "id")
relationship.setRelatedItem(identity)
myItem.addRelationship(relationship)
myItem = myItem.apply()

However, I don't know how to use getProperty here, because the item returned is actually a User item, not the related Identity item. How would I go about getting just the ID of the Identity? Must I return just the Identity item only, or is there a way to reference the nested/related item to get at the needed property?

Thanks.

 

 



DavidSpackman - Monday, June 23, 2014 9:05 PM:

Hi, 

Good reference is the Programmers Guide

 

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim innovator As Innovator = Me.getInnovator() 

 

' Admin user

Dim userID As String = "30B991F927274FA3829655F50C99472E"

 

Dim myItem As Item = Me.newItem("User", "get")

myItem.setAttribute("select", "id")

'myItem.setAttribute("where", "[User].id='" + userID + "'")

myItem.setID(userID)

 

Dim relationship As Item = Me.newItem("Alias", "get")

relationship.setAttribute("select", "related_id")

 

Dim relatedItem As Item = Me.newItem("Identity", "get")

relatedItem.setAttribute("select", "id, keyed_name")

 

relationship.setRelatedItem(relatedItem)

myItem.addRelationship(relationship)

Dim results As Item = myItem.apply()

 

' Test for an error. 

If results.isError() Then 

Return innovator.newError("Error: " + results.getErrorDetail())

End If 

 

' Get a handle to the Alias Items. 

Dim aliasItems As Item = results.getRelationships("Alias") 

Dim count As Integer = aliasItems.getItemCount() 

Dim i As Integer 

 

Dim IdentityID_list As String = ""

 

' Iterate over the Alias Items 

For i = 0 To count - 1

' Get a handle to the relationship Item by index. 

Dim aliasItem As Item = aliasItems.getItemByIndex(i)

 

Dim keyedName As String = aliasItem.getRelatedItem().getProperty("keyed_name","")

Dim IdentityID_method1 As String = aliasItem.getRelatedItem().getID()

 

' Get a handle to the related Item for this relationship Item. 

Dim Identity As Item = aliasItem.getRelatedItem()

 

Dim IdentityID_method2 As String = Identity.getID()

Dim IdentityID_method3 As String = Identity.getProperty("id","")

 

IdentityID_list = "<b>Keyed Name:</b> " + keyedName + "<br><b>IdentityID_method1:</b> " + IdentityID_method1 + "<br><b>IdentityID_method2:</b> " + IdentityID_method2 + "<br><b>IdentityID_method3:</b> " + IdentityID_method3

Next

 

Return innovator.newError(IdentityID_list)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



jbiggs - Tuesday, June 24, 2014 10:53 AM:

Thank you David, I forgot all about the programmer's guide, which has led me to approach what I'm doing a different way, and has gotten me stuck on one piece yet again:

I have an itemtype that has a null relationship in which I store some fields on the relationship itself. I am creating a grid event that fires when the user selects a grid row. It is supposed to check one of the line fields to determine if the current user has the same identity id as the identity on the current line. I haven't done much in Javascript or with client methods, and I am having trouble figuring out how to reference a field stored in the relationship. I was under the impression that document.thisItem.getProperty("the_needed_field") would get that for me, because thisItem should refer to the relationship line itself, but this is apparently not the case. Out of curiosity, I then tried replacing "the_needed_field" with a field name from the parent itemtype, but again got the same error:

Unable to get value of the property 'getProperty': object is null or undefined

Do I need to do a complete search for the related item I'm working on, starting at the top and getting the relationships and so on? It seems like I should be able to use document.thisItem for this.

Thanks again.



pankaj - Thursday, June 26, 2014 7:09 AM:

Hi,

Could you explain it in detail.

Form value you need or Relationship value?

Which event you had written your code and where ?

Or Try with this document.thisItem.getAttribute("the_needed_field").

 

Regards,

Pankaj B

 

 



jbiggs - Thursday, June 26, 2014 9:00 AM:

Thank you Pankaj, here are some further details:

I have an itemtype called "Tech Doc" that is the parent itemtype. It has a null relationship called "Circ List", which contains the following fields for each line which are stored as properties on the relationship itemtype:

1. circ_number: integer
2. reviewer: Item Identity
3. notes: string
4. review_date: date

The method is attached to the OnSelectRow grid event (on the relationshiptype on Grid Events tab). I'm guessing that grid events are client-side and therefore need to be Javascript.

What I'm trying to do is this: when a user clicks on a grid row for this relationship, the method should examine the reviewer field and determine whether the current user has the same identity as the reviewer. I'm having trouble getting the reviewer field value. Here is my code so far:

var inn = new Innovator();
var reviewerID = document.thisItem.getProperty("reviewer");
var userID = inn.getUserID();


var qry = inn.newItem("User","get");
qry.setAttribute("select","id");
var alias = new Item("Alias","get");
alias.setAttribute("select","related_id");
var identity = new Item("Identity","get");
identity.setAttribute("select","id");
identity.setAttribute("where", "[Identity].id='" + reviewerID + "'");
alias.setRelatedItem(identity);
qry.addRelationship(alias);
var results = qry.apply();

Once this is working, I should be able to get the property "id" from the returned item, and compare it to the userID. But when the method fires, it gives me the error I mentioned earlier:

Unable to get value of the property 'getProperty': object is null or undefined

There's only one place I'm using "getProperty, and that is in trying to get the reviewer ID on line 2. So the question is how would I go about getting that field value?



DavidSpackman - Thursday, June 26, 2014 6:48 PM:

If you want to see what existing methods are you in place you can use this little bit of SQL code

-----------------------------------------------------------------

SELECT

RT.NAME RelationshipType,

M.NAME Method,

RGE.GRID_EVENT

FROM 

innovator.RelationshipType RT

INNER JOIN innovator.RELATIONSHIP_GRID_EVENT RGE ON RGE.SOURCE_ID = RT.ID

INNER JOIN innovator.METHOD M ON M.ID = RGE.RELATED_ID

--WHERE

-- RGE.GRID_EVENT = 'ondeleterow'

ORDER BY

RT.NAME,

M.NAME,

RGE.GRID_EVENT

-----------------------------------------------------------------

 

There is some information in the programmers guide (4.5.4 Grid Events) 

Also some more information in Developing Solutions guide if you are an Aras subscriber.

Here is some examples of code we use
-----------------------------------------------------------------
Aim: Prevent the relationship being deleted if you are not the creator
Event: ondeleterow
-----------------------------------------------------------------
//debugger;
var xpath = "Relationships/Item/related_id/Item[@id='" + relatedID + "']";
var relitm = item.selectSingleNode(xpath);
if (relitm === null) {
   return;
}
var createdBy = top.aras.getItemProperty(relitm, "created_by_id");
if (createdBy && createdBy !== top.aras.getUserID())
{
alert("Not possible to delete this item because you are not the creator");
return false;
}
 
return true;
-----------------------------------------------------------------
Aim: Prevent the relationship being deleted if state is closed
Event: ondeleterow
-----------------------------------------------------------------
var xpath = "Relationships/Item/related_id/Item[@id='" + relatedID + "']";
var relitm = item.selectSingleNode(xpath);
if (relitm === null) {
   return;
}
var state = top.aras.getItemProperty(relitm, "state");
if ( state == "Closed")
{
alert("Not possible to delete closed Action Item");
return false;
}
 
return;
-----------------------------------------------------------------


DavidSpackman - Thursday, June 26, 2014 7:01 PM:

Another good tip that got me started.

If you want to see what existing methods are in place that you can learn from, check the 2 Aras demo databases on the community project page.

 

Where is some SQL code that helps me investigate what methods are running where, and also lets me look for example code

 

----------------------------------------------------------------------------------------------------

-- Methods

select

B.METHOD_TYPE

, B.NAME

, B.METHOD_CODE

, innovator.ConvertToLocal(B.[CREATED_ON],DEFAULT) AS [CREATED_ON]

, innovator.ConvertToLocal(B.[MODIFIED_ON],DEFAULT) AS [CREATED_ON]

, U1.KEYED_NAME [CREATED BY]

, U2.KEYED_NAME [MODIFIED BY]

 

from

innovator.METHOD B

left join innovator.[USER] U1 ON U1.ID = B.CREATED_BY_ID

left join innovator.[USER] U2 ON U2.ID = B.MODIFIED_BY_ID

where

B.METHOD_CODE LIKE '%isNew%'

AND B.IS_CURRENT = 1

ORDER BY

B.METHOD_TYPE,

B.[CREATED_ON]

GO

 

----------------------------------------------------------------------------------------------------

-- ItemType Client Event

select

C.NAME ITEMTYPE

,A.CLIENT_EVENT

,B.METHOD_TYPE

,B.NAME METHOD_NAME

,B.METHOD_CODE

from

innovator.CLIENT_EVENT A

left join innovator.METHOD B ON A.RELATED_ID = B.ID

left join innovator.ITEMTYPE C ON A.SOURCE_ID = C.ID

--where

--A.SOURCE_ID = '4F1AC04A2B484F3ABA4E20DB63808A88'

--AND 

--B.METHOD_TYPE='Javascript'

--AND 

--B.METHOD_CODE LIKE '%getNextSequence%'

ORDER BY

C.NAME

GO

----------------------------------------------------------------------------------------------------

-- Form Event

select

C.NAME FORM_NAME

,A.FORM_EVENT

,B.METHOD_TYPE

,B.NAME METHOD_NAME

,B.METHOD_CODE

from

innovator.FORM_EVENT A

left join innovator.METHOD B ON A.RELATED_ID = B.ID

left join innovator.FORM C ON A.SOURCE_ID = C.ID

--where

-- A.FORM_EVENT LIKE '%unload'

--A.SOURCE_ID = '4F1AC04A2B484F3ABA4E20DB63808A88'

--AND 

--B.METHOD_TYPE='Javascript'

ORDER BY

C.NAME

 

GO

----------------------------------------------------------------------------------------------------

-- ItemType - Server Events

select

C.NAME ITEMTYPE

,A.SERVER_EVENT

,B.METHOD_TYPE

,B.NAME METHOD_NAME

,B.METHOD_CODE

from

innovator.SERVER_EVENT A

left join innovator.METHOD B ON A.RELATED_ID = B.ID

left join innovator.ITEMTYPE C ON A.SOURCE_ID = C.ID

where

--A.SOURCE_ID = '4F1AC04A2B484F3ABA4E20DB63808A88'

--AND 

-- B.METHOD_CODE LIKE '%email%'

A.SERVER_EVENT = 'OnAfterGet'

ORDER BY

C.NAME

GO

----------------------------------------------------------------------------------------------------

 

-- RelationshipType

SELECT

RT.NAME RelationshipType,

RGE.GRID_EVENT,

M.NAME METHOD_NAME,

M.METHOD_CODE

FROM 

innovator.RelationshipType RT

INNER JOIN innovator.RELATIONSHIP_GRID_EVENT RGE ON RGE.SOURCE_ID = RT.ID

INNER JOIN innovator.METHOD M ON M.ID = RGE.RELATED_ID

WHERE

RGE.GRID_EVENT = 'onselectrow'

ORDER BY

RT.NAME,

M.NAME,

RGE.GRID_EVENT

GO

----------------------------------------------------------------------------------------------------

-- Workflow Activity Server Events

select

WFM.NAME WORKFLOW_MAP_NAME

, AT.NAME ACTIVITY_TEMPLATE_NAME

, ATM.[EVENT]

, M.METHOD_TYPE

, M.NAME METHOD_NAME

, M.METHOD_CODE

FROM

innovator.WORKFLOW_MAP WFM

left join innovator.WORKFLOW_MAP_ACTIVITY WFMA ON WFMA.SOURCE_ID = WFM.ID

left join innovator.ACTIVITY_TEMPLATE AT ON AT.ID = WFMA.RELATED_ID

left join innovator.ACTIVITY_TEMPLATE_METHOD ATM ON ATM.SOURCE_ID = AT.ID

left join innovator.METHOD M ON M.ID = ATM.RELATED_ID

WHERE

WFM.IS_CURRENT = '1'

AND M.NAME IS NOT NULL

--AND M.METHOD_CODE LIKE '%email%'

ORDER BY

WFM.NAME,

AT.NAME,

ATM.SORT_ORDER

----------------------------------------------------------------------------------------------------

 

 



jbiggs - Friday, June 27, 2014 4:02 PM:

Thank you for the info David, unfortunately I am not in a position to run SQL queries against the database at the moment. I did try the programmer's guide and it did not offer any advice on this problem. The javascript methods you posted are similar to what I am trying to do, but I haven't touched xpath in a number of years, and trying to parse out how your code works is outside my ability for now. I have a request for help from Aras support in progress, and I'll post back here with their recommendation, unless someone is able to answer my question about getting the value of the reviewer field as noted in my last post.



DavidSpackman - Friday, June 27, 2014 8:27 PM:

Tested this example

I would suggest you use the owned_by_id property rather than the reviewer property, allows you to standardise more.

 

Code is a little different to above because you are using a null relationship. 

------------------------------------------------------------------------------------------------------------------------------------------

var xpath = "Relationships/Item[@id='" + relationshipID + "']";

var relitm = item.selectSingleNode(xpath);

if (relitm === null) {

   return;

}

var reviewer = top.aras.getItemProperty(relitm, "reviewer");

if(!pb_isCurrUserMemberOfIdentityId(reviewer))

{

  alert("You are not a member of the reviewer identity");

}

else

{

alert("You are a member of the reviewer identity");

}

 

return;

 

//Code from PE_ShowCreateNewRevButton method

function pb_isCurrUserMemberOfIdentityId(identity_to_check) 

{

  if (identity_to_check === "") {return false;}

 

  var sessionIdentitites_array = top.aras.getIdentityList().split(",");

  // now we have all identities the current user (session) is member of

  // scan if given identity is in the list - if yes, return true

  for (var i=0;i<sessionIdentitites_array.length;i++)

  {

    if (identity_to_check == sessionIdentitites_array[i]) {return true;}

  }

  return false;

};

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 



jbiggs - Tuesday, July 1, 2014 11:30 AM:

David, thank you very much for your help with this. I was able to get it working with your xpath statements, and am starting to understand how this stuff works together. Aras actually suggested that I use the built in permission model using ***_by_id properties. You're right, standardizing is better wherever possible.

Thanks again!