locked
Exception "The entity with a name = 'new_employee' was not found in the MetadataCache."} while running the CRUD operation RRS feed

  • Question

  • Hi All,

    I am creating a Sample CRUD application in CRM 2013 on premise.

    I am using following two classes for my console application.

    deviceismanager.cs

    OnlineService.cs

    Below Is my reference file:-

    Below is the Crud operation class:-

    class CrudOperations
            {
            public void CreateRecord()
                {
    
                IOrganizationService service = OnlineService.servproxy;//To get CRM Service
    
                if (service != null)
                    {
    
                        Entity myentity = new Entity("new_employee");
    
                        myentity.Attributes["new_name"] = "Rahul";
    
                        myentity.Attributes["new_ename"] = "Veldandi";
    
                        myentity.Attributes["new_employeesalary"] = new Money(23000);//Currency
    
                        myentity.Attributes["new_employeelocation"] = new OptionSetValue(100000003);//optionset
    
                        myentity.Attributes["new_employeehobbies"] = "TV \n Playing Cricket";//MultiLineText
    
                        myentity.Attributes["new_employeegender"] = false;//Two Options                myentity.Attributes["new_employeeaccount"] = new EntityReference("account", new Guid("A478321B-E44C-E511-8102-C4346BADA644"));//Lookup
    
                        myentity.Attributes["new_department"] = new EntityReference("new_department", new Guid("E07CB8D2-6C50-E511-810E-C4346BAD5740"));//Lookup
    
                        myentity.Attributes["createdon"] = new DateTime(2015, 09, 11);//Date Time
    
                        service.Create(myentity);
    
                        Console.WriteLine("Record Created Successfully");
    
                    }
    
                }
    
            public void RetrieveRecord()
                {
    
                    Entity et = OnlineService.servproxy.Retrieve("new_employee", new Guid("BF18416B-2E58-E511-810E-C4346BAD3768"), new ColumnSet(true));
    
                    string name = et["new_name"].ToString();
    
                    string hobbies = et["new_employeehobbies"].ToString();
    
                    string account = et.GetAttributeValue<EntityReference>("new_employeeaccount").Name;
    
                    string gender = et.FormattedValues["new_employeegender"].ToString();
    
                    string location = et.FormattedValues["new_employeelocation"].ToString();
    
                    string dept = et.GetAttributeValue<EntityReference>("new_department").Name;
    
                    decimal sal = et.GetAttributeValue<Money>("new_employeesalary").Value;
    
                    Console.WriteLine("employee Name:" + name);
    
                    Console.WriteLine("employee hobbies:" + hobbies);
    
                    Console.WriteLine("employee account" + account);
    
                    Console.WriteLine("employee gender" + gender);
    
                    Console.WriteLine("employee location" + location);
    
                    Console.WriteLine("employee department" + dept);
    
                    Console.WriteLine("employee salary" + sal);
    
                }
    
            public void UpdateRecord()
                {
    
                    Entity myet = OnlineService.servproxy.Retrieve("new_employee", new Guid("BF18416B-2E58-E511-810E-C4346BAD3768"), new ColumnSet("new_employeesalary", "new_employeelocation", "new_employeeaccount", "new_employeehobbies"));
    
                    Console.WriteLine("Enter Employee salary");
    
                    decimal sal = Convert.ToDecimal(Console.ReadLine());
    
                    myet["new_employeesalary"] = new Money(sal); ;
    
                    myet["new_employeelocation"] = new OptionSetValue(100000002);
    
                    myet["new_employeeaccount"] = new EntityReference("account", new Guid("9C78321B-E44C-E511-8102-C4346BADA644"));
    
                    myet["new_employeehobbies"] = "Watching TV";
    
                    OnlineService.servproxy.Update(myet);
    
                    Console.WriteLine("Record Updated Successfully");
    
                }
    
            public void delete()
                {
    
                    OnlineService.servproxy.Delete("new_employee", new Guid("24E6B2EF-7B57-E511-810A-C4346BADB280"));
    
                    Console.WriteLine("Record Deleted Sucessfully");
    
                }
    
            public void multiretrive()
                {
    
                    QueryExpression query = new QueryExpression();
    
                    query.EntityName = "new_employee";
    
                    query.ColumnSet = new ColumnSet(true);
    
                    RetrieveMultipleRequest request = new RetrieveMultipleRequest();
    
                    request.Query = query;
    
                    RetrieveMultipleResponse responce = (RetrieveMultipleResponse)OnlineService.servproxy.Execute(request);
    
                foreach (Entity emp in responce.EntityCollection.Entities)
                    {
    
                        string name = emp["new_name"].ToString();
    
                        string hobbies = emp["new_employeehobbies"].ToString();
    
                        string account = emp.GetAttributeValue<EntityReference>("new_employeeaccount").Name;
    
                        string gender = emp.FormattedValues["new_employeegender"].ToString();
    
                        string location = emp.FormattedValues["new_employeelocation"].ToString();
    
                        string dept = string.Empty;
    
                        if (emp.Attributes.Contains("new_department"))
                            {
    
                            dept = emp.GetAttributeValue<EntityReference>("new_department").Name;
    
                            }
    
                        decimal sal = emp.GetAttributeValue<Money>("new_employeesalary").Value;
    
                        Console.WriteLine("employee Name : " + name);
    
                        Console.WriteLine("employee hobbies : " + hobbies);
    
                        Console.WriteLine("employee account : " + account);
    
                        Console.WriteLine("employee gender : " + gender);
    
                        Console.WriteLine("employee location : " + location);
    
                        Console.WriteLine("employee department : " + dept);
    
                        Console.WriteLine("employee salary : " + sal);
    
                    }
    
                }
            }

    below is my Program file:

    
    class Program
            {
            static void Main(string[] args)
                {
                     Console.WriteLine("Enter Your Choice: C for Create , U for update  , D for delete");
    
                char ch =char.Parse(Console.ReadLine());
    
                switch (ch)
    
                {
    
                        case 'c':
    
                            CrudOperations c = new CrudOperations();
                            c.CreateRecord();
                            break;
    
                        case 'u':
    
                            CrudOperations u = new CrudOperations();
                            u.UpdateRecord();
                            break;
    
                        case 'd':
    
                            CrudOperations d = new CrudOperations();
                            d.delete();
                            break;
    
                        case 'e':
                            CrudOperations r= new CrudOperations();
                            r.RetrieveRecord();
                            break;
    
                        case 'f':
                            CrudOperations mr =new CrudOperations();
                            mr.multiretrive();
                            break;
    
                        default:
    
                            Console.WriteLine("Please Choose any one from given choices");
                            break;
    
                        }
    
                    Console.ReadLine();      
                }
            }

    When I am running the code, Service get created then I get exception of " the entity with a name= 'new_employee' was not found in metadata cache". Any help greatly appreciated.

    Tuesday, August 9, 2016 10:38 AM

All replies

  • Have you created this entity in CRM, and with the same name ?? make sure you are using the same logical name, which is there in your CRM organization.

    Thanks


    Mahender |Our Blog
    "Make sure to click on 'Vote as Helpful' and 'Mark As Answer', if you get answer of your question


    Wednesday, August 10, 2016 6:00 AM
    Moderator
  • Please make sure that you are connecting the correct organisation as the issue seen that the entity is not present in MetadataCache.
     
    Hope you have created this entity in the CRM, please make sure you published too.
     
    Also check the entity logical name which you are using is same as in the CRM.
     
    Hope this help.

    Inogic | Blog | news@inogic.com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Wednesday, August 10, 2016 11:40 AM