web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to auto populate the current logged in user and modified date...

(0) ShareShare
ReportReport
Posted on by 26

Hello,

I need to auto populate 2 fields.  Field B should be populated with the current user logged into CRM.  Field C should be auto populated with the current Date.

Both Field B and C should only be populated once Field A is either answered Yes or No.  If it is blank the 2 fields shouldn't be populated.

Any help is appreciated.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    If you are using the system fields Owner and Modified Date, they will be automatically populated with the logged in user, and modified date. The modified date is an internal field that cannot be modified.

    If you are using custom fields for the logged in user and modified date, you can use JavaScript to get the owner:

    getUserId and getUserName of the Xrm.Page.content class.

    With the combination of these two fields you can set the lookup field by calling the following function:

    var userId = Xrm.Page.context.getUserId();

    var username = Xrm.Page.content.getUserName();

    setLookupfield("new_systemuserid", userId, username, "systemuser");

    function setLookupField(fieldName, lookupId, lookupName, entityName) {

       var lookupData = new Array();

       var lookupItem = new Object();

       lookupItem.id = lookupId;

       lookupItem.name = lookupName;

       lookupItem.entityType = entityName;

       lookupData[0] = lookupItem;

       Xrm.Page.getAttribute(fieldName).setValue(lookupData);

    }

    For the Modified Date, you can just use the following

    var currentDateTime = new Date();

    Xrm.Page.getAttribute("new_modifieddate").setValue(currentDateTime);

    After you set these two value, save the record by using the Xrm.Page.data.save();

  • USA80 Profile Picture
    26 on at

    What about the part where I only want the fields to be populated if Field A contains data?  Is that doable?

  • Verified answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    You make that field required, and add a onChange event to the field.

    function fieldAOnChange()

    {

      var fieldAValue = Xrm.Page.getAttribute("new_fieldA").getValue();

      if (fieldAValue == true)

      {

         // Do Some Action

      }

      else

      {

         // Do Some Other Action

      }

    }

  • USA80 Profile Picture
    26 on at

    I have an entity called Results.  On this entity I have 2 fields, "Results Approved Last Update Name" and "Result Approved Last Update Date"  Based on what you have above, what do I need to do with the labeling to make this work correctly?  I only want these 2 fields to be populated when the field called, "Result Approved" is changed to Yes or No

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    On you CRM form, add an event for the Result Approved field, and call it resultApprovedOnChange

    - Double click on the field in form designer, click on events and add the event. Make sure you have added the JavaScript library that will contain the code

    - Add the following code to the JavaScript library. Replace the field names as required:

    function resultApprovedOnChange()

    {

       var resultApproved = Xrm.Page.getAttribute("new_resultapproved").getValue();

       if (resultApproved == true)

       {

           var userId = Xrm.Page.context.getUserId();

           var username = Xrm.Page.content.getUserName();

           // If you username field is a lookup field use the following code

           var lookupData = new Array();

           var lookupItem = new Object();

           lookupItem.id = userId;

           lookupItem.name = username;

           lookupItem.entityType = "systemuser";

           lookupData[0] = lookupItem;

           Xrm.Page.getAttribute("new_resultapprovedlastupdatename").setValue(lookupData);

           // If you username field is a text field

           Xrm.Page.getAttribute("new_resultapprovedlastupdatename").setValue(username);

           var currentDateTime = new Date();

           Xrm.Page.getAttribute("new_resultapprovedlastupdatedate").setValue(currentDateTime);

       }

       else if (resultApproved == false)

       {

       }

    }

    Update your web resource JavaScript file, and Publish.

    * Please note that comment above based on the username field. Code is different if field is lookup or field is single line of text (string).

  • ashlega Profile Picture
    34,477 on at

    You might just create a workflow that would trigger on the update of "results approved" and populate those two fields with "modifiedby" and "modifiedon"

  • USA80 Profile Picture
    26 on at

    The user needs to see it in real time, so before the record gets saved.

  • USA80 Profile Picture
    26 on at

    So this is what I did:

    Create a web resource that contained the following info:

    function resultApprovedOnChange()

    {

      var resultApproved = Xrm.Page.getAttribute("nhs_approved").getValue();

      if (resultApproved == true)

      {

          var userId = Xrm.Page.context.getUserId();

          var username = Xrm.Page.content.getUserName();

          // If you username field is a lookup field use the following code

          var lookupData = new Array();

          var lookupItem = new Object();

          lookupItem.id = userId;

          lookupItem.name = username;

          lookupItem.entityType = "systemuser";

          lookupData[0] = lookupItem;

          Xrm.Page.getAttribute("nhs_resultapprovedlastupdatename").setValue(lookupData);

          // If you username field is a text field

         Xrm.Page.getAttribute("nhs_resultapprovedlastupdatename").setValue(username);

         var currentDateTime = new Date();

         Xrm.Page.getAttribute("nhs_resultapprovedlastupdatename").setValue(currentDateTime);

      }

      else if (resultApproved == false)

      {

      }

    }

    Then went into the form, into the field Results Approved, Clicke don Events, added the web resource, then on the bottom for the Event Handler I had an even "onChange" and pointing to function "resultApprovedOnChange()"

    When I published it all the fields were there but when I changed the Result Approved field from yes to no or no to yes, nothing populated for the name.

    What am I missing?

  • USA80 Profile Picture
    26 on at

    After further talks, a workflow may work as they now do not need it real time.  

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    First of all regarding the code above, notice the comments that based on the type of field you are using, you have to select one piece of code or the other. The first part under the comment is for a lookup field, and the other for a text field.

    If you choose the Workflow route:

    You can also create a Real Time Workflow (same as workflow, just when you open the record, click on Covert To Real Time Workflow).

    Check the Change Property on the workflow, and add your nhs_approved field to the there.

    In the actions pane, add an Update event, and make changes to the required field (nhs_resultapprovedlastupdatename and nhs_resultapprovedlastupdatedate

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans