Bookmarklet for Creating a Record with Specific GUID in Dynamics 365

Paul Nieuwelaar, 10 November 2017

When we’re creating Dynamics 365 solutions, we often need to create “reference” entities which we use with lookup fields to give us more flexibility than basic option set fields. For example, having a lookup for Country on accounts. When populating the lookup values for these entities, the system assigns a random GUID to each record.

The problem comes when we’re working with Development environments, Production environments, and UAT environments for the same solution, the reference data created in each environment is going to have different GUID’s, which means even if we create the exact same values (e.g. with the same “Name”), as soon as we need to reference them by GUID, the values are going to be different.

This means we’re going to face problems with any views, business rules, workflows, or anything else that needs to do filtering by a specific record. This is because if we create a view in the development environment which filters by one of those lookup values, it’s going to look for that same record by GUID when we deploy the solution to UAT or Prod.

I’ve discussed workarounds for this in the past, which includes referencing the lookup values by name, rather than by GUID.

The only way to specify your own GUID when creating a record is to create the record using the SDK. So with this in mind, I’ve created a user-friendly bookmarklet which uses the Web API to create a record of our choice, using a GUID of our choice. This means we can easily create reference data in our Dev, UAT, and Prod instances using the same GUID’s, so we don’t have to worry about facing issues when we deploy our solution.

The Solution

Simply copy the following code into a browser bookmarklet, then launch it from any Dynamics 365 screen. You’ll be prompted for the entity schema name, and then the GUID you want to use. Once confirmed, the record will be created using the specified GUID, and the form will then be opened for you to complete the rest of the fields. At this point, the record has already been created, and the GUID has already been set. And that’s it! No more worrying about lookup-related errors after deploying.

image

javascript:var form=$("iframe").filter(function(){return"visible"==$(this).css("visibility")})[0].contentWindow;try{var name=form.EntityLogicalName||form.Xrm.Page.data.entity.getEntityName()}catch(e){}var entityName=prompt("Type the schema name of the entity to create:",name||"account");if(entityName){var id=prompt("Type the ID to give the new record:","00000000-0000-0000-0000-000000000000");if(id){var req=new form.XMLHttpRequest;req.open("POST",encodeURI(form.Xrm.Page.context.getClientUrl()+"/api/data/v"+(form.APPLICATION_VERSION||"8.2")+"/"+entityName+"s"),!0),req.setRequestHeader("Accept","application/json"),req.setRequestHeader("Content-Type","application/json; charset=utf-8"),req.setRequestHeader("OData-MaxVersion","4.0"),req.setRequestHeader("OData-Version","4.0"),req.onreadystatechange=function(){if(4==this.readyState)if(req.onreadystatechange=null,this.status>=200&&this.status<=299)form.Xrm.Utility.openEntityForm(entityName,id);else{var e=JSON.parse(this.response).error;alert(e.message)}};var data={};data[entityName+"id"]=id,req.send(JSON.stringify(data))}}void(0)

You can also check out a bunch of other cool Dynamics 365 bookmarklets on this GitHub project.