web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Get Related Record details & update in Parent Record using c# in CRM

(0) ShareShare
ReportReport
Posted on by 2,667

Hi All,

I have one lookup field(Contact entity) in Case entity .Need to get lookup details like fields values present in Contact & update in Case fields using C#.

The below code using in Console, but getting error while am updating in Case record with entityId in last line "Invalid Type for entity id value" : 

static void Main(string[] args)
        {
            IOrganizationService _crmService = ConnectToCRM();
            // CreateAutoNumberAttribute(_crmService);
            if (_crmService != null)
            {
                QueryExpression qe = new QueryExpression("incident");
                EntityCollection coll = _crmService.RetrieveMultiple(qe);                                             
                Console.WriteLine("Retrieved : " + coll.Entities.Count);
                Console.ReadLine();
                string entityId = "c35a88e1-4fe5-ea11-a824-001dd8b70014";
                //string entity = "incident";
                //string getassetId = "8E72FA18-26CF-EA11-A827-001DD8B70013"; //8e72fa18-26cf-ea11-a827-001dd8b70013
                //Entity enContact = _crmService.Retrieve("contact", Guid.Parse(entityId), new ColumnSet(true));
                ColumnSet attributes = new ColumnSet(new string[] { "mdc_reportername", "mdc_reporterisnotanmicuser" });
                Entity enCase = _crmService.Retrieve("incident", Guid.Parse(entityId), attributes);
                EntityReference reporterName = enCase.Attributes.Contains("mdc_reportername") ? enCase.GetAttributeValue<EntityReference>("mdc_reportername") : null;
                bool isMICUser = enCase.Contains("mdc_reporterisnotanmicuser") ? enCase.GetAttributeValue<bool>("mdc_reporterisnotanmicuser") : false;
 
                string title = string.Empty;
                string company = string.Empty;
                string dept = string.Empty;
                string emailaddress = string.Empty;
                string mobilephone = string.Empty;
                string lineManager = string.Empty;
                string lineManagerEMail = string.Empty;
                Guid divisionID = Guid.Empty;
                Guid platformID = Guid.Empty;
                Guid aasetID = Guid.Empty;
                Guid reporteId = Guid.Empty;
                bool isuser = false;
 
                if (enCase.Attributes.Contains("mdc_reportername") && reporterName != null)
                {
                    reporteId = (enCase.GetAttributeValue<EntityReference>("mdc_reportername")).Id;
                    Console.WriteLine("Reporter Name From Case : " + reporteId);
                    Console.ReadLine();
                }
                if (enCase.Attributes.Contains("mdc_reporterisnotanmicuser"))
                {
                    isuser = enCase.GetAttributeValue<bool>("mdc_reporterisnotanmicuser");
                    Console.WriteLine("Is MIC User From Case : " + isuser);
                    Console.ReadLine();
                }
 
                string contactFetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                        <entity name='contact'>
                                        <attribute name='contactid' />
                                        <attribute name='mdc_title' />
                                        <attribute name='mdc_platform' />
                                        <attribute name='mdc_linemanageremail' />
                                        <attribute name='mdc_linemanager' />
                                        <attribute name='emailaddress1' />
                                        <attribute name='mdc_division' />
                                        <attribute name='mdc_department' />
                                        <attribute name='mdc_company' />
                                        <attribute name='mdc_asset' />
                                        <filter type='and'>
                                        <condition attribute='mdc_title' operator='not-null' />
                                        <condition attribute='contactid' operator='eq' value='" + reporteId + @"' />
                                          </filter>
                                          </entity>
                                          </fetch>";
 
                FetchExpression fetchQuery = new FetchExpression(contactFetch);
                Console.WriteLine("Get fetch : " + fetchQuery);
                EntityCollection fetchResults = _crmService.RetrieveMultiple(fetchQuery);
                if (fetchResults.Entities.Count() > 0)
                {
                   
                    foreach (Entity item in fetchResults.Entities)
                    {
                        if (item.Attributes.Contains("mdc_asset"))
                        {
                            EntityReference assetValue = item.GetAttributeValue<EntityReference>("mdc_asset");
                            aasetID = assetValue.Id;
                            Console.WriteLine("AssetValue From Fetch : " + aasetID);
                            Console.ReadLine();
                        }
                        if (item.Attributes.Contains("mdc_title"))
                        {
                            title = item["mdc_title"].ToString();
                            Console.WriteLine("title from fetch : " + title);
                            Console.ReadLine();
                        }
 
                        if (item.Attributes.Contains("mdc_division"))
                        {
                            EntityReference divisionValue = item.GetAttributeValue<EntityReference>("mdc_division");
                            divisionID = divisionValue.Id;
                            Console.WriteLine("divisionValue from fetch : " + divisionValue.Name);
                            Console.ReadLine();
                        }
                        if (item.Attributes.Contains("mdc_company"))
                        {
                            company = item["mdc_company"].ToString();
                            Console.WriteLine("Company from fetch : " + company);
                            Console.ReadLine();
                        }
 
                        if (item.Attributes.Contains("mdc_platform"))
                        {
                            EntityReference platformValue = item.GetAttributeValue<EntityReference>("mdc_platform");
                            platformID = platformValue.Id;
                            Console.WriteLine("Platform from fetch : " + platformID);
                            Console.ReadLine();
                        }
                        if (item.Attributes.Contains("mdc_department"))
                        {
                            dept = item["mdc_department"].ToString();
                            Console.WriteLine("Dept from fetch : " + dept);
                            Console.ReadLine();
                        }
                        if (item.Attributes.Contains("emailaddress1"))
                        {
                            emailaddress = item["emailaddress1"].ToString();
                            Console.WriteLine("emailaddress from fetch : " + emailaddress);
                            Console.ReadLine();
                        }
                        if (item.Attributes.Contains("mobilephone"))
                        {
                            mobilephone = item["mobilephone"].ToString();
                            Console.WriteLine("mobilephone from fetch : " + mobilephone);
                            Console.ReadLine();
                        }
                        if (item.Attributes.Contains("mdc_linemanager"))
                        {
                            lineManager = item["mdc_linemanager"].ToString();
                        }
                        if (item.Attributes.Contains("mdc_linemanageremail"))
                        {
                            lineManagerEMail = item["mdc_linemanageremail"].ToString();
                        }
 
                        Entity caseEntity = new Entity("incident");
                        if (!string.IsNullOrEmpty(emailaddress))
                            caseEntity["mdc_reporteremailaddress"] = emailaddress;
                        if (!string.IsNullOrEmpty(company))
                            caseEntity["mdc_reportercompany"] = company;
                        if (!string.IsNullOrEmpty(title))
                            caseEntity["mdc_reportertitle"] = title;
                        if (platformID != null)
                            caseEntity["mdc_platform"] = platformID;
                        if (aasetID != null)
                            caseEntity["mdc_relatedasset"] = aasetID;
                        caseEntity["incidentid"] = entityId;                        
                        _crmService.Update(caseEntity);
                        Console.WriteLine("Updated Case Record : " + caseEntity);
                        Console.ReadLine();
 }
}
Please let me know where am going wrong.
Thanks,
Jharana
I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    Can you replace below line of code.

    FROM

    caseEntity["incidentid"] = entityId;  

    TO

    caseEntity.Id = entityId;  

    Please mark my answer verified if i were helpful

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi Bipin,

    Thnx for the reply I tried with that caseEntity.Id = Guid.parse(entityId);

    but its giving error " Incorrect type of attribute value System.Guid".

    Thanks,

    Jharana

  • Suggested answer
    Jharana Baliyar Singh Profile Picture
    2,667 on at

    Ya its working now.

    if (platformID != null)

                               caseEntity["mdc_platform"] = new EntityReference("contact" ,platformID;

                           if (aasetID != null)

                               caseEntity["mdc_relatedasset"] =new EntityReference("contact" ,aasetID);

    caseEntity.Id = Guid.parse(entityId);

    modified with above lines.

    Thanks,

    Jharana

  • Verified answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Glad that it worked.

    Please mark my answer verified.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

#2
ManoVerse Profile Picture

ManoVerse 60 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 43

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans