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

01

Enlarge Image

Then, you have to add a new webresource for JavaScript.

02

Enlarge Image

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:

03

Enlarge Image

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.

  1. until Point Of Control 1: This code is only there, to set my variables and to write these informations into the console.
  2. 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.
  3. until Point Of Control 3: The creation Date which was empty is now set to the actual Date.
  4. 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.
  5. 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:

04

Enlarge Image

Hope this helps!

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Category

nice to know

Tags

, , , , , , , , ,