If you are looking for a identity column (auto number) or any logic based on total record count
you can leverage OOB auto number feature
docs.microsoft.com/.../create-auto-number-attributes
Sample code
CreateAttributeRequest widgetSerialNumberAttributeRequest = new CreateAttributeRequest
{
EntityName = "newWidget",
Attribute = new StringAttributeMetadata
{
//Define the format of the attribute
AutoNumberFormat = "WID-{SEQNUM:5}-{RANDSTRING:6}-{DATETIMEUTC:yyyyMMddhhmmss}",
LogicalName = "new_serialnumber",
SchemaName = "new_SerialNumber",
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
MaxLength = 100, // The MaxLength defined for the string attribute must be greater than the length of the AutoNumberFormat value, that is, it should be able to fit in the generated value.
DisplayName = new Label("Serial Number", 1033),
Description = new Label("Serial Number of the widget.", 1033)
}
};
_serviceProxy.Execute(widgetSerialNumberAttributeRequest);
to update existing field
//Modify the retrieved auto-number attribute
AttributeMetadata retrievedAttributeMetadata = attributeResponse.AttributeMetadata;
retrievedAttributeMetadata.AutoNumberFormat = "CAR-{RANDSTRING:5}{SEQNUM:6}"; //Modify the existing attribute by writing the format as per your requirement
// Update the auto-number attribute
UpdateAttributeRequest updateRequest = new UpdateAttributeRequest
{
Attribute = retrievedAttributeMetadata,
EntityName = "newWidget",
};
// Execute the request
_serviceProxy.Execute(updateRequest);
After updating the auto number format set seed value to next number. For initial setup find total count of records then add one to set seed value.
//Define the seed
SetAutoNumberSeedRequest req = new SetAutoNumberSeedRequest();
req.EntityName = "newWidget";
req.AttributeName = "newSerialnumber";
req.Value = 10000;
_serviceProxy.Execute(req);
If you are performing any custom logic based on total count you can perform that based on this auto number field.