Hi, I am working on CRM I want to update the contact entity with values in the appointment entity. Contact entity has two fields Last Appointment Date and Next Appointment Date. two fields in Appointment entity called Start Date and End Date. These two value should be copied to contact field values. I want this to happen using JavaScript
There are a couple of ways to call the CRM entities via their Webservices. Using javsacript can prove very time consuming as it's often very difficult to determine why errors are occuring. I generally tend to create a small ASP .Net app, add the CRM SDK as a webservice, call that explicitly and watch the SOAP requests using HTTP Fiddler. Once I then have the actual SOAP call made I can then copy the call, translate it into a string so that I can manipulate the values required and then fire it to the SDK url via the javsacript request.
Here are some examples of calling the CRM SDK for updates / fetches using only javascript:
And here is an official Microsoft CRM javascript samples pack:
I would strongly recommend that you not use JavaScript, but use Callouts (CRM 3.0) or Plugins (CRM 4.0), purely because the JavaScript will execute on the Save click, but once the JavaScript has executed, the save could fail for other reasons (i.e. a pre-update Callout or Plugin failure).
A post-update Callout or Plugin to do this using the related Id to access the contact would be (in my opinion) a more robust solution. If however, you absolutely want this to be done through JavaScript, then yes, Brian Scott's suggestion of following the examples he's given is the way to go.
Hi Antony Gibbs,
If it's a plug-in too no problem for me.. actually first i tried in plug-in. I did not get through it. So i moved to java script. This is the code which i used. public void Execute(IPluginExecutionContext context) { DynamicEntity entity = null;
if (context.InputParameters.Properties.Contains("Target") &&
context.InputParameters.Properties["Target"] is DynamicEntity)
{
entity = (DynamicEntity)context.InputParameters.Properties["Target"];
if (entity.Name != EntityName.appointment.ToString())
return;
}
else
{
return;
}
try
{
Guid appointmentID = new Guid(context.OutputParameters["id"].ToString());
ICrmService crmservice = context.CreateCrmService(true);
appointment appObj = (appointment)crmservice.Retrieve(EntityName.appointment.ToString(), appointmentID, new ColumnSet(new string[] { "regardingobjectid", "location" }));
if (appObj.regardingobjectid == null || appObj.regardingobjectid.type != "contact")
{
return;
}
Guid contactID = appObj.regardingobjectid.Value;
contact cnt = new contact();
cnt.contactid = new Key();
cnt.contactid.Value = contactID;
cnt.firstname = appObj.location;
crmservice.Update(cnt);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new InvalidPluginExecutionException(
"Invalid plug-in.", ex);
}
}