Solved

Editing field in different entity using ONSAVE event in CRM 4.0

Posted on 2010-09-20
8
1,504 Views
Last Modified: 2012-08-14
Hi All

I'm trying to build the code to put in an ONSAVE event for a form that will update a field in a different entity's record when the form is saved.

There is a 1 to many link between the entities, so I basically need code to find the correct entry in the other entity, check a field value in this other entity and update that field if necessary?

I have code that changes entries in the same entity, but cannot find out how to do this in a different one!

Any ideas?

Thanks
Wayne





0
Comment
Question by:WayneATaylor
  • 4
  • 3
8 Comments
 
LVL 29

Expert Comment

by:Feridun Kadir
ID: 33723041
You cannot use Javascript in an OnSave event or indeed an OnLoad or OnChange event to update a field in a different record other than the one generating the event.

Fields in other records can be updated however but you will need to use a SOAP message in the Javascript to call the appropriate CRM web services. The CRM SDK has a brief mention of this and there are numerous blog articles describing how to do this.

Let me know if you need more information.

0
 
LVL 10

Author Comment

by:WayneATaylor
ID: 33723055
Thanks for that.

Do you have the link to any blogs you mention? I've been searching for something but been unable to find anything that explains what I need.

Thanks
Wayne

0
 
LVL 29

Accepted Solution

by:
Feridun Kadir earned 500 total points
ID: 33723110
Sure:

1. http://danielcai.blogspot.com/2010/07/crm-web-service-toolkit-for-javascript.html 
2. http://mscrm-chandan.blogspot.com/2010/06/crm-update-web-service-call-in-java.html

Also have a look at this site: http://www.stunnware.com/ 

I've also copied the CrmService.Update Method Using Jscript topic from the SDK into the code section below. Do you have the CRM SDK?

 


CrmService.Update Method Using JScript
This sample shows how to use the CrmService.Update method using the same example provided in the following topic: CrmService.Update method. 

To test this sample: 

Paste the following code into any Event Detail Properties dialog box. 
Save the form and then click Create Form on the Preview menu. 
Example

When the event occurs, the code will run. This sample uses the Update method to set the value in the Address1_line1 field for a specified contact record.

[JScript]

// Prepare variables for updating a contact.
var contactId = "56914948-991C-DD11-AD3A-0003FF9EE217";
var newAddressLine1 = "34 Market St.";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>"+ 
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ 
authenticationHeader+
"<soap:Body>"+ 
"<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ 
"<entity xsi:type='contact'>"+ 
"<address1_line1>"+newAddressLine1+"</address1_line1>"+ 
"<contactid>"+contactId+"</contactid>"+ 
"</entity>"+ 
"</Update>"+ 
"</soap:Body>"+ 
"</soap:Envelope>";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Update");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result
var resultXml = xHReq.responseXML;

// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
 var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
 alert(msg);
}
// Display a confirmation message and open the updated contact.
else
{
alert("Contact with id = "+contactId+" successfully updated.");
window.open("/sfa/conts/edit.aspx?id={"+contactId+"}");
}
The following is an example of a successful response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <soap:Body>
  <UpdateResponse xmlns="http://schemas.microsoft.com/crm/2007/WebServices" />
 </soap:Body>
</soap:Envelope>

--------------------------------------------------------------------------------

© 2010 Microsoft Corporation. All rights reserved.

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Expert Comment

by:abaines
ID: 33732720
Hi feridun,

when I paste that code from the CRM SDK into the onChange event of an attribute, I just get the 'CRM has encountered and error' msg with the send or not send buttons. Do you know of a way to debug the code in a way that gives more info? Does there have to be a contact with contactId = "56914948-991C-DD11-AD3A-0003FF9EE217"; already present?

Tks
0
 
LVL 29

Expert Comment

by:Feridun Kadir
ID: 33732757
Oh, the code I pasted is *sample* code from the CRM SDK.

The sample code updates the code for an existing contact with an ID of 569.. etc.  For testing you will need to replace the ID with one that exists in your system.

In your final code you will need to have a way of determining the ID of the record that you want to update. In your original post you said:

"........ so I basically need code to find the correct entry in the other entity, check a field value in this other entity and update that field ............"

Part of the code to find the correct entry in the other entity will get the ID of the other entity. Then you can use this ID with code to retrieve the field value that needs to be checked and if necessary, use code similar to the sample above, to update the field.

Soi I think there will be three calls to the webservices: 1 - to determine the ID of the entity to be checked, 2 - to retrieve the field in the entity to be checked, and possibly 3 - to update the field

Out of curiosity, since this is 1:n relationship will possibly several other records how will you know which one of the many to check and update?

Its not simple, I'm afraid.






0
 

Expert Comment

by:abaines
ID: 33768220
Hi Feridun,

I didn't post the question above. I posted a different question, see (http://www.experts-exchange.com/Database/Software/CRM/Q_26487753.html#firstComment). Someone suggested that this question addressed the same issue.
At the moment I'm just trying to get some sample code working before I tailor it to what I need.
Tks.
0
 
LVL 29

Expert Comment

by:Feridun Kadir
ID: 33769601
It does address a similar issue. Are you having a particular problem at the moment? and should we carry this conversation on in your actual question?
0
 

Expert Comment

by:abaines
ID: 33770110
Hi Feridun

I think it would be better if you replied to my question, that way I can assign the points if the issue is resolved.

The issue at the moment is that when I try the sample code above with a contactID from our contacts list, I just get an 'MS Dynamics CRM has encountered an error', Send - Don't Send. Nothing in system jobs. Not much info to go on.

Tks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Sep 22nd 2014 Microsoft released Update Rollup 1 for Microsoft Dynamics CRM 2013 Service Pack 1 and back in July Update Rollup 3 was released.  So we now have:   Update Rollup 1Update Rollup 2Update Rollup 3Service Pack 1Update Rollup 1 for S…
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
Viewers will learn the different options available in the Backstage view in Excel 2013.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question