Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1548
  • Last Modified:

Editing field in different entity using ONSAVE event in CRM 4.0

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
WayneATaylor
Asked:
WayneATaylor
  • 4
  • 3
1 Solution
 
Feridun KadirPrincipal ConsultantCommented:
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
 
WayneATaylorAuthor Commented:
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
 
Feridun KadirPrincipal ConsultantCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
abainesCommented:
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
 
Feridun KadirPrincipal ConsultantCommented:
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
 
abainesCommented:
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
 
Feridun KadirPrincipal ConsultantCommented:
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
 
abainesCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now