Update Message (CrmService) Dynamics 4.0 - dynamics-crm

I want to update the fields like "cs_hv_additionnalparticularities", "cs_hv_smscope" and so on, but the function srv.Update(de);updates all the form records, I mean it triggers a workflow that I don't want it to happen.
here is my code:
// Retrieve the DynamicEntity that goes with target
RetrieveRequest retrieve = new RetrieveRequest();
retrieve.Target = target;
retrieve.ColumnSet = new AllColumns();
retrieve.ReturnDynamicEntities = true;
// Create a response reference and execute the retrieve request.
RetrieveResponse response1 = (RetrieveResponse)srv.Execute(retrieve);
DynamicEntity de = (DynamicEntity)response1.BusinessEntity;
if (opp.Properties.Contains("cs_hv_additionnalparticularities"))
de["cs_hv_additionnalparticularities"] = opp["cs_hv_additionnalparticularities"];
if (opp.Properties.Contains("cs_hv_smscope"))
de["cs_hv_smscope"] = opp["cs_hv_smscope"];
if (opp.Properties.Contains("cs_hv_ugscope"))
de["cs_hv_ugscope"] = opp["cs_hv_ugscope"];
if (opp.Properties.Contains("cs_hv_acdc"))
de["cs_hv_acdc"] = opp["cs_hv_acdc"];
if (opp.Properties.Contains("cs_hv_smmv"))
de["cs_hv_smmv"] = opp["cs_hv_smmv"];
if (opp.Properties.Contains("cs_hv_smhv"))
de["cs_hv_smhv"] = opp["cs_hv_smhv"];
if (opp.Properties.Contains("cs_hv_ughv"))
de["cs_hv_ughv"] = opp["cs_hv_ughv"];
if (opp.Properties.Contains("cs_hvid"))
de["cs_hvid"] = opp["cs_hvid"];
de["cs_generercable"] = new CrmBoolean(true);
srv.Update(de);
I don't want to use this function srv.Update(de); to update the fields.
Can somebody please give me the update function code that can do this work ??

If you don't want to update additional columns, just retrieve the specific columns (property ColumnSet) instead of the new AllColumns().
You can use only the update the record with the Update method, make sure that your workflow triggers only for the requested fields, and not for additional fields.

Related

Laravel model is updating multiple models instead of single

I am trying to update my model using multiple where conditions.
my code is
public function UpdateEducationData(Request $request)
{
$user = app('user');
// return $request;
$education = eduinfo::where("id", $user->id)->where("exam", $request->type)->first();
// dump sql query for debugging
// $education->rawSql();
// dd($education);
$education->board = $request->board;
// $education->degree = $request->degree;
$education->year = $request->year;
$education->rollno = $request->rollno;
$education->obtainmarks = $request->obtainmarks;
$education->totalmarks = $request->totalmarks;
$education->division = $request->division;
$education->grade = $request->grade;
$education->totalcgpa = $request->totalcgpa;
$education->obtailcgpa = $request->obtailcgpa;
// update education based on exam
$education->save();
return redirect()->back()->with("message", "Education Information Updated Successfully");
return "Update Education Data";
}
My code is updating a eduinfo table based on id and exam. But whenever this function is called it updates all eduinfo records related to that user id.
I tried to update eduinfo table single record but multiple records are being update at once. I dumped eduinfo after retriving the model and yes it's retrieving the single model using first() method but still when save() is called it updates all records of that user id in eduinfo.
I think Your conditions to make a unique record are not.
But if you wanna Just run one time! I have an offer to you.
$flag=true;
if($flag){
$user = \Auth::user();
$education = eduinfo::where('id','=', $user->id)->where('exam','=', $request->type)->first();
$education->board = $request->board;
$education->year = $request->year;
$education->rollno = $request->rollno;
$education->obtainmarks = $request->obtainmarks;
$education->totalmarks = $request->totalmarks;
$education->division = $request->division;
$education->grade = $request->grade;
$education->totalcgpa = $request->totalcgpa;
$education->obtailcgpa = $request->obtailcgpa;
$education->save();
$flag=false;
}
It's depends on my answer.

Linq/Entity Framework syntax for adding a record to a database

I need to add a record to a database using the Entity Framework. Since I'm brand new to using this syntax I am not sure how to properly write the code (Below is my best guess).
First, the agent must have their info inserted into the Agent table. This table produces a self-incrementing primary key known as a SymNumber. I then need to take that SymNumber and use it as a primary key for an insert into the AgentIdentification table.
I have run this code a couple of times, and I do not come up with an error, however since I am using a unit test to test the code I cannot tell for sure if the agent is being added properly. Secondly, I know for a fact that I am not correctly grabbing the SymNumber as generated by the Agent table after the first insert. The SymNumber is an int value in the Linq code set to 0, and this does not change during the AgentIdentification insert.
Any help would be greatly appreciated!
AgentResourcesEntities _db = new AgentResourcesEntities();
try
{
Agent agent = new Agent();
agent.EntityType = "P";
agent.FirstName = agentNewTraining.FirstName;
agent.LastName = agentNewTraining.LastName;
agent.LastChangeOperator = agentNewTraining.Requestor;
agent.LastChangeDate = DateTime.Now;
if (!String.IsNullOrEmpty(agentNewTraining.NameSuffix)) agent.NameSuffix = agentNewTraining.NameSuffix;
_db.Agent.AddObject(agent);
AgentIdentification agentIdentification = new AgentIdentification();
agentIdentification.SymNumber = agent.SymNumber;
agentIdentification.ReferenceType = "S";
agentIdentification.DummyReference = 0;
agentIdentification.LastChangeOperator = agentNewTraining.Requestor;
agentIdentification.LastChangeDate = DateTime.Now;
_db.AgentIdentification.AddObject(agentIdentification);
return true;
}
catch (Exception)
{
return false;
}
First you need to call
_db.SaveChanges();
to get your changed persisted.
But if you want also synchronize (get the new generated value) your agent.SymNumber you will need to call SaveChanges() right after adding it to context.
So the code will be like:
/// ...... ////
_db.Agent.AddObject(agent);
_db.SaveChanges();
AgentIdentification agentIdentification = new AgentIdentification();
agentIdentification.SymNumber = agent.SymNumber; // sym number is now synchronized from DB
///...../////
_db.AgentIdentification.AddObject(agentIdentification);
_db.SaveChanges();
But if SymNumber is foreign key so the AgentIdentification has could have reference to some Agent instance, you can just tie those instances with that reference and would not need to call that additional SaveChanges() in the middle.
Call _db.SaveChanges() after inserting.

How to get list of data items list from CRM Metadata Source in code behind?

How to get list of data items list from CRM Metadata Source in code behind?
I have a CRm Metadata Source like
<crm:CrmMetadataDataSource ID="dsquestionOptionset" runat="server" EntityName="contact"
AttributeName="securityquestion"/>
in the html.
I would like to get list of data items in the code behind from the datasource.
"securityquestion" is an intger value and this is linked to an option set.
I tried like
var listOfItems=dsquestionOptionset.Items;
But not possible
Any help is appreciated
Vinu
You'll want to query the Metadata. An example (from here: http://msdn.microsoft.com/en-us/library/gg509035.aspx) would be something like:
RetrieveAttributeRequest retrieveAttributeRequest =
new RetrieveAttributeRequest
{
EntityLogicalName = Contact.EntityLogicalName,
LogicalName = "new_picklist",
RetrieveAsIfPublished = true
};
// Execute the request.
RetrieveAttributeResponse retrieveAttributeResponse =
(RetrieveAttributeResponse)_serviceProxy.Execute(
retrieveAttributeRequest);
// Access the retrieved attribute.
PicklistAttributeMetadata retrievedPicklistAttributeMetadata =
(PicklistAttributeMetadata)
retrieveAttributeResponse.AttributeMetadata;
// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList =
retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();

Can I update the owner id of a Contact using LINQ?

I'm using CRM 2011, and attempting to update the OwnerId of contact using this code:
var crmContext = new CustomCrmContext(service);
var contact = crmContext.Contact.FirstOrDefault(c=>c.Id == id);
contact.OwnerId.Id= newOwnerId;
crmContext.UpdateObject(contact);
crmContext.SaveChanges();
I don't get any errors, however, the ownerId never updates in the database. I am able to update other attributes, but I'm just wondering if maybe the OwnerId is special and you have to use OrganizationRequest("Assign")? If so, where is this documented so I know what other attributes I cannot update?
The owner of a record cannot be modified with an update. You have to send a AssignRequest instead.
// Create the Request Object and Set the Request Object's Properties
var request = new AssignRequest
{
Assignee = new EntityReference(SystemUser.EntityLogicalName, _newOwnerId),
Target = new EntityReference(Account.EntityLogicalName, _accountId)
};
// Execute the Request
_service.Execute(request);

Retrieving related entities using RetrieveMultipleRequest

I have an entity called Invoice and an entity called InvoiceItem.
There is a one to many relationship called new_invoice_invoiceitem.
There is a LookupAttribute in InvoiceItem called new_parent_invoice_invoiceitem.
I am trying to retrieve the InvoiceItems that are related to the Invoice with a particular ID using the following code:
QueryExpression query = new QueryExpression();
query.EntityName = "new_invoiceitem";
query.ColumnSet = new AllColumns();
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "new_parent_invoice_invoiceitem";
condition.Values = new object [] { new Guid("fe1009cc-e034-49d5-bc59-ab4c3091a6f9") };
condition.Operator = ConditionOperator.Equal;
FilterExpression filter = new FilterExpression();
filter.AddCondition(condition);
query.Criteria = filter;
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;
RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmService.Execute(request);
BusinessEntityCollection bec = response.BusinessEntityCollection;
The code runs without errors but the BusinessEntityCollection is always empty even though there are records in Dynamics.
Any idea what I'm doing wrong?
Thanks,
David
Try setting request.ReturnDynamicEntities = true

Resources