Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Editing field in different entity using ONSAVE event in CRM 4.0

Posted on 2010-09-20
8
Medium Priority
?
1,539 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 30

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 30

Accepted Solution

by:
Feridun Kadir earned 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 30

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 30

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

963 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