How to call a server method with applyMethod() from a client method, and use the results coming back from server

Server Method : [Get_Identity_Details] Can run this and get results I want. Hard coded guid for test

Innovator inn = this.getInnovator();
string sessionuserid = this.getProperty("sessionuser","invalid");
if (sessionuserid == ""){}

StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT ID,KEYED_NAME FROM [ArasDev].[innovator].[IDENTITY] WHERE CLASSIFICATION='Group' AND ID in ");
sql.AppendLine(" ( SELECT SOURCE_ID from [ArasDev].[innovator].[MEMBER] where RELATED_ID in");
sql.AppendLine("( SELECT ID FROM [ArasDev].[innovator].[IDENTITY] WHERE CLASSIFICATION='Role' AND ID in");
sql.AppendLine("( SELECT SOURCE_ID from [ArasDev].[innovator].[MEMBER] where RELATED_ID in");
sql.AppendLine("(SELECT RELATED_ID from [ArasDev].[innovator].[ALIAS] where SOURCE_ID='<USER GUID>'");
sql.AppendLine("))))");

Item results = inn.applySQL(sql.ToString());

return results;

=======================================

Client method : calls the server method. Can see the result in debug from server method, but it does not work after that. How to make use of the results from server method?

var inn = new Innovator();
var userid = inn.getUserID();

var itemTypeDropdownComponent = getFieldComponentByName("owned_by_id");
var itemTypeDropdownList = [];


var body = "<sessionuser>" + userid + "</sessionuser>";
var res = aras.applyMethod("NOV_Identity_Details",body);

if (res.isError()) {
aras.AlertError(res);
return;
}
else if (res.getItemCount() === 0)
{
  //do something
}
else
{
for (var i=0;i<res.getItemCount();i++)
{
var currentItem = res.getItemByIndex(i);
itemTypeDropdownList.push({label: currentItem.getProperty("KEYED_NAME"), value: currentItem.getProperty("ID")})
}
}
itemTypeDropdownComponent.component.setState({
list: itemTypeDropdownList
});

return null;

  • Hi amitosh,

    It appears that the result of the aras.applyMethod function is a string containing the item data returned by the server method. To put this into a more readable format, I'd recommend using code like below to load it into an IOM Item object.

    var res = aras.applyMethod("NOV_Identity_Details", "body");
    var resItem = aras.IomInnovator.newItem(); // Create a new empty item
    resItem.loadAML(res); // and then load in the string returned from aras.applyMethod
    alert(resItem.getProperty("item_number", "")); // so we can access the item data using a convenient API

    You can then perform all your logic and checks using the resItem variable. With that being said, we recommend against using direct SQL inside of a server method as it bypasses the standard Aras permission model. Instead you could perform all of this logic inside of your client method by taking advantage of the standard aras.getIdentityList() function. This will return a list containing all of the identities the currently logged in user belongs to. This list is also built off of a recursive query, so it will get all a list of identities even if the user is a member of a member of a member, etc. The code for this might look something like below.

    var myGroupIdentities = aras.IomInnovator.newItem("Identity", "get");
    myGroupIdentities.setProperty("id", aras.getIdentityList());
    myGroupIdentities.setPropertyCondition("id", "in");
    myGroupIdentities.setProperty("classification", "Group");
    myGroupIdentities.setAttribute("select", "id,keyed_name");
    myGroupIdentities = myGroupIdentities.apply();

    Chris

    Christopher Gillis

    Aras Labs Software Engineer

  • Thanks Chris. Works now!!!

    But the method says it returns Item, is it returning containing the item data because its called from client?

    public Item applyMethod(
    	string methodName,
    	string body
    )

    getIdentityList - I tried this, but looping through the results and adding to the list took visible performance hit. Trying to convert to sql as I am going 2 levels in the Identity tree as you can see from SQL. Also, this piece of code need not go through access check as per our implementation

  • Hi amitosh,

    I believe you're looking at the description of the applyMethod function available from the Innovator class in the IOM which does return an Item. The functions available from the aras object in a JavaScript method don't map directly with the functions available from the Innovator object in a server method even though some of them will occasionally share the same name.

    A JavaScript representation of the Innovator object is available through a client method, however. If we use this Innovator object (available from aras.IomInnovator), a nicer way of writing the sample I provided in my last comment would instead be:

    var res = aras.IomInnovator.applyMethod("NOV_Identity_Details""body");

    Since this function is called from an Innovator object, the return value is an Item object, so it will save you from needing to any manual conversion from a string to an Item.

    Hope this helps!

    Chris

  • perfect! thanks for the update. Works as expected now