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 :

Dynamics 365 Auto Number

gdas Profile Picture gdas 50,091 Moderator

Creating AutoNumber of entities is common requirement in Dynamics 365. Here I am going to share very basic scenario, let’s say we have an entity called Policy. When you create Policy for customer, you want to generate Policy Number. The policy number should be created with first three character of customer name and with auto number will be increment by 1.

If the contact name is “Goutam Das” then policy number will be created like below –

GOU-10000, GOU-10000, GOU-566445, GOU345454.

To do above auto number, here I am taking help of JavaScript and work flow.

Step1:  Create Auto Number Entity with following fields.

  • Prefix (Single line text) 

You can put your auto number prefix if its predefined and not the dynamic one.

  • Suffix (Single line text) 

You can put your auto number suffix in this field if you plan to add any suffix after auto number.

  • Number (Whole Number) 

autonumber1

Number will be the last auto number which will generate in next creation of record. So every time this value will be change when any record is created. If you can see this value is 100024 which means if you create new record the auto number should be 100024, and next time when you see this value it will be 100025. The workflow will basically increment this value by the increment number defined in the entity when each time a record is created.

  • Increment (Whole Number)

Step2: I have Created following fields in Policy Entity to create auto number, this is the entity where I would like to create auto number functionality.

  • Contacts Lookup to choose the customer. (Lookup)
  • Policy Number (Single Line Text)

This field should be read only in the form as this is system generate and can’t be modify manually.

  • AutoNumberPrefix (Single Line Text)

policy1

You may have other fields.

Before going to step3, I am answering few questions which might be you are thinking something like you can do this using plugin as well –

Why I have used JavaScript: –

I have used JavaScript here as the requirement is to create auto number with the Prefix of customer name. So every time in the “Policy Details” forms when user select customer name I am basically getting the customer name and splitting the customer name with first three character and set the prefix to an another field of “Policy” entity called “AutoNumberPrefix”.

// This function is required to get the prefix from selected contacts
// This function will trigger onchange of contact lookup
function OnChangeName()
{
// Ge the name from selected contact lookup
var contactName = Xrm.Page.getAttribute("atm_contacts").getValue()[0].name;
var prefixVal = contactName.substring(0, 3);
Xrm.Page.getAttribute("atm_autonumberprefix").setValue(prefixVal.trim().toUpperCase());
}

Secondly as I took the “Number” filed type whole number in “AutoNumber” entity and I am incrementing this value using workflow. So when I set the auto number value to the “Policy Number”, it’s basically showing the format like “GOU-1,00,001” due to the formatting of whole number, which is not fulfilled my requirement. So I have used here another JavaScript method to replace the comma in the on load event to make Policy Number like “GOU-100001”.

//This function will require when the auto number set to the policy number
//This function will be call onload of the policy form at the moment autonumber created and replace the Comma (,) and do save
function OnloadRemoveComma(){
var autonumber = Xrm.Page.getAttribute("atm_policynumber").getValue();
if(autonumber != null && autonumber != "" && autonumber != undefined )
{
// This will ensure that the page will save only the first time when comma is there in the autonumber
if(autonumber.indexOf(',') > 0)
{
// Replace Comma
autonumber= autonumber.replace(/,/g, '').trim(); //  Replace comma
Xrm.Page.getAttribute("atm_policynumber").setValue(autonumber);
Xrm.Page.data.entity.save();
}
}
}

Why we need workflow: –

I have created a real time workflow which will be trigger on creation of Policy and do following updates.

  • Update with AutoNumber Lookup.
  • Update the “Policy Number “field with following formula.

Policy Number Prefix (Policy Entity) – [Separator (-)]- Number (AutoNumber)

  • Increment Number field (Auto Number) by Increment Field (Auto Number entity).

So now I am going to each steps in details –

Step 3:  Create a web resource with above JS function and register the method in the policy form.

Step 4: Create a 1: N relationship between your AutoNumber entity and the policy entity where I would like to have the auto number.

relationship2

relationship1

Step 5: Create a record in Auto Number entity and mentioned the aoutnumber configurations which you want. Here I have kept prefix , suffix blank , you may add and use this in the workflow if you are using same prefix or suffix.

autonumberrecpord

Step 6: Create a workflow on the entity which you want to create auto number, here I have created the workflow for Policy entity record create event.

process1

Step 7: In the first step of workflow you must associate your entity with auto number lookup which you created for auto number configuration.

process2

Update Policy with AutoNumber lookup

Step 8: In the second step set the auto number to your custom entity with format which you want. Here I have set Policy Number.

2018-12-23_23-20-02_process3

Step 9: Increment Number field in the auto number entity by increment field value.

2018-12-23_23-20-02_process4

Step 10: Save and Activate the workflow and create a Policy.

Here is the result –

2018-12-23_23-28-02_Process5


This was originally posted here.

Comments

*This post is locked for comments