For some scenarios we would be having some requirements where multiple records needs to be created which can take number of iterations. To user service.create() method would affect the performance significantly. So we need to executemultiple request.
Below is the code for the executemultiple() method usage. To create contact.
Same can be used for other entities as well.
Here i am selecting record from grid and looping through the selected records.
Entity contact = new Entity();
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
Relationship relationship = new Relationship("account_primary_contact");
String bdate;
Int32 rowcount = dataGrid.SelectedItems.Count;
DataRowView rw;
AccountManager selectedVal;
EntityCollection val1;
EntityCollection chkContact;
String name;
Guid value;
OptionSetValueCollection mul = new OptionSetValueCollection();
ExecuteMultipleRequest request = new ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = false
},
Requests = new OrganizationRequestCollection()
};
int i = 0;
while (i < rowcount)
{
var currentRowIndex = dataGrid.Items.IndexOf(dataGrid.SelectedItems[i]);
rw = (DataRowView)dataGrid.SelectedItems[i];
object item = dataGrid.Items[i];
contact.LogicalName = "contact";
contact["firstname"] = rw["Firstname"].ToString();
contact["middlename"] = rw["Middlename"].ToString();
contact["lastname"] = rw["Lastname"].ToString();
contact["emailaddress1"] = rw["EmailId"].ToString();
contact["telephone1"] = rw["TelePhone"].ToString();
contact["mobilephone"] = rw["Mobile"].ToString();
bdate = rw["BirthDate"].ToString();
contact["birthdate"] = Convert.ToDateTime(bdate);
mul.Add(new OptionSetValue(100000001));
selectedVal = cmbAccount.SelectedItem as AccountManager;
if ((Boolean)rw["AccountFlag"])
{
int k = 0;
val1 = GetAccount(true);
foreach (Entity j in val1.Entities)
{
name = val1.Entities[k].Attributes["name"].ToString();
value = (Guid)val1.Entities[k].Attributes["accountid"];
selectedVal = new AccountManager() { Name = name, Value = value };
k++;
}
}
contact["new_datasource"] = mul;
Guid accountId = selectedVal.Value;
contact["parentcustomerid"] = new EntityReference("account", accountId);
chkContact = ValidateContact(rw["Firstname"].ToString(), rw["Lastname"].ToString(), rw["EmailId"].ToString());
if (chkContact.Entities.Count > 0)
{
var boundItem = dataGrid.CurrentCell.Item;
Entity contactUpdate = new Entity("contact");
contactUpdate.Id = chkContact.Entities[0].Id;
contactUpdate["telephone1"] = rw["TelePhone"].ToString();
contactUpdate["mobilephone"] = rw["Mobile"].ToString();
mul.Add(new OptionSetValue(100000001));
mul.Add(new OptionSetValue(100000002));
contactUpdate["new_datasource"] = mul;
UpdateRequest ur = new UpdateRequest { Target = contact };
request.Requests.Add(ur);
}
else
{
CreateRequest cr = new CreateRequest { Target = contact };
request.Requests.Add(cr);
}
i++;
}
_service.Execute(request);
Below is the code for the executemultiple() method usage. To create contact.
Same can be used for other entities as well.
Here i am selecting record from grid and looping through the selected records.
Entity contact = new Entity();
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
Relationship relationship = new Relationship("account_primary_contact");
String bdate;
Int32 rowcount = dataGrid.SelectedItems.Count;
DataRowView rw;
AccountManager selectedVal;
EntityCollection val1;
EntityCollection chkContact;
String name;
Guid value;
OptionSetValueCollection mul = new OptionSetValueCollection();
ExecuteMultipleRequest request = new ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = false
},
Requests = new OrganizationRequestCollection()
};
int i = 0;
while (i < rowcount)
{
var currentRowIndex = dataGrid.Items.IndexOf(dataGrid.SelectedItems[i]);
rw = (DataRowView)dataGrid.SelectedItems[i];
object item = dataGrid.Items[i];
contact.LogicalName = "contact";
contact["firstname"] = rw["Firstname"].ToString();
contact["middlename"] = rw["Middlename"].ToString();
contact["lastname"] = rw["Lastname"].ToString();
contact["emailaddress1"] = rw["EmailId"].ToString();
contact["telephone1"] = rw["TelePhone"].ToString();
contact["mobilephone"] = rw["Mobile"].ToString();
bdate = rw["BirthDate"].ToString();
contact["birthdate"] = Convert.ToDateTime(bdate);
mul.Add(new OptionSetValue(100000001));
selectedVal = cmbAccount.SelectedItem as AccountManager;
if ((Boolean)rw["AccountFlag"])
{
int k = 0;
val1 = GetAccount(true);
foreach (Entity j in val1.Entities)
{
name = val1.Entities[k].Attributes["name"].ToString();
value = (Guid)val1.Entities[k].Attributes["accountid"];
selectedVal = new AccountManager() { Name = name, Value = value };
k++;
}
}
contact["new_datasource"] = mul;
Guid accountId = selectedVal.Value;
contact["parentcustomerid"] = new EntityReference("account", accountId);
chkContact = ValidateContact(rw["Firstname"].ToString(), rw["Lastname"].ToString(), rw["EmailId"].ToString());
if (chkContact.Entities.Count > 0)
{
var boundItem = dataGrid.CurrentCell.Item;
Entity contactUpdate = new Entity("contact");
contactUpdate.Id = chkContact.Entities[0].Id;
contactUpdate["telephone1"] = rw["TelePhone"].ToString();
contactUpdate["mobilephone"] = rw["Mobile"].ToString();
mul.Add(new OptionSetValue(100000001));
mul.Add(new OptionSetValue(100000002));
contactUpdate["new_datasource"] = mul;
UpdateRequest ur = new UpdateRequest { Target = contact };
request.Requests.Add(ur);
}
else
{
CreateRequest cr = new CreateRequest { Target = contact };
request.Requests.Add(cr);
}
i++;
}
_service.Execute(request);
Comments
Post a Comment