Retrieve Contact resords of an Account using Plugins
-
Thursday, November 22, 2012 1:32 PM
Hello Experts I want to retrieve contact records of an account using Plugins. I am new in Plugins. I have gone through some posts but i am not getting how to resolve it. Please guide me
Thank you
saroj
All Replies
-
Thursday, November 22, 2012 1:44 PM
Hi,
Here is the same code
QueryExpression _Query = new QueryExpression
{
EntityName = "contact",
ColumnSet = new ColumnSet(true),
Criteria =
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName="parentcustomerid",
Operator=ConditionOperator.Equal,
Values={_AccountID}
}
}
}
};
EntityCollection _Entities = service.RetrieveMultiple(_Query);
-
Thursday, November 22, 2012 2:11 PM
Hi Vikram Thanks for your reply. According to you i am doing like this but now the contact form is not opening and throwing an error.
Here i want that if the account has less than 10 record i want to populate a field lets take firstname field of contact form. Here is my code.
public void Execute(IServiceProvider serviceProvider)
{
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory =(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService crmService = serviceFactory.CreateOrganizationService(context.UserId);
EntityCollection _Entities=null;
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "account")
{
Guid accountId = entity.Id;
QueryExpression _Query = new QueryExpression
{
EntityName = "contact",
ColumnSet = new ColumnSet(true),
Criteria =
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName="parentcustomerid",
Operator=ConditionOperator.Equal,
Values={accountId}
}
}
}
};
_Entities = crmService.RetrieveMultiple(_Query);
}
if (_Entities.TotalRecordCount < 10)
{
Entity ent = new Entity("contact");
ent.Attributes.Add("firstname", "Saroj");
}
}
saroj
- Edited by Saroj kumar Das Thursday, November 22, 2012 2:12 PM
-
Thursday, November 22, 2012 3:47 PM
Hi,
what is the datatype of the field ? which one you want to populate.
in which event and stage have you registered above plugin ?
It seems you have registered this plugin in account entity and you want to update contact field but here is my question, you want to update all contact's field (i.e firstname field) ??
-
Friday, November 23, 2012 4:31 AM
HI Vikram Thank you for reply. The Problem is first i have to open an account record then i will select the contact to check how many contacts are there under the account and if i will add a new contact to the account, there is a field of type int in which it will show the contact number. e.g if there are 3 contact already and if i add another one it will show 4 in the field. As i am new in plugin i am not getting the idea how to resolve it. i have done this problem in javascript. Now i want to do it in Plugin.Please Guide me.
Thank you.
saroj
-
Friday, November 23, 2012 5:25 AM
Hi,
You need to write Pre-Create and Pre-Update plugin (set filtering attribute parentcustomerid in update step).
public void Execute(IServiceProvider serviceProvider)
{
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory =(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService crmService = serviceFactory.CreateOrganizationService(context.UserId);
EntityCollection _Entities=null;
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "account")
{
Guid accountId = entity.Id;
QueryExpression _Query = new QueryExpression
{
EntityName = "contact",
ColumnSet = new ColumnSet(true),
Criteria =
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName="parentcustomerid",
Operator=ConditionOperator.Equal,
Values={accountId}
}
}
}
};
_Entities = crmService.RetrieveMultiple(_Query);
}
entity.Attributes.Add("new_total", _Entities.TotalRecordCount);
} -
Friday, November 23, 2012 7:16 AM
HI Vikram, i changed the requirement in some extend. i have specified the primary entity as contact and from contact entity i have to retrieve how many contacts are there against the parentcustomerid. I am getting the parentcustomerid. but when i tried to count it's returning -1. I am pasting the code please suggest where i am wrong.
EntityCollection _Entities = null;
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "contact")
{
if (entity.Attributes.Contains("parentcustomerid"))
{
EntityReference parentCustomer = (EntityReference)entity.Attributes["parentcustomerid"];
accountId = parentCustomer.Id;
entity.Attributes.Add("pager", accountId.ToString());
}
QueryExpression _Query = new QueryExpression
{
EntityName = "contact",
ColumnSet = new ColumnSet(true),
Criteria =
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName="parentcustomerid",
Operator=ConditionOperator.Equal,
Values={accountId}
}
}
}
};
_Entities = crmService.RetrieveMultiple(_Query);
entity.Attributes.Add("new_total", _Entities.TotalRecordCount);
}
}saroj
-
Friday, November 23, 2012 7:23 AM
Sorry, You need to use like below
entity.Attributes.Add("new_total", _Entities.Entities.Count + 1);
- Edited by _Vikram Friday, November 23, 2012 7:24 AM
- Marked As Answer by Saroj kumar Das Friday, November 23, 2012 7:27 AM
-
Friday, November 23, 2012 7:28 AM
Thank you Vikram. It's Working now.
Thank you again.
saroj
-
Friday, November 23, 2012 11:19 AM
HI vikram i have another requirement to get some Optionset Values according to parentcustomerid. So i am doing like the below. Here i am not getting output. Is it right approach or i should go for RetrieveAttributeRequest and RetrieveAttributeResponse type.
QueryExpression selectQuery = new QueryExpression
{
EntityName = "contact",
ColumnSet = new ColumnSet(new String[] { "new_optionValue", }),
Criteria =
{
Conditions =
{
new ConditionExpression
{
AttributeName="parentcustomerid",
Operator=ConditionOperator.Equal,
Values={accountId}
}
}
}
};EntityCollection optionValue = crmService.RetrieveMultiple(_Query);
entity.Attributes.Add("address1_name",optionValue[0].ToString());// To test whether the value is coming or not
Please suggest where i am wrong.
Thank you
saroj
- Edited by Saroj kumar Das Friday, November 23, 2012 11:21 AM
-
Friday, November 23, 2012 11:44 AM
HI I am also doing like below to retrieve the optionset value. But the error is "An Item with the same key is already added". So i commented the EntityLogicalName="contact". Now the error is "Invalid Argument" In Error Log File the Message is "Attribute Name/Column Number and Entity Name are required when attribute id is not specified"
Here is my code below
RetrieveAttributeRequest request= new RetrieveAttributeRequest()
{
RequestName="RetrieveAttribute",
//EntityLogicalName="contact",
LogicalName="new_optionValue",
RetrieveAsIfPublished=true
};RetrieveAttributeResponse retrieveAttributeResponse =(RetrieveAttributeResponse)crmService.Execute(request);
PicklistAttributeMetadata retrievedPicklistAttributeMetadata =(PicklistAttributeMetadata)
retrieveAttributeResponse.AttributeMetadata;
OptionMetadata[] optionList =retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
entity.Attributes.Add("address1_name", optionList[0].Value);// To TestPlease suggest
Thank you
saroj
- Edited by Saroj kumar Das Friday, November 23, 2012 11:45 AM
- Edited by Saroj kumar Das Friday, November 23, 2012 11:46 AM