locked
Do we need to pass on some user credentials for using queryByAttribute in crm 2011 online? RRS feed

  • Question

  • Hi,

    Do we need to pass on some user credentials for using queryByAttribute in crm 2011 online?

    I have written a query in my plugin as below which is expected to return a GUID for an account.

     QueryByAttribute query = new QueryByAttribute("account");
                                    query.Attributes.AddRange("accountnumber");
                                    query.Values.AddRange("NVISION");

                                    RetrieveMultipleRequest retrieveMultipleRequest = new RetrieveMultipleRequest();
                                    retrieveMultipleRequest.Query = query;


                                    RetrieveMultipleResponse retrieveMultipleResponse = (RetrieveMultipleResponse)service.Execute(retrieveMultipleRequest);

     

                                    Guid accountId = Guid.Empty;

                                    foreach (Entity busEntity in retrieveMultipleResponse.EntityCollection.Entities)
                                    {
                                        // Pick the first accountid. 
                                        accountId = ((Guid)(((Entity)busEntity)["accountid"]));
                                        break;
                                    }

     

    I have got this account NVISION in my test enviornment. I have put same value in account number field as well. please tell me where I  am going wrong.

     

    thank you.

    Friday, August 19, 2011 2:56 PM

Answers

  • Hi,

    You are doing it correctly in a plugin and in plugin you do not need to pass additonal credentials make sure that the account with account number = NVISION exsist in Account records.


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Marked as answer by CRMizer Wednesday, August 24, 2011 1:19 PM
    Friday, August 19, 2011 4:15 PM
  • Try this:

    QueryExpression SelectAccountRec = new QueryExpression
    {
    	EntityName	= "account",
    	ColumnSet	= new ColumnSet( new string[] { "accountid" } ),
    	Criteria	= new FilterExpression()
    };
    
    SelectAccountRec.Criteria.AddCondition("name", ConditionOperator.Equal, "NVISION");
    
    EntityCollection Accounts = _Service.RetrieveMultiple(SelectAccountRec);
    
    Guid AccountId = Guid.Empty;
    
    if (Accounts.Entities.Count > 0)
    	AccountId = Accounts.Entities[0].id;

    • Marked as answer by CRMizer Wednesday, August 24, 2011 1:19 PM
    Monday, August 22, 2011 2:48 PM
  • Hi,

    Make sure that your plugin is registered on Pre-Operation stage or else it will not display in entity id field or user have to refresh the form to view it.


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by Jehanzeb.Javeed Monday, August 22, 2011 10:27 PM
    • Marked as answer by CRMizer Wednesday, August 24, 2011 1:20 PM
    Monday, August 22, 2011 3:33 PM
  • if (!entity.Contains("my_lookupfield"))
    	entity.Attributes.Add("my_lookupfield", new EntityReference("account", AccountId));
    else
    	entity.Attributes["my_lookupfield"] = new EntityReference("account", AccountId);
    

    • Marked as answer by CRMizer Wednesday, August 24, 2011 1:19 PM
    Tuesday, August 23, 2011 6:47 AM

All replies

  • Hi,

    You are doing it correctly in a plugin and in plugin you do not need to pass additonal credentials make sure that the account with account number = NVISION exsist in Account records.


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Marked as answer by CRMizer Wednesday, August 24, 2011 1:19 PM
    Friday, August 19, 2011 4:15 PM
  • Dear Javeed,

    Thank you for the reply. I am still having problems with htis.

    My task is to retrieve GUID for an account record, given is the account name. I have got the record set up for testing. I understand that you have to query the MS CRM db in order to do so, that is the reason i have used queryByAttribuite , request and response as stated in my above email.

    is there any other way I can accomplish the task.

    your help will be much appreciated.

    thank you in advance.

    Monday, August 22, 2011 2:29 PM
  • Try this:

    QueryExpression SelectAccountRec = new QueryExpression
    {
    	EntityName	= "account",
    	ColumnSet	= new ColumnSet( new string[] { "accountid" } ),
    	Criteria	= new FilterExpression()
    };
    
    SelectAccountRec.Criteria.AddCondition("name", ConditionOperator.Equal, "NVISION");
    
    EntityCollection Accounts = _Service.RetrieveMultiple(SelectAccountRec);
    
    Guid AccountId = Guid.Empty;
    
    if (Accounts.Entities.Count > 0)
    	AccountId = Accounts.Entities[0].id;

    • Marked as answer by CRMizer Wednesday, August 24, 2011 1:19 PM
    Monday, August 22, 2011 2:48 PM
  • Hi Jan,

    Thank for that I am still working with this, I had another thing I am stuck with, as to how will I be able to display the returning Guid in a field. I have a thinking that

    entity.Attributes.Add("Field_to_be_updated", AccountId);

    Please note Field_to_be_updated is a Lookup field.

    Thank you, Your help is much appreciated.

     

    Monday, August 22, 2011 3:29 PM
  • Hi,

    Its correct, lookup field value can be added/updared by passing the Entity Id (AccountId) GUID value


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Monday, August 22, 2011 3:32 PM
  • Hi,

    Make sure that your plugin is registered on Pre-Operation stage or else it will not display in entity id field or user have to refresh the form to view it.


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by Jehanzeb.Javeed Monday, August 22, 2011 10:27 PM
    • Marked as answer by CRMizer Wednesday, August 24, 2011 1:20 PM
    Monday, August 22, 2011 3:33 PM
  • if (!entity.Contains("my_lookupfield"))
    	entity.Attributes.Add("my_lookupfield", new EntityReference("account", AccountId));
    else
    	entity.Attributes["my_lookupfield"] = new EntityReference("account", AccountId);
    

    • Marked as answer by CRMizer Wednesday, August 24, 2011 1:19 PM
    Tuesday, August 23, 2011 6:47 AM
  • Dear All,

    Thank you to you guys especially Jan AS and Jehanzeb Javeed. Finally was able to complete this thing.

    Thank you.

    Tuesday, August 23, 2011 12:11 PM
  • Hi,

    I am glad that it helps, if the any of the above post was helpful then please Mark as Answers.


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Tuesday, August 23, 2011 12:15 PM