Solved

Editing field in different entity using ONSAVE event in CRM 4.0

Posted on 2010-09-20
8
1,498 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
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 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 29

Expert Comment

by:feridun
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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now