Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

CRM JavaScript - Resubmit the same Lookup value again

Posted on by 11,323

Im trying to run some JavaScript which:

  • Saves the original User Lookup -> Replaces that Lookup with a new Static User Lookup -> Then Replaces that new Static user Lookup with the Original Lookup
  • CRM Test User 2 -> CRM Test User -> CRM Test User 2
  • In the end the User field should always go back to the original. This is to kick off some workflows. 

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();
}

7701.code.png

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

  • Verified answer
    Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: CRM JavaScript - Resubmit the same Lookup value again

    Hi Alex,

    I was able to solve the issue by creating a real time workflow:

    1. Saving the old resource in a new field & Assign the Static Lookup to the original resource field
    2. Saving the old resource field value to the original resource field

    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.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: CRM JavaScript - Resubmit the same Lookup value again

    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");

                        }    

                  );

    }

    }

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: CRM JavaScript - Resubmit the same Lookup value again

    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");

               }    

           );

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: CRM JavaScript - Resubmit the same Lookup value again

    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:

    4555.Screenshot_5F00_1.png

    System Job:

    4555.Screenshot_5F00_1.png

    The Entity Parameters it takes when it executes.

    4555.Screenshot_5F00_2.png

    Its a bit of a strange thing i'm trying to do but I'm still trying to come up with a solution.

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: CRM JavaScript - Resubmit the same Lookup value again

    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

  • Suggested answer
    Chadi Tannous Profile Picture
    Chadi Tannous 1,037 on at
    RE: CRM JavaScript - Resubmit the same Lookup value again

    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.

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: CRM JavaScript - Changing and saving users

    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.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: CRM JavaScript - Changing and saving users

    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..

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: CRM JavaScript - Changing and saving users

    Yeah basically I want to resubmit  the "f1_resource" value so CRM thinks that the field has changed.

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: CRM JavaScript - Changing and saving users

    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);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans