RE: How can I create a field that is a 6-digit randomly generated unique GUID in CRM?
I can't get my hands on the link right now but, there is a new feature that hasn't made it to the user interface yet allows you to create auto generated value fields. The six-character seems to go A9A9A9 -- alpha/numeral chain of 3. Here's the class I put together. You may want to search on some of the more unusual terms to find sources of greater explanation:
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Tooling.Connector;
using System.Collections;
namespace consoleUtility {
class AutonumberingFieldMaker {
public AutonumberingFieldMaker(Microsoft.Xrm.Sdk.IOrganizationService CRMservice) {
this.service = CRMservice;
}
private Microsoft.Xrm.Sdk.IOrganizationService service;
private StringAttributeMetadata attributeMetadata = null;
private string nbrFormat = null;
public string AutonumberingFormat { set { nbrFormat = value;
buildAttributeMetadata();
} get { return nbrFormat; } }
private string sname = null;
public string SchemaName { set { sname = value; attributeMetadata = buildAttributeMetadata(); } get { return sname; } }
private string dname = null;
public string DisplayName { set { dname = value; attributeMetadata = buildAttributeMetadata(); } get { return dname; } }
private int? len = null;
public int? MaxLength { set { len = value; attributeMetadata = buildAttributeMetadata(); } get { return len; } }
private string desc = null;
public string Description { set { desc = value; attributeMetadata = buildAttributeMetadata(); } get { return desc; } }
private StringAttributeMetadata buildAttributeMetadata() {
if (null != nbrFormat && null != sname && null != dname && null != len) {
StringAttributeMetadata am = new StringAttributeMetadata() {
AutoNumberFormat = this.nbrFormat,
SchemaName = this.sname,
LogicalName = this.sname.ToLower(),
FormatName = StringFormatName.Text,
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
DisplayName = new Microsoft.Xrm.Sdk.Label(this.dname, 1033),
IsAuditEnabled = new Microsoft.Xrm.Sdk.BooleanManagedProperty(false),
IsGlobalFilterEnabled = new Microsoft.Xrm.Sdk.BooleanManagedProperty(false),
MaxLength = len
};
if (null!=desc)
am.Description = new Microsoft.Xrm.Sdk.Label(desc, 1033);
return am;
}
return null;
}
public void CreateIn(string entityLogicalName) {
if (null!=attributeMetadata) {
CreateAttributeRequest req = new CreateAttributeRequest() {
EntityName = entityLogicalName,
Attribute = attributeMetadata
};
service.Execute(req);
}
}
}
}
here's how I ran it twice:
AutonumberingFieldMaker cmAddr = new AutonumberingFieldMaker(CrmService);
cmAddr.AutonumberingFormat = "a{RANDSTRING:6}";
cmAddr.Description = "Autogenerated address number";
cmAddr.DisplayName = "adnbr";
cmAddr.SchemaName = "new_adnbr";
cmAddr.MaxLength = 8;
cmAddr.CreateIn("customeraddress");
cmAddr.AutonumberingFormat = "c{RANDSTRING:6}";
cmAddr.Description = "Autogenerated account number";
cmAddr.DisplayName = "cmAddr";
cmAddr.SchemaName = "new_cmAddr";
cmAddr.MaxLength = 8;
cmAddr.CreateIn("account");