Auto number format for custom entity

Yanıt Auto number format for custom entity

  • 02 Şubat 2012 Perşembe 07:27
     
     

    Hi Everyone,

    I am using the case entity of crm 2011.I want some specific format for the case based on the category selected .

    For eg. Category :A then case number will be AXXXX. And same for category B: the case number will be BXXXX.

    Case number is a autopopulated (field on saving a new case ) by crm case entity.

    Is there any way to do so except plugins.

    And if only way is plugins Then how it can be done .

Tüm Yanıtlar

  • 02 Şubat 2012 Perşembe 07:55
    Yanıtlayıcı
     
     

    Hi,

    Try it jscript, in autonumbering under settings you can set prefix.

     

     


    Thanks & Regards, MS CRM Consultant, V.Surya.
  • 02 Şubat 2012 Perşembe 09:15
     
     

    Thanks for reply

    ya the setting for auto-generated number can be changed from there .

    But how to change the format based on the condition (value selected from the category field)

    In form Category is a field Which is a type of option values,If I select A from that thn the case number should be of format Axxxx And if i select B the case number should be Bxxxx.

    So how we can make this awith unique case number each time.

    ??

    Thanks

  • 02 Şubat 2012 Perşembe 10:33
     
     Yanıt

    Hi,

    The case (incident) entity has a field called ticketnumber, which can be programmatically updated when a record is created, but can't be updated after a record has been saved.

    There are multiple ways you can implement the auto-numbering based on the selected category, which you can achieve without using a plugin, as long as new cases are only created from within CRM and not automatically using workflows, plugins or other external applications.

    The basic approach is when you set the value of the ticketnumber field for a case when saving a new record, you can ignore the out of the box CRM auto-numbering by using Xrm.Page.getAttribute('ticketnumber').setValue('yourCaseNumber');

    This is the same for other entities which implement the CRM auto-numbering.

    One approach that I can recommend is to create a new entity to store the information needed to generate and format a unique number for new records.

    As a basic example, you could create a new entity called Record Number (e.g. new_recordnumber) which stores the last assigned record number in a field (e.g. new_lastrecordnumer) as well as any fields for specifying the format of a full record number.4

    For the case entity, when a new record is created, you query the Record Number entity to retrieve the last assigned record number, increment the value by 1 and update the new_lastrecordnumber field.

    From here, you can store this value in the case ticketnumber field and when the record is saved, add the prefix based on which category has been selected.

    This can all be done in JavaScript and depending on how reusable you want this solution to be, you can add additional fields to the Record Number entity for generating a value in a specific format, such as:

    Starting Number (e.g. 1000)
    Length (e.g. 6) 
    Prefix (e.g. "MY-CASE:")
    Suffix (e.g. "[AUS]")
    Entity Name (e.g. incident) 

    Using this the first record number generated for a case would be "MY-CASE:001000[AUS]" and the second number would be "MY-CASE:001001[AUS]" - this gives you a lot of flexibility for assigning automatic numbers to multiple entity types. Storing the entity name (logical name) on the Record Number means you can apply the functionality to generate unique numbers for any type of record, each with customisable formatting.

    Is this the kind of thing you are looking for? Let me know if you want any more information or some example code.

    Cheers,
    Tully

    • Yanıt Olarak İşaretleyen Developer_2011 06 Şubat 2012 Pazartesi 05:22
    •  
  • 02 Şubat 2012 Perşembe 15:56
     
     Yanıt
    • Yanıt Olarak İşaretleyen Developer_2011 06 Şubat 2012 Pazartesi 05:22
    •  
  • 06 Şubat 2012 Pazartesi 05:22
     
     
    Thanks fo reply
    • Yanıt Olarak İşaretleyen Developer_2011 06 Şubat 2012 Pazartesi 05:22
    • Yanıt İşaretini Geri Alan Developer_2011 06 Şubat 2012 Pazartesi 05:22
    •  
  • 30 Mayıs 2012 Çarşamba 14:35
     
      Kod İçerir

    Hello

    I did this recently. This code uses a custom entity called "new_sequencenumber" to hold current counter value, pad length and pad character for the case ticket number. The code uses the casetypecode on the case form as the prefix and then adds a sequential number.

            protected void ExecutePreCaseCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                // Get context
                IPluginExecutionContext context = localContext.PluginExecutionContext;
    
                // Get service
                IOrganizationService service = localContext.OrganizationService;
    
                // Get service provider
                IServiceProvider serviceProvider = localContext.ServiceProvider;
    
                // Set tracing
                // ITracingService tracingService = localContext.TracingService;
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
    
                    // obtain target entity from input params
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    // check target entity is a case
                    if (context.PrimaryEntityName == "incident")
                    {
                        
                        // get sequence number record for incident if it exists
                        Entity sequenceNumber = GetEntityByName("incident", "new_name", "new_sequencenumber", new ColumnSet("new_sequencenumberid"), service);
    
                        // if it doesn't exist create it and set initial counter to 0
                        if (sequenceNumber == null)
                        {
                            sequenceNumber = new Entity("new_sequencenumber");
                            sequenceNumber.Attributes.Add("new_name", "incident");
                            sequenceNumber.Attributes.Add("new_countervalue", 1);
    
                            // default pad length and pad character to sensible values
                            sequenceNumber.Attributes.Add("new_padcharacter", "0");
                            sequenceNumber.Attributes.Add("new_padlength", 6);
    
                            sequenceNumber.Id = service.Create(sequenceNumber);
                            
                        }
    
                        Guid sequenceNumberId = sequenceNumber.Id;
    
                        // generate new GUID and update sequence records dummy field to lock record
                        if (sequenceNumber.Attributes.Contains("new_dummy"))
                            sequenceNumber.Attributes.Remove("new_dummy");
    
                        sequenceNumber.Attributes.Add("new_dummy", Guid.NewGuid().ToString());
                        service.Update(sequenceNumber);
    
                        // sequence record is now locked so get current counter
                        sequenceNumber = service.Retrieve("new_sequencenumber", sequenceNumberId, new ColumnSet(true));
    
                        // read counter value
                        int countervalue = (int)sequenceNumber.Attributes["new_countervalue"];
                        char padcharacter = sequenceNumber.Attributes["new_padcharacter"].ToString()[0];
                        int padlength = (int)sequenceNumber.Attributes["new_padlength"];
    
                        string paddedcountervalue = (countervalue.ToString()).PadLeft(padlength, padcharacter);
    
                        // increment counter value
                        countervalue++;
    
                        // add ticket number to case
                            
                            // remove existing ticketnumber from input params
                            entity.Attributes.Remove("ticketnumber");
    
                            // get case type
                            int casetype = entity.GetAttributeValue<OptionSetValue>("casetypecode").Value;
    
                            switch (casetype) {
                            
                                // if incident
                                case 1:
                                    entity.Attributes.Add("ticketnumber", ("INC-" + paddedcountervalue));
                                    break;
    
                                // if problem
                                case 2:
                                    entity.Attributes.Add("ticketnumber", ("PR-" + paddedcountervalue));
                                    break;
    
                                // if change request
                                case 3:
                                    entity.Attributes.Add("ticketnumber", ("CR-" + paddedcountervalue));
                                    break;
    
                                default:
                                    break;
                            }
    
                        // update counter value back
                        sequenceNumber.Attributes.Remove("new_countervalue");
                        sequenceNumber.Attributes.Add("new_countervalue", countervalue.ToString());
                        service.Update(sequenceNumber);
                    }
                }
            }


    Iain Wright

  • 08 Eylül 2012 Cumartesi 12:40
     
     
    Disclaimer: I work for North52, and we have both free (community edition) & paid options on our product.

    As part of N52 Formula Manager we have an auto number function which will resolve your issue & you don't need any coding. The best  bit is that there is a free community edition available so you can deploy to production environments for free. 

    Here's a link to a video showing how it works & the formatting of the number is completely flexible.

    http://www.youtube.com/watch?v=72aCs1a78BE

    Those looking for specifics on the algorithm we use can read the blog post below which is very similar to what we do internally.

    http://connect.greenbeacon.com/2012/01/technical-post-implementing-robust-microsoft-dynamics-crm-2011-auto-numbering-using-transactions/#comments

    John

    John Grace, Founder at North52 www.north52.com