How to change the type of field programmatically in TDM 3?

I have a bunch of fields that are varchar2(1) and I want to change them to char(1).

I have the following script but I don’t think it is right. Looking at the object model (PER) it is not clear to me how I change the type.

function main()
{
var app = System.GetInterface(‘Application’);
var Model = app.Models.GetObjectByName(“Model”);

for (e=0; e<Model.Entities.Count; e++)
{
entity = Model.Entities.GetObject(e);
if (entity.Name.indexOf(“SH_”) > -1)
{
Log.Information(entity.Name);

  for (a=0; a<entity.Attributes.Count; a++)
  {
    attr = entity.Attributes.GetObject(a);       
    if (attr.Datatype.Name.indexOf("Varchar2") > -1 && attr.DataTypeParam1 == 1) 
    {
       attr.DataType.Name = "Char(%p1 %p2)";
       Log.Information(attr.Name + "=" + attr.DataType.Name); 
    }
  }  
}    

}

}
Thanks

Could you please provide a sample that assigns an existing domain to an attribute as well? For instance, I created an YN domain of type char(1) for Y/N flags. Can your example show how to convert fields that are char(1) to the YN domain?

Thanks

Data Type for an Attribute is an object. You don’t change its name in order to change the Attribute’s Data Type. Instead you have to change the Data Type object.
So you have to find the right Data Type object. Example for assigning Data Types are in TDM2 import methods in the scripts PERModelMS05 (How to get a Data Type object) and PERAttributeOR (How to change the Data Type Parameters + IDs).

How to get a Data Type object. Assuming you find appropriate ID in the Database XML package or somewhere else…
Model.ModelDef.DataTypes.GetObjectById("{7A9501DD-AE69-48E6-BAC8-7D177D154276}")

How to assign the Data Type to an Attribute:
Attribute.SetLinkedObject(“DataType”, DataTypeObj);

How to modify the Data Type length:
Attribute.DataTypeParam1 = “1”;

You assign a Domain to an Attribute just the same way as you assign a Data Type.

Hi,

data type ID numbers are available in the Reference. In the latest beta version, enable the Expert mode in Options (item General, Expert Mode), then click Help | Reference.

In the Reference there is a folder Data Types, with topic Data Types. There you can find all databases with all supported data types and their IDs.

BTW: In the Help file you can find new topic in section Scripting and Customization. Topic name: Sample scripts and scripting tips. (you can also open the Help file and on tab Index you can type “sample”).

Regards,

Vaclav

Thanks. I found it.