Answered by:
Example for Update an attribute using a plugin

Question
-
Hello ,
Can I get an Example for Update an attribute using a plugin
Thanks Murali
Wednesday, March 6, 2013 8:39 AM
Answers
-
For option sets use this:
OptionSetValue myOptionSet = new OptionSetValue();
myOptionSet.Value = Value;
workReportItem.Attributes["schema name"] = myOptionSet;
For entity reference use this:
workReportItem.Attributes["schema name"] = new EntityReference("account", MissionAccount.Id);
General syntax of update is as:
var workReportItem = new brd_workreportitem { brd_name = S.brd_name, rest of the code }; crm.UpdateObject(workReportItem); crm.SaveChanges();
If the answer helped you, remember to mark it as answer.
- Edited by Payman BiukaghazadehEditor Wednesday, March 6, 2013 9:05 AM
- Marked as answer by Payman BiukaghazadehEditor Thursday, November 7, 2013 7:02 AM
Wednesday, March 6, 2013 9:05 AMModerator
All replies
-
Try below code.
Guid recordId = new Guid ("Provide the record GUID you want to update"); Entity investor = new Entity("entitySchema you want to update"); investor.Id = recordId; investor.Attributes.Add("attributeSchema you want to update", "value with what you want to update"); service.Update(investor);
Chandan - http://mscrm-chandan.blogspot.in/ I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful !!!
Wednesday, March 6, 2013 8:47 AM -
For option sets use this:
OptionSetValue myOptionSet = new OptionSetValue();
myOptionSet.Value = Value;
workReportItem.Attributes["schema name"] = myOptionSet;
For entity reference use this:
workReportItem.Attributes["schema name"] = new EntityReference("account", MissionAccount.Id);
General syntax of update is as:
var workReportItem = new brd_workreportitem { brd_name = S.brd_name, rest of the code }; crm.UpdateObject(workReportItem); crm.SaveChanges();
If the answer helped you, remember to mark it as answer.
- Edited by Payman BiukaghazadehEditor Wednesday, March 6, 2013 9:05 AM
- Marked as answer by Payman BiukaghazadehEditor Thursday, November 7, 2013 7:02 AM
Wednesday, March 6, 2013 9:05 AMModerator -
hi,
I update the phone number in the account form into the contact entity using plugins.
here the code using plugin developer toolkit,
protected void ExecutePostAccountUpdate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}IPluginExecutionContext context = localContext.PluginExecutionContext;
// TODO: Implement your custom Plug-in business logic.
// TODO: Implement your custom Plug-in business logic.
//Get the IOrganizationService
IOrganizationService service = localContext.OrganizationService;//create the service context
var ServiceContext = new OrganizationServiceContext(service);
ITracingService tracingService = localContext.TracingService;// TODO: Implement your custom Plug-in business logic.
string oldPhone = ""; // to store the old Main Phone no:
string newPhone = ""; //// to store the new Main Phone no:// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
// get the pre entity image
Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
//get the post entity image
Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;// get the preimage and postimage telephone1 value
if (preImageEntity.Attributes.Contains("telephone1"))
{
oldPhone = (string)preImageEntity.Attributes["telephone1"];
}if (postImageEntity.Attributes.Contains("telephone1"))
{
newPhone = (string)postImageEntity.Attributes["telephone1"];
}if (newPhone != oldPhone)
{
try
{
//Create query to get the related contacts
var res = from c in ServiceContext.CreateQuery("contact")
where c["parentcustomerid"].Equals(entity.Id)
select c;foreach (var c in res)
{
Entity e = (Entity)c;
e["telephone1"] = newPhone;//ServiceContext.Attach(e);
ServiceContext.UpdateObject(e);
}ServiceContext.SaveChanges();
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}just try it once.
- Proposed as answer by Sireesha Garikipati Monday, March 11, 2013 11:42 AM
Monday, March 11, 2013 11:28 AM