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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to set 2 entities value in execute multiple request.

(0) ShareShare
ReportReport
Posted on by

Hii, maybe it just a fool question. But i want to know how can i set value for 2 entities programatically in a executemultipleresuest class. Something like: Set value for contact name and account name in a plugin. I would be very gratefull if you show me a snippet code. 

EDITED

i found this code : 

private EntityCollection GetCollectionOfEntitiesToCreate()
        {
            return new EntityCollection()
            {
                EntityName = Account.EntityLogicalName,
                Entities = {
                    new Account { Name = "Example Account 1" },
                    new Account { Name = "Example Account 2" },
                    new Account { Name = "Example Account 3" },
                    new Account { Name = "Example Account 4" },
                    new Account { Name = "Example Account 5" }
                }
            };
        }


but i can't found EntityLogicalName right after (Account.) Also i want to ask you, there are "Example accout" from 1-5. Are they the entity logical name ? So i change "example accout" with "new_customentity" ?

 

regards,

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Aman Kothari Profile Picture
    on at

    Hi,

    May be if I understand right then you can achieve this using below code

    public void Execute(IServiceProvider serviceProvider)

       {

           //Extract the tracing service for use in debugging sandboxed plug-ins.

           ITracingService tracingService =

               (ITracingService)serviceProvider.GetService(typeof(ITracingService));

           // Obtain the execution context from the service provider.

           IPluginExecutionContext context = (IPluginExecutionContext)

               serviceProvider.GetService(typeof(IPluginExecutionContext));

           // The InputParameters collection contains all the data passed in the message request.

           if (context.InputParameters.Contains("Target") &&

               context.InputParameters["Target"] is Entity)

           {

               //For Account

               Entity account = new Entity("account");

               account["name"] = "sample account name";

               //For Contact

               Entity contact = new Entity("contact");

               contact["firstname"] = "first name";

               contact["lastname"] = "last name";

           }

       }

    You can also refer below links for create simple plugins

    msdn.microsoft.com/.../gg594416.aspx

    crmbook.powerobjects.com/.../developing-a-plug-in

    www.c-sharpcorner.com/.../create-plug-in-of-ms-dynamic-crm-2013-in-Asp-Net

    Hope it'll help you.

    Thanks

  • Community Member Profile Picture
    on at

    Hii, aman. Yeah it would be something like that. But how about the next line ?

    Like :

    service.create(account);

    service.create(contact);

    Is it possible to do that thing ?

  • Suggested answer
    Aman Kothari Profile Picture
    on at

    Yes you can use service.create(account) and service.create(contact) at the end of the code,

    It'll work.

  • Community Member Profile Picture
    on at

    Hi aman, thank you  for your answer, but it seem the method  didn't work for my case. The only method that work for me Executemultiple request. 

    Heres my snippet code :

      Entity ent = new Entity("new_productitem");
                                productitem["new_instanceid"] = dt.Rows[i][3].ToString();
                                productitem["new_productnumber"] = dt.Rows[i][4].ToString();
    var multiplerequest = new ExecuteMultipleRequest()
    
                               {
    
                                   Settings = new ExecuteMultipleSettings()
    
                                   {
    
                                       ContinueOnError = false,
    
                                       ReturnResponses = true
    
                                   },
    
                                   Requests = new OrganizationRequestCollection()
    
                               };
    
                               CreateRequest createcontractline = new CreateRequest { Target = ent };
    
    multiplerequest.Requests.Add(createcontractline);
    
                               ExecuteMultipleResponse multirespon = (ExecuteMultipleResponse)service.Execute(multiplerequest);

    The problem is i dont know how to set the second entity value using this method.

    regards

  • Suggested answer
    Aman Kothari Profile Picture
    on at

    Hi Saraswati,

    Try Below code for execute multiple create request with ExecuteMultipleRequest.

                     //For Account

                       Entity account = new Entity("account");

                       account["name"] = "sample account name";

                       //For Contact

                       Entity contact = new Entity("contact");

                       contact["firstname"] = "first name";

                       contact["lastname"] = "last name";

                       var multiplerequest = new ExecuteMultipleRequest()

                       {

                           Settings = new ExecuteMultipleSettings()

                           {

                               ContinueOnError = false,

                               ReturnResponses = true

                           },

                           Requests = new OrganizationRequestCollection()

                       };

                       CreateRequest createRequestforaccount = new CreateRequest { Target = account };

                       CreateRequest createRequestforcontact = new CreateRequest { Target = contact };

               //Multiple Requests add here to multiplerequest object

                       multiplerequest.Requests.Add(createRequestforaccount);

                       multiplerequest.Requests.Add(createRequestforcontact);

                       ExecuteMultipleResponse multirespon = (ExecuteMultipleResponse)service.Execute(multiplerequest);

    Hope it'll help you.

    Thanks

  • Community Member Profile Picture
    on at

    I tried it yesterday, but it didn't work.

  • Aman Kothari Profile Picture
    on at

    Can you post whole plugin code here ?

  • Community Member Profile Picture
    on at

    Hii, here my full code:

     protected void btnOne_Click(object sender, EventArgs e)
            {
                Stopwatch st = new Stopwatch();
                st.Start();
    
    
                if (FileUpload1.HasFile)
                {
                    string Filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                    string Folderpath = ConfigurationManager.AppSettings["FolderPath"];
    
                    string Filepath = Server.MapPath(Folderpath + Filename);
                    FileUpload1.SaveAs(Filepath);
    
                    DataTable dt = ImportData(Filepath, Extension);
    
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            try
                            {
                                Entity contractline = new Entity("new_contractline");
                              
                                contractline["new_contractid"]  = dt.Rows[i][1].ToString();
                                contractline["new_lineitem"] = dt.Rows[i][2].ToString();
                 
                                Entity productitem = new Entity("new_productitem");
                                productitem["new_instanceid"] = dt.Rows[i][3].ToString();
                                productitem["new_productnumber"] = dt.Rows[i][4].ToString();
    
                               
                                var multiplerequest = new ExecuteMultipleRequest()
                                {
                                    Settings = new ExecuteMultipleSettings()
                                    {
                                        ContinueOnError = false,
                                        ReturnResponses = true
                                    },
                                    Requests = new OrganizationRequestCollection()
                                };
    
                                CreateRequest createcontractline = new CreateRequest { Target = contractline };
                                CreateRequest createproductitem = new CreateRequest { Target = productitem };
                                multiplerequest.Requests.Add(createcontractline);
                                multiplerequest.Requests.Add(createproductitem); 
    
                                ExecuteMultipleResponse multirespon = (ExecuteMultipleResponse)service.Execute(multiplerequest);
                            }
                            catch (Exception ex)
                            {
                                //Label1.Text = ex.Message;
                            }
    
                          
                        }
                        st.Stop();
                        TimeSpan ts = st.Elapsed;
    
                        Label1.Text = dt.Rows.Count + "Records created, total time consume is " + ts;
                    }
                    else
                    {
                        Label1.Text = "Tidak ada data yang dipilih";
                    }
                }
            }
            private DataTable ImportData(string Filepath, string Extension)
            {
                string connString = "";
                DataTable dt = new DataTable();
    
                switch (Extension)
                {
                    case ".xls":
                        connString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                        break;
                    case ".xlsx":
                        connString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                        break;
                }
    
                connString = string.Format(connString, Filepath, 1);
    
                try
                {
                    OleDbConnection excelConn = new OleDbConnection(connString);
                    OleDbCommand excelCmd = new OleDbCommand();
                    OleDbDataAdapter oda = new OleDbDataAdapter();
                    excelCmd.Connection = excelConn;
    
                    excelConn.Open();
                    DataTable dtexcelschema;
                    dtexcelschema = excelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    string SheetName = dtexcelschema.Rows[0]["TABLE_NAME"].ToString();
                    excelConn.Close();
    
                    excelConn.Open();
                    excelCmd.CommandText = "Select * from [" + SheetName + "]";
                    oda.SelectCommand = excelCmd;
                    oda.Fill(dt);
                    excelConn.Close();
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message;
                }
                return dt;
            }


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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Shidin Haridas Profile Picture

Shidin Haridas 2

#1
SA-08121319-0 Profile Picture

SA-08121319-0 2

#1
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans