Asked by:
create contact with lead fullname on click of ribbon button

Question
-
I have written a plugin to create a contact with same name of lead(fullname) onclick of ribbon button(on lead form)... and its working fine.
Now i want to keep a condition that the contact should be created only if there is no record with that name.. if there is already a contact record with the name then it should throw an error.. How do i add this to the below codeif (entity.LogicalName == "lead") { Entity resultset = service.Retrieve("lead", entity.Id, new ColumnSet(new String[] { "new_option","fullname" })); if (Convert.ToInt32(resultset.Attributes.Contains("new_option") )==1) { Entity Contact = new Entity("contact"); Contact["lastname"] = resultset["fullname"]; if (context.OutputParameters.Contains("id")) { Guid ids = new Guid(context.OutputParameters["id"].ToString()); } service.Create(Contact);
Monday, May 26, 2014 11:09 AM
All replies
-
Hi,
Try the following code:
if (entity.LogicalName == "lead") { Entity resultset = service.Retrieve("lead", entity.Id, new ColumnSet(new String[] { "new_option","fullname" })); if (Convert.ToInt32(resultset.Attributes.Contains("new_option") )==1) { if (resultset.Attributes.Contains("fullname")) { QueryExpression _query = new QueryExpression { EntityName = "contact", ColumnSet = new ColumnSet(true), Criteria = { FilterOperator = LogicalOperator.And, Conditions = { new ConditionExpression { AttributeName="fullname", Operator=ConditionOperator.Equal, Values=resultset["fullname"] } } } }; EntityCollection _contacts = service.RetrieveMultiple(_Query); if (_contacts.Entities.Count > 0) { throw InvalidPluginException("Contact already exists with the same name"); } Entity Contact = new Entity("contact"); Contact["lastname"] = resultset["fullname"]; if (context.OutputParameters.Contains("id")) { Guid ids = new Guid(context.OutputParameters["id"].ToString()); } service.Create(Contact); } } }
Monday, May 26, 2014 4:06 PM -
Iam facing an error in the following syntax..
Property or indexer 'Values' cannot be assigned to -- it is read only
Tuesday, May 27, 2014 6:21 AM -
Hi,
Could you try with .ToString()
Conditions = { new ConditionExpression { AttributeName="fullname", Operator=ConditionOperator.Equal, Values=resultset["fullname"].ToString() } }
Tuesday, May 27, 2014 6:23 AM -
not working :(
Tuesday, May 27, 2014 6:35 AM -
Hi amy,
Try the code below , it works fine.Pls try add your values to your condition. Also i suggest you to use Linq in Crm. It is better using early bound and easy to write.
QueryExpression expression = new QueryExpression();
expression.EntityName = "contact";
expression.ColumnSet = new ColumnSet(true);
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "fullname";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(Convert.ToString(resultset["fullname"]));
expression.Criteria.AddCondition(condition);
EntityCollection _contacts = service.RetrieveMultiple(expression);
Mark as answered if it is helpful for you.
- Proposed as answer by Polat Aydın[MCP] Tuesday, May 27, 2014 8:30 AM
Tuesday, May 27, 2014 8:13 AM