Im trying to run some JavaScript which:
function changeUsers() { var lookupItem = Xrm.Page.getAttribute("f1_resource").getValue(); if (lookupItem != null) { //Gets the values of Original Lookup var name = lookupItem[0].name; var guid = lookupItem[0].id; var entType = lookupItem[0].entityType; //Get Guid of New Lookup var crmtestuserid = "{61509916-DAE9-E511-8111-3863BB343C90}"; var crmtestuser = crmtestuserid.replace('{', '').replace('}', ''); //Set & Save the Lookup with new Lookup Xrm.Page.getAttribute("f1_resource").setValue([{id: crmtestuser,name: "CRM Test User",entityType: "equipment"}]); Xrm.Page.data.entity.save(); } //Set & Save the lookup with Original Lookup Xrm.Page.getAttribute("f1_resource").setValue([{id: guid,name: name,entityType: entType}]); Xrm.Page.data.entity.save(); }
What happens is that it does this:
CRM Test User 2 -> CRM Test User
Am I doing this correctly?
*This post is locked for comments
Hi Alex,
I was able to solve the issue by creating a real time workflow:
It triggers from a custom Ribbon button which then calls some JS which triggers the workflow. I use:
https://processjs.codeplex.com/
function changeUsers() { Process.callWorkflow("36A122E1-2715-4C5C-89A4-FC1BC74420A6", Xrm.Page.data.entity.getId(), function () {}, function () { alert("Error with workflow"); }); setTimeout(function () { Xrm.Page.data.refresh(); }, 3000); }
I would say that Alex has solved how to resubmit a field. I have verified his answer.
My solution changes the resource 3 times so that my 3rd party workflow recognises that there is a change of resource.
Thanks for the help Alex.
Also, I think that guid might be out of scope.. you defined var inside "if", but you are using it outside of "if".. not sure how javascript treats it.
This might work better (but I have not tried this.. the other one with setSubmitMode I did try:)) )
function changeUsers()
{
var lookupItem = Xrm.Page.getAttribute("f1_resource").getValue();
if (lookupItem != null)
{
//Gets the values of Original Lookup
var name = lookupItem[0].name;
var guid = lookupItem[0].id;
var entType = lookupItem[0].entityType;
//Get Guid of New Lookup
var crmtestuserid = "{61509916-DAE9-E511-8111-3863BB343C90}";
var crmtestuser = crmtestuserid.replace('{', '').replace('}', '');
//Set & Save the Lookup with new Lookup
Xrm.Page.getAttribute("f1_resource").setValue([{id: crmtestuser,name: "CRM Test User",entityType: "equipment"}]);
Xrm.Page.data.entity.then
(function () {
Xrm.Page.getAttribute("f1_resource").setValue([{id: guid,name: name,entityType: entType}]);
Xrm.Page.data.entity.save();
},
function () {
alert("broken");
}
);
}
}
Hi Thomas,
were you doing this with the "timeout" etc, or were you doing this with the script I posted last time? That one did trigger the workflow for me, but, I think, it's not what you've been using, right?
With the "save", maybe it would help moving that second update to a callback instead:
Xrm.Page.data.save().then
(function () {
Xrm.Page.getAttribute("f1_resource").setValue([{id: guid,name: name,entityType: entType}]);
Xrm.Page.data.entity.save();
},
function () {
alert("broken");
}
);
Hi Alex,
Your solution did indeed work and I was hoping that the workflow would trigger. However the workflow doesn't trigger if the resource doesn't change to a new resource.
The workflow inst something I can configure as its part of a 3rd party solution.
Audit History:
System Job:
The Entity Parameters it takes when it executes.
Its a bit of a strange thing i'm trying to do but I'm still trying to come up with a solution.
Hi Tom,
I tried it in a little different way - seems to work:
var emptyFieldValue = Xrm.Page.getAttribute("tcs_emptyfield").getValue();
if(emptyFieldValue == null) Xrm.Page.getAttribute("tcs_emptyfield").setValue("1");
else Xrm.Page.getAttribute("tcs_emptyfield").setValue(null);
Xrm.Page.getAttribute("tcs_resource").setSubmitMode("always");
Xrm.Page.data.entity.save();
Turns out that "save" function does need some data change to happen. Otherwise, it won't really save anything. Once there is a change, it will consider "setSubmitMode" for other fields.. so, the way it worked for me is:
- I've added an extra field to the form (can be hidden) - text field, 1 character
- In that script, I will flip it from null to "1" or back
- Then there is still setSubmitMode for the lookup
- And, then, a call to "save"
The workflow registered on the update of that lookup field does kick in this way
Once you save the first time the form will refresh. so your last two line of code will not be reached
to solve this issue you can create a new field (Two options) and modify its value and save and on the workflow filter on change of this new field and make your processing.
Tried that solution but no luck. I will keep trying it with TimeOuts though.
The console recognises that the Attribute has changed values. It must be something to do with how the form saves and submits its values. Ill keep having a go though.
Then I'd simply try to reduce that code to this:
Xrm.Page.getAttribute("f1_resource").setSubmitMode("always");
Xrm.Page.data.entity.save();
Might just work..
Yeah basically I want to resubmit the "f1_resource" value so CRM thinks that the field has changed.
Hi Thomas,
I'm not sure what you are trying to do, but you could use setSubmitMode("always") on that field to kick off a workflow (without having to switch the users etc)
Workflow triggers don't care about whether the field value has, actually, been changed. they only need that field to be submitted.
If, somehow, that does not fit.. try moving the second update to a timer function:
setTimeout(function(){
Xrm.Page.getAttribute("f1_resource").setValue([{id: guid,name: name,entityType: entType}]);
Xrm.Page.data.entity.save();
}, 500);
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156