pravinnalkande - Monday, February 6, 2012 12:54 AM:
Hi All,
When I execute the following method i get an error like 'Item is not a collection'
Please help me.What is the problem in below method.
---------------------------------------
Innovator inn= this.getInnovator();
Item result=null;
Item myItem=null;
string itemTypeName= this.getProperty("ItemType");
string []newTypeName=itemTypeName.Split('=');
//return inn.newError(newTypeName.Length.ToString());
for(int typeCount=0;typeCount<newTypeName.Length-1;typeCount++)
{
string Test=newTypeName[newTypeName.Length - 1];
Item itemType= this.newItem("ItemType","get");
itemType.setProperty("name",Test);
itemType=itemType.apply();
Item requiredProperty= this.newItem("Property","get");
requiredProperty.setProperty("is_required","1");
requiredProperty.setProperty("is_hidden","0");
requiredProperty.setProperty("is_hidden2","0");
requiredProperty.setProperty("source_id",itemType.getItemByIndex(typeCount).getProperty("id"));
requiredProperty=requiredProperty.apply();
//return inn.newError(requiredProperty.getItemCount().ToString());
string createItemAML="<AML><Item type=""+Test+"" action="add">";
string value="";
if(requiredProperty.getItemCount()>0)
{
for(int i=0;i<requiredProperty.getItemCount();i++)
{
if(requiredProperty.getItemByIndex(i).getProperty("data_type")=="string")
{
value="";
}
else if(requiredProperty.getItemByIndex(i).getProperty("data_type")=="date")
{
value=DateTime.Now.ToString("s");
}
else
{
value="0";
}
createItemAML=createItemAML+"<"+requiredProperty.getItemByIndex(i).getProperty("name")+">"+value+"</"+requiredProperty.getItemByIndex(i).getProperty("name")+">";
}
}
createItemAML=createItemAML+"</Item></AML>";
myItem=this.newItem("sg_mapping","get");
myItem.apply();
result=inn.applyAML(createItemAML);
myItem.appendItem(result);
}
return myItem;
----------------------------------------
Can anyone tell me what is the problem inside this method?
Cheers,
Pravin
Brian - Thursday, February 9, 2012 5:49 AM:
Hi Pravin,
to start with "ItemType" is an attribute not a property.
Use this.getType() to retrieve the item type.
This will be a single string and so you should not "split" into a array.
I suspect that when you call "itemType = itemType.apply()" you are actually getting an error returned. So when you try to retrieve a property from this faulty Item it returns an error.
It is always good to test the result of an "apply()" call.
eg. Item result = query.apply();
if (result.isError()){
// react to the error
}
if ( result.isEmpty()){
// there is nothing that has been returned.
}
if ( result.isCollection()){
// you are dealing with more than one Item
}
Now you are confident that if you get to this code the "result" item is OK.
You don't always need to check all of these.
You can use:
for (int i = 0; i < result.getItemCount(); i++){
Item instance = result.getItemByIndex(i);
string value = instance.getProperty("propName","");
}
Try this,
Cheers,
Brian.