从Microsoft CRM 2011 Online中检索多个帐户

时间:2012-09-12 06:53:02

标签: java dynamics-crm-2011

使用Microsoft CRM 2011在线并使用webservices,我使用webservices调用创建的OrganizationServiceStub类在我的Main.java中使用下面的方法。检索到的输出没有记录是-1可以帮助我出错的地方。我想检索名称以“Tel”开头而不给出accountid的帐户。我可以看到CRM中存在数据。 感谢

public static void getAccountDetails(OrganizationServiceStub service, ArrayOfstring fields)
{
    try{
    ArrayOfanyType aa = new ArrayOfanyType();   
    aa.setAnyType(new String[] {"Tel"});

    ConditionExpression condition1 = new ConditionExpression();
    condition1.setAttributeName("name");


    condition1.setOperator(ConditionOperator.BeginsWith);
    condition1.setValues(aa);

    ArrayOfConditionExpression ss = new ArrayOfConditionExpression();
    ss.setConditionExpression(new ConditionExpression[] {condition1});

    FilterExpression filter1 = new FilterExpression();
    filter1.setConditions(ss);

    QueryExpression query = new QueryExpression();
    query.setEntityName("account");

    ColumnSet cols = new ColumnSet();
    cols.setColumns(fields);
    query.setColumnSet(cols);

    query.setCriteria(filter1);

    RetrieveMultiple ll = new RetrieveMultiple();
    ll.setQuery(query);

    RetrieveMultipleResponse result1 = service.retrieveMultiple(ll);

    EntityCollection accounts = result1.getRetrieveMultipleResult();

    System.out.println(accounts.getTotalRecordCount());        
} 
catch (IOrganizationService_RetrieveMultiple_OrganizationServiceFaultFault_FaultMessage e) {
    logger.error(e.getMessage());
    e.printStackTrace();
} 
catch (RemoteException e) {
    logger.error(e.getMessage());
    e.printStackTrace();
}
}

2 个答案:

答案 0 :(得分:3)

对于Java,请将此代码段包含在上述问题中

ArrayOfanyType aa = new ArrayOfanyType();   
aa.setAnyType(new String[] {"555"});
ConditionExpression condition1 = new ConditionExpression();

condition1.setAttributeName("telephone1");

condition1.setOperator(ConditionOperator.BeginsWith);
condition1.setValues(aa);

ArrayOfConditionExpression ss = new ArrayOfConditionExpression();
ss.setConditionExpression(new ConditionExpression[] {condition1});

FilterExpression filter1 = new FilterExpression();
filter1.setConditions(ss);

QueryExpression query = new QueryExpression();
query.setEntityName("account");

PagingInfo pagingInfo = new PagingInfo();
pagingInfo.setReturnTotalRecordCount(true);

query.setPageInfo(pagingInfo);

OrganizationServiceStub.ColumnSet colSet = new OrganizationServiceStub.ColumnSet();
OrganizationServiceStub.ArrayOfstring cols = new OrganizationServiceStub.ArrayOfstring();

cols.setString(new String[]{"name", "telephone1", "address1_city"});

colSet.setColumns(cols);

query.setColumnSet(colSet);
query.setCriteria(filter1);

RetrieveMultiple ll = new RetrieveMultiple();
ll.setQuery(query);

OrganizationServiceStub.RetrieveMultipleResponse response = serviceStub.retrieveMultiple(ll);
EntityCollection result = response.getRetrieveMultipleResult();
ArrayOfEntity attributes = result.getEntities();

Entity[] keyValuePairs = attributes.getEntity();

for (int i = 0; i < keyValuePairs.length; i++) {
    OrganizationServiceStub.KeyValuePairOfstringanyType[] keyValuePairss = keyValuePairs[i].getAttributes().getKeyValuePairOfstringanyType();
    for (int j = 0; j < keyValuePairss.length; j++) {
         System.out.print(keyValuePairss[j].getKey() + ": ");
         System.out.println(keyValuePairss[j].getValue());
    }
}

答案 1 :(得分:0)

不确定您的EntityCollection对象与SDK中的.Net版本有多相似,但是您需要在.Net中的查询的PagingInfo中为ReturnTotalRecordCount属性指定TotalRecordCount有价值。您可以不检查accounts.Entities.Count吗?

注意:我也不是Java家伙......