Hello
Im trying to write a plugin so that when a record is created it searches for a matching record with the same email address and then creates a relationship to the existing record.
This is what ive got, first issue is the search is finding itself and then i just get an error at the service.Update point
So i dont think the guid from the record found in the FetchXML is coming across but can't work out how to get it or how i exclude itself from the search!
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity ent = (Entity)context.InputParameters["Target"];
ContactEmailAddress1 = ent.GetAttributeValue<string>("emailaddress1");
if (ContactEmailAddress1 != null)
{
string LeadFetchXml = @"<fetch mapping='logical' version='1.0' output-format='xml - platform' distinct='false'>
<entity name = 'lead'>
<attribute name = 'fullname'/>
<attribute name = 'leadid'/>
<attribute name = 'mobilephone'/>
<attribute name = 'emailaddress3'/>
<attribute name = 'emailaddress2'/>
<attribute name = 'emailaddress1'/>
<attribute name = 'parentcontactid'/>
<attribute name = 'leadenquiry'/>
<attribute name = 'enquirylookup'/>
<order descending = 'false' attribute = 'fullname'/>
<filter type = 'and'>
<filter type ='and'>
<filter type = 'or'>";
if (ContactEmailAddress1 != null)
LeadFetchXml += @"<condition attribute='emailaddress1' operator='eq' value='" + ContactEmailAddress1.ToString() + @"'/>
<condition attribute='emailaddress2' operator='eq' value='" + ContactEmailAddress1.ToString() + @"'/>
<condition attribute='emailaddress3' operator='eq' value='" + ContactEmailAddress1.ToString() + @"'/>";
LeadFetchXml += @"
</filter>
<condition attribute='parentcontactid' operator='null' />
</filter>
</filter>
</entity>
</fetch>";
EntityCollection sresult = service.RetrieveMultiple(new FetchExpression(LeadFetchXml));
{
foreach (var c in sresult.Entities)
{
//How do i exclude itself here?????
ParentGuid = (Guid)c.Attributes["leadid"];
ent.Attributes["leadenquiry"] = new EntityReference("lead", ParentGuid);
ent.Attributes["enquirylookup"] = new EntityReference("lead", ParentGuid);
//If i exclude itself, it then Fails here
service.Update(ent);
}
}
}
}
}
}
}
*This post is locked for comments
I have the same question (0)