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 :
Microsoft Dynamics CRM (Archived)

Retrieve values of a custom entity

(0) ShareShare
ReportReport
Posted on by 445

Hi,


I've created a autonumbering plugin for my custom entity and its working fine.

There is a one field in my custom entity which contains the city name of the sales person who's creating the record.

My requirement is to add the city name as suffix to the autonumber. (city name will differ)

For that I need to fetch the value from my custom entity. How to do it?

For autonumber I'm already retrieving values from a different entity, within the same code I need to retrieve my custom entity values.

Thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    You can update your auto number field by reading your city value. You have to create a plugin for update message and register it PostStage.

    Mansoor

  • Verified answer
    Imtiazh Profile Picture
    50 on at

    Hi

    My suggestion would be to keep your autonumber method as is, just have an additional optional field called suffix that if populated is added to the end of the new number. This means that you can that use this method for anything, if you want to add a city, country, county etc to the end it doesn't matter. The method only know if there is a suffix add it otherwise return the number as is.  heres is an example

           internal static string getNextId(IOrganizationService service, Entity target, string idField, int numberOfDigits, string suffix ="")

           {

               string newId = "";

               if (service != null && target != null)

               {

                   try

                   {

                       //QueryByAttribute qe = new QueryByAttribute(target.LogicalName);

                       QueryExpression qe = new QueryExpression(target.LogicalName);

                       // Retrieve new_autonumber field

                       qe.ColumnSet = new ColumnSet();

                       qe.ColumnSet.Columns.Add(idField);

                       qe.Orders.Add(new OrderExpression(idField, OrderType.Descending));

                       qe.PageInfo.Count = 1;

                       qe.PageInfo.PageNumber = 1;

                       // Retrieve first page (one record) of records

                       EntityCollection ec = service.RetrieveMultiple(qe);

                       int nextnumber = 0;  // default initial value

                       if (ec != null & ec.Entities.Count > 0)

                       {

                                 Entity result = (Entity)ec[0];

                           // Calculate next supplier if value if existing value

                           if (result.Attributes.Contains(idField))

                           {

                               if (result[idField].GetType() == typeof(int))

                               {

                                   nextnumber = result.GetAttributeValue<int>(idField);

                               }

                               else

                               {

                                   string suppcode = result.GetAttributeValue<string>(idField);

                                   if (!string.IsNullOrEmpty(suppcode))

                                   {

                                       nextnumber = int.Parse(suppcode);

                                   }

                               }

                           }

                       }

                       //increment number;

                       nextnumber += 1;

                       //Update the target id field with the new number

                       if (target.Attributes.Contains(idField))

                       {

                           target[idField] = nextnumber;

                       }

                       else

                       {

                           target.Attributes.Add(idField, nextnumber);

                       }

                       string padding = "D" + numberOfDigits.ToString();

                       newId = nextnumber.ToString(padding);

                       if (!string.IsNullOrEmpty(suffix))

                       {

                           newId += suffix;

                       }

                   }

                   catch (System.Exception ex)

                   {

                       // do nothing

                   }

               }

               return newId;

           }

    I would also do this as a Prevalidation Create. Plugin.

    regards

    Imtiaz

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans