Creating Relationship between a lead and sales literature in Dynamics 365 CRM

263 views Asked by At

I just got a requirement for asp.net webforms app, which has to integrate with MS Dynamics 365 CRM. I have never done dev for Dynamics 365 but anyhow I've managed to connect with CRM using .net sdk and created a Lead and Sales Literature entity. My intent is to achieve what we can do by going to Dynamics 365 portal --> Lead --> related --> Activities--> Sales Literature:

enter image description here

So, I want to establish relationship between these two entities (lead and sales literature) using .net sdk, here is my code:

 AssociateRequest association = new AssociateRequest

            {

                Target = new EntityReference(leadEntity.LogicalName, leadid),

                RelatedEntities = new EntityReferenceCollection

                {


                 new EntityReference(SLEntity.LogicalName, SLID)
                },

                Relationship = new Relationship("Lead_SalesLiterature"),
                RequestId = new Guid()
            };

           // Execute the request.

           CRMService.Execute(association);

But the code fails to establish the relationship on CRMService.Execute(association); saying that:

System.ServiceModel.FaultException`1: 'The Entity Relationship with SchemaName = 'SalesLiterature_Lead' was not found in the MetadataCache'

I have checked both Lead Entity Reference and Sales Literature Entity Reference but not finding Schema name for this relationship. Am I missing something or this is not possible?

2

There are 2 answers

0
Vahid Samimi On

this error show to you that your relation name is wrong so you should find correct relation name.so you need to go CRM customization and under the SalesLiterature entity in relationships find correct schema name

2
Arun Vinoth-Precog Tech - MVP On

I strongly believe that the "Sales Literature" showed in your screenshot is a custom activity entity, and it's not the same as OOB "Sales Literature" entity.

To create a custom entity (activity) with a Lead as regardingobjectid can be done using this code. Just replace the task with your entity name

           Entity followup = new Entity("task");
           followup["subject"] = "Sample task - an activity";
           followup["description"] = "Sample description";

           followup["scheduledstart"] = DateTime.Now;
           followup["scheduledend"] = DateTime.Now.AddDays(2);

           Guid regardingobjectid = new Guid("26ADDD07-D9F4-E711-8138-E0071B715B11"); //leadid
           string regardingobjectidType = "lead";
           followup["regardingobjectid"] = new EntityReference(regadingobjectidType,regardingobjectid);

           // Create the followup activity
           CRMService.Create(followup);