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)

Capturing the GUID of a new record and saving to a field in MS CRM Online on initial Save.

(0) ShareShare
ReportReport
Posted on by

Hi,

I have created a Workflow in Microsoft CRM Online that basically sends an email to a customer which a custom URL that is derived from the GUID of the actual record that is related.

I've used some to copy the GUID on the initial save and store into a custom field on the entity. Unfortunately on the initial save it does copy the GUID field until I save again (2x saves).

CODE (OnSave Event)

function AlertGUID() {
var GUIDvalue = Xrm.Page.data.entity.getId();
if (GUIDvalue != null) {
var ConfirmURL = Xrm.Page.data.entity.attributes.get("new_confirmationurl");
ConfirmURL.setValue(GUIDvalue);
}
}

Does anyone know how I can get this to happen on the initial save (and avoid clicking twice)?

Any help would be greatly appreciated.

Cheers,
Chris

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at

    This is Javascript code..so it will execute before save..if you want on save then plug in is best option..you need to register plugin on post save event.then you will get record id..and then update your custom attribute with guid..

    you can do same with custom workflow activity in workflow..

    foe both options you need custom development.

  • Suggested answer
    Larry McCoy Profile Picture
    on at

    Chris,

    You won't be able to do this in JavaScript with one save. The OnSave event executes before the database transaction, so the GUID does not yet have a value.  That's why it takes two saves, in the first save during record creation, your variable GUIDvalue will be null, so the rest of the script won't set any values.

    You can accomplish what you are trying to do with a Plugin that runs on Create. Rather than describe the entire process for your, I found the following article which describes the steps that you would need to follow:

    lsharepoint.com/.../using-guid-of-entity-record-in-dynamics-crm-

    workflow-designer-a-generic-solution-based-on-custom-plugin/

    ~Larry

  • Suggested answer
    Bruno Lucas Profile Picture
    5,421 on at

    Hi Chris,

    When you say initial save, does it mean open a record, change some values and save or open a blank form and create the record?

    Xrm.Page.data.entity.getId(); won't have a value on a create.

    www.resultondemand.nl/.../6fc1055e-f240-417f-85e0-810b761a48aa.htm

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Chris,

    See this post:

    community.dynamics.com/.../134587

    You need to put it in the form onload, instead.

    Thanks.

  • Chris Vine Profile Picture
    on at

    Hi Larry. This looks like exactly what I will need to do - however, can you do plugins for Microsoft CRM Online? I vaguely remember someone mentioning you couldn't?

  • Aileen Gusni Profile Picture
    44,524 on at

    Chris,

    Use a plugin oncreate and make sure it is POST event or you can put it in the onload js.

    Both can work for crm online.

    Thanks.

  • Chris Vine Profile Picture
    on at

    Worked Perfectly.

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    The right approach here is an asynchronous plugin on Create event.

    Reasons:

    1) An OnLoad JavaScript still requires an additional save (first save of create + additional save to store the value inside the field)

    2) There are no guarantees that the OnLoad event will be called (for example when the user do a "Save And Close" when he creates the record)

    3) Assuming that the field is read-only it's necessary additional code to handle the read-only status (with setDisabled method)

    Advantages of a Plugin:

    1) The GUID will be stored no matter how the record is created (CRM UI, SDK, Outlook Synchronization)

    2) Fairly simple code:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

    {

       Entity target = (Entity)context.InputParameters["Target"];

    string GUIDValue = target.Id.ToString();

    Entity saveTarget = new Entity(target.LogicalName);

    saveTarget.Id = target.Id;

    saveTarget["mycustomfield"] = GUIDValue;

    service.Update(saveTarget);

    }

    Instead of a plugin is possible to use a Workflow but a Custom Workflow Activity is necessary to grab the GUID.

  • Larry McCoy Profile Picture
    on at

    Chris,

    I believe you can do plugins for CRM online, they just need to be registered in the Sandbox.  Also, the post that mentioned OnLoad (that you marked as the answer) will still require the entity to be saved twice, and as Guido mentions below, the OnLoad even may not even fire if the user clicks Save and Close.  Also, any entity records that are created from a mechanism other than the CRM form (such as a workflow, a plugin, or an external application) will not run any javascript.  A plugin is really the best solution. You could technically use a Workflow with a custom workflow activity (in order to get the GUID during the save), but the custom workflow activity would require you to write .NET code anyways so you may as well just use the plugin.

  • Community Member Profile Picture
    on at

    Can i do this from an Out Of The Box workflow?

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