
Microsoft Dynamics CRM: How to set a unique ID for an entity
In some companies, the entity opportunity is used very often. So if you capture several opportunities each day, I think it could be difficult to find them again afterwards. The easiest way to solve this problem is to set a unique ID for each opportunity:
First, you have to add a field to your entity-form. In my case, I called the field “ID”.
Then, you have to add a new webresource for JavaScript.
For my belongings, I want to set the ID out of the opportunity in a combined way. First, I want the ID to begin with “VC_”, then I want to have the creation date converted in a number and after another “_”, I want to have another date (which in my case is called ‘Date of Revenue’) to be converted in the same format like the creation date. So in the end, the ID should look something like this:
So let’s see the code you have to add to your Javascript Webresource:
function SetOpportunityId() { //If Opportunity-ID isn't filled already - fill it! if (Xrm.Page.getAttribute("abc_id").getValue() === null) {// && Xrm.Page.getAttribute("name").getValue() === null var OpportunityId = Xrm.Page.getAttribute("abc_id").getValue(); var createdon = Xrm.Page.getAttribute("createdon"); var Topic = Xrm.Page.getAttribute("name").getValue(); console.log("SetOpportunityId: If-condition (Set ID) - PointOfControl 1: Opportunity-ID is: " + OpportunityId + "; Creation Date is " + createdon + "; Theme is: " + Topic); var currentDate = new Date(); console.log("SetOpportunityId: If-condition (Set ID) - PointOfControl 2: CurrentDate is: " + currentDate); Xrm.Page.getAttribute("createdon").setValue(currentDate); console.log("SetOpportunityId: If-condition (Set ID) - PointOfControl 3: createdon is: " + createdon); var day = currentDate.getDate(); if (day < 10) { day = "0" + day; } var month = currentDate.getMonth(); month = month + 1; if (month < 10) { month = "0" + month; } var seconds = currentDate.getTime(); currentDate = currentDate.getFullYear() + month + day + seconds console.log("SetOpportunityId: If-condition (Set ID) - PointOfControl 4: Day is: " + day + ", Month is: " + month + ", Seconds is: " + seconds + ", So the currentDate is now: " + currentDate); var DateOfRevenue = Xrm.Page.getAttribute("vorausdatumdeserlsbeginns").getValue(); var day1 = DateOfRevenue.getDate(); if (day1 < 10) { day1 = "0" + day1; } var month1 = DateOfRevenue.getMonth(); month1 = month1 + 1; if (month1 < 10) { month1 = "0" + month1; } var seconds1 = DateOfRevenue.getTime(); DateOfRevenue = DateOfRevenue.getFullYear() + month1 + day1 + seconds1 var generatedID = "VC_"+ currentDate + "_" + DateOfRevenue Xrm.Page.getAttribute("abc_id").setValue(generatedID); console.log("SetOpportunityId: If-condition (Set ID) - PointOfControl 5: ID is set now: " + Xrm.Page.getAttribute("abc_id").getValue()); } else { var OpportunityId = Xrm.Page.getAttribute("abc_id").getValue(); var createdon = Xrm.Page.getAttribute("createdon"); var Topic = Xrm.Page.getAttribute("name").getValue(); console.log("SetOpportunityId: Else-condition (Set ID) - PointOfControl: Opportunity-ID is: " + OpportunityId + "; Creation Date is " + createdon + "; Theme is: " + Topic); } }
Perhaps there’s need for a bit of explanation.
With the if-else-condition, I chech if the opportunity does already have a unique ID. If the opportunity does not have an ID, the IF-condition is used. If the opportunity does have an ID, the ELSE-condition is used. The Else-condition does nothing else than writing the actual ID into the console.
So let’s check the IF-condition in detail.
- until Point Of Control 1: This code is only there, to set my variables and to write these informations into the console.
- until Point Of Control 2: I ask for the current Date. As I set the IDs only for new opportunities, I use the actual Date. And as you can see in Screenshot above, this date isn’t very useful for our ID like this.
- until Point Of Control 3: The creation Date which was empty is now set to the actual Date.
- until Point Of Control 4: The creation Date is seperated into it’s components. If it would be the first of November today, I don’t want the day to be only the number 1. I want to have a format like 01 to not have different lengths in the opportunity ID later. For the month, this is the same. Afterwards, I take the single components and join them again.
- until Point Of Control 5: I used another date out of my opportunity (Date of Revenue) and made the same conversions as in Point 4. After that, I set the generated ID out of my parts “VC_” + creationDate + DateOfRevenue
After all these steps, my Dynamics needs to finally know when to execute this JavaScript. That’s why we have to make the final changes on the form of the entity Opportunity:
Hope this helps!
Share it:
- Click to share on WhatsApp (Opens in new window)
- Click to share on Facebook (Opens in new window)
- Click to share on Twitter (Opens in new window)
- Click to share on Pinterest (Opens in new window)
- Click to email a link to a friend (Opens in new window)
- More
- Click to share on LinkedIn (Opens in new window)
- Click to share on Tumblr (Opens in new window)
- Click to print (Opens in new window)
- Click to share on Reddit (Opens in new window)
- Click to share on Telegram (Opens in new window)
- Click to share on Skype (Opens in new window)
- Click to share on Pocket (Opens in new window)
Related
Join the conversation! 1 Comment
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
About Miss Ob·so·let
Imperfection is beauty, madness is genious. it is better to be absolutely ridiculous than absolutely boring.Latest Posts By Miss Ob·so·let
- 05.22.23Success is going from failure to failure without loss of enthusiasm.
- 05.19.23Scotland
- 05.19.23cats
- 05.19.23True Friends
- 04.14.23Beautiful Philippines – 4 weeks to remember
Hi and thank you for sharing the idea.
I followed all the steps, but unfortunately, I received the following error while saving an opportunity:
One of the scripts for this record has caused an error. For more details, download the log file.
Cannot read properties of null (reading ‘setValue’)
Would you please let me know how I can fix the issue?
Thanks and Regards
Mina
LikeLike