Asked by:
Plug-in that populates an Entity LOOKUP derived from another Entity

Question
-
New to Plugin coding and need help figuring out how to derive a lookup fromone entity to another entity
Document Queue (vbs_crm_documentqueue)
- Connect to Document XREF (vbs_vbs_crm_documentincidentxref) via concatenation of Category – SubCategory
- Primary Field - vbs_documenttypecd_subtypecd
- Returning Below
- Rule Department (LOOKUP TO Rule Department)
- Rule Category (LOOKUP TO Rule Category)
- Rule SubCategory (LOOKUP TO SubCategory)
- Trying to Populate above Retrieved LOOKUP’s back into Document Queue which has the same Exact lookup fields
- Rule Department (LOOKUP TO Rule Department)
- Rule Category (LOOKUP TO Rule Category)
- Rule SubCategory (LOOKUP TO SubCategory)
- Trying to Populate above Retrieved LOOKUP’s back into Document Queue which has the same Exact lookup fields
Service.Update highlighted below is ABORTING!!!
// Link to Entity VBS_CRM_DocumentIncidentXREF to retrieve Department, Category, SubCategory
try
{
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "vbs_documenttypecd_subtypecd";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(lookupkey.ToString());
//create a column set.
ColumnSet columns = new ColumnSet("vbs_documenttypecd_subtypecd" , "vbs_ruledepartmentid", "vbs_rulecategoryid", "vbs_rulesubcategoryid");
//Create query expression
QueryExpression query1 = new QueryExpression();
query1.ColumnSet = columns;
query1.EntityName = "vbs_vbs_crm_documentincidentxref";
query1.Criteria.AddCondition(condition);
EntityCollection result1 = service.RetrieveMultiple(query1);
int result1count = result1.Entities.Count;
if (result1count == 1)
{
var r = result1.Entities[0];
foreach(var c in r.Attributes)
{
if (r.Attributes.Contains("vbs_ruledepartmentid"))
{
Guid deptGUID = ((EntityReference)r.Attributes["vbs_ruledepartmentid"]).Id;
docqueue.Attributes["vbs_ruledepartmentid"] = deptGUID;
}
if (r.Attributes.Contains("vbs_rulecategoryid"))
{
Guid catGUID = ((EntityReference)r.Attributes["vbs_rulecategoryid"]).Id;
string cat = ((EntityReference)r.Attributes["vbs_rulecategoryid"]).Name;
docqueue.Attributes["vbs_rulecategory"] = catGUID;
docqueue.Attributes["vbs_category"] = cat;
}
if (r.Attributes.Contains("vbs_rulesubcategoryid"))
{
Guid subcatGUID = ((EntityReference)r.Attributes["vbs_rulesubcategoryid"]).Id;
string subcat = ((EntityReference)r.Attributes["vbs_rulesubcategoryid"]).Name;
docqueue.Attributes["vbs_rulesubcategoryid"] = subcatGUID;
docqueue.Attributes["vbs_subcategory"] = subcat;
}
// service.Update(docqueue);
}
Monday, November 14, 2016 7:03 AM - Connect to Document XREF (vbs_vbs_crm_documentincidentxref) via concatenation of Category – SubCategory
All replies
-
Do you get an error - if so, what is it (you'll get most information by catching it as a FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> and accessing the Detail.Message property)
It looks like you're trying to set some attributes as a Guid type (e.g. vbs_rulesubcategoryid), but these should be passed as an EntityReference, which you could with:
docqueue.Attributes["vbs_rulesubcategoryid"] = r.Attributes["vbs_rulesubcategoryid"]
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
Monday, November 14, 2016 11:52 AMModerator -
try this:-
EntityReference myEntity = r.Attributes["vbs_rulesubcategoryid"] docqueue.Attributes["vbs_rulesubcategoryid"] = EntityReference(myEntity.LogicalName, myEntity.Id);
Please check your source and destination attribute schema names are same.
Regards Faisal
Wednesday, November 23, 2016 1:39 PM