How to set field when owner is changed
-
Monday, July 30, 2012 8:19 AM
Hi
On an account I have some Javascript that sets a field when the owner is updated. (MS CRM2011)
This field is set correctly when a new account is created.
When the owner of an existing account is changed, (I am using the OnSave event and checking for mode 47) the value of the new field is set correctly, but when the screen refreshes the new value is not saved.
Please let me know what I need to do to get this field to save.
Thanks in advance for your assistance.
Regards Louanne
All Replies
-
Monday, July 30, 2012 8:25 AMCan you please share the code with us.
I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
Mubasher Sharif
Check out my about.me profile!
http://mubashersharif.blogspot.com
Linked-In Profile
Follow me on Twitter!
- Edited by MubasherSharif Monday, July 30, 2012 8:27 AM
- Edited by MubasherSharif Monday, July 30, 2012 9:12 AM
-
Monday, July 30, 2012 8:35 AM
Hi
Sorry I was not clear. I am not trying to assign an owner to the record.
I have created a new attribute CSR.
When the owner is changed I would like to set the new CSR attribute to a value I read off the SystemUser table (default CSR).
The value goes into the new CSR attribute, but is not saved when the owner assignment is written to the database, and when the view is refreshed the new value in the CSR field is replaced with the previous value.
Can you help me with this. It is probably something very basic I'm missing as I don't do much CRM scripting.
-
Monday, July 30, 2012 12:46 PMModerator
Assign operation is different from Update operation, you cannot do both at the same time.
You can register a workflow that will update the attribute when the assign occurs or you can also register a plugin to do so. I'm not sure it can work with JavaScript
Gonzalo | gonzaloruizcrm.blogspot.com
- Marked As Answer by Louanne Perry Wednesday, August 01, 2012 11:59 PM
-
Monday, July 30, 2012 1:39 PM
Hey There.
Have you tried to create an independent function which will be called when the owner field is changed?
acount_ownerid_onChange = function(){ var owner = Xrm.Page.getAttribute('ownerid').getValue(); if(owner == null) return; //value change, update a dummy boolean field here. Xrm.Page.getAttribute('new_ownerchanged').setValue(true); Xrm.Page.getAttribute('new_ownerchanged').setSubmitMode('always'); }; -
Monday, July 30, 2012 9:31 PM
Please see below for javascript.
AccountPredefaults is call onLoad
OwnerChanged is called onSave
/*********************
Set predefaults for new records
***********************/AccountPredefaults = function ()
{
try {
if (Xrm.Page.ui.getFormType() == 1) {
// Default CSR from Sales Rep
changeCSR();
}
}
catch (err) {
alert("Loading Account Predefaults function failed\n" + err.message);
}}
OwnerChanged = function ()
{
try {
if (event.Mode == 47) // owner changed
{
// Default CSR from Sales Rep
changeCSR();
}
}
catch (err) {
alert("Loading Account Change function failed\n" + err.message);
}
}
/*********************
Set CSR based on Sales Rep
***********************/function changeCSR() {
var ownerid = Xrm.Page.getAttribute("ownerid").getValue();
if (ownerid != null) {
retrieveSelectedOwner(ownerid[0].id);
}
}function retrieveSelectedOwner(ownerid) {
var entity = "SystemUser";
var select = "?$select=SystemUserId,q2_DefaultCSRId"
var oDataSelect;// build query string
oDataSelect = "/XRMServices/2011/OrganizationData.svc/" + entity + "Set(guid'" + ownerid + "')" + select + "";$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: oDataSelect,
beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
success: function (data, textStatus, XmlHttpRequest) {
// Use for a single selected entity
ProcessReturnMsg(data.d);
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
}
});
}function ProcessReturnMsg(SystemUserEntity) {
if (SystemUserEntity.q2_DefaultCSRId != null) {
var csrId = SystemUserEntity.q2_DefaultCSRId.Id;
var csrName = SystemUserEntity.q2_DefaultCSRId.Name;// insert the default unit of measure
var defaultcsrid = new Array();
defaultcsrid[0] = new Object();
defaultcsrid[0].id = csrId;
defaultcsrid[0].name = csrName;
defaultcsrid[0].entityType = "SystemUser";Xrm.Page.getAttribute("q2_csrid").setValue(defaultcsrid);
// force save on assignment
Xrm.Page.getAttribute("q2_csrid").setSubmitMode("always");Xrm.Page.data.entity.save();
}
} -
Monday, July 30, 2012 11:53 PM
Louanne, Gonzalo is correct. Changing the Owner field does not perform an Update. It is a separate service call. You will need to set your field either via a workflow or a plugin.- Marked As Answer by Louanne Perry Wednesday, August 01, 2012 11:59 PM
-
Wednesday, August 01, 2012 7:41 AM
Hi,
I think while you assign the record and then save it It didn't actually save the record. You can register script on change of owner lookup.
Regards,

