Base64 to File in a Custom Section

Hello.

 

I'm trying to save a pdf file that i get from a service web, the file comes in a base64 string so i use a script to convert it to a pdf and save it in Creatio, I have this code that works fine for the Accounts section but when i want to save the file in  a Custom section, the process send me an error sometimes because it doesnt find the Id other because it says that the duplicate key value violates unique constraint.

 

This is the code that works in the Accounts Section.

 

var base64FileString = Get<string>("Base64File");
var accountId = Get<Guid>("AccountId");
 
var attachFileType = new Guid("529bc2f8-0ee0-df11-971b-001d60e938c6");
var fileName = Get<string>("NameFile"); // set proper file type
 
var entity = UserConnection.EntitySchemaManager.GetInstanceByName("AccountFile");
var fileEntity = entity.CreateEntity(UserConnection);
fileEntity.SetDefColumnValues();
fileEntity.SetColumnValue("AccountId", accountId);
fileEntity.SetColumnValue("TypeId", attachFileType);
fileEntity.SetColumnValue("Name",fileName);
fileEntity.SetBytesValue("Data", Convert.FromBase64String(base64FileString));
fileEntity.Save();
return true; 

 

And this is the code that I want to use in the custom section

 

var base64FileString = Get<string>("Base64File");
var resguardoId = Get<Guid>("ResguardoId");
 
var attachFileType = new Guid("529bc2f8-0ee0-df11-971b-001d60e938c6");
var fileName = Get<string>("NameFile"); // set proper file type
 
var entity = UserConnection.EntitySchemaManager.GetInstanceByName("SysFile");
var fileEntity = entity.CreateEntity(UserConnection);
fileEntity.SetDefColumnValues();
fileEntity.SetColumnValue("ResguardoId", resguardoId);
fileEntity.SetColumnValue("TypeId", attachFileType);
fileEntity.SetColumnValue("Name",fileName);
fileEntity.SetBytesValue("Data", Convert.FromBase64String(base64FileString));
fileEntity.Save();
return true; 

 

 

Like 0

Like

4 comments

The SysFile entity doesn't, and won't, have a "ResguardoId" column. It has generic columns so it can work with any entity. Instead of setting ResguardoId, set the following: 

  • RecordId - the Id of the record (the value for ResguardoId)
  • RecordSchemaName - the name of the entity for the RecordId (whatever the entity name is for ResguardoId)

Ryan

Ryan Farley,

 

Thank you 

 

I'm made the change, the process starts without any error but the file is not save it in the record.

 

var base64FileString = Get&lt;string&gt;("Base64File");
var recordId = Get&lt;Guid&gt;("RecordId");
 
var attachFileType = new Guid("529bc2f8-0ee0-df11-971b-001d60e938c6");
var fileName ="New.pdf"; // set proper file type
 
var entity = UserConnection.EntitySchemaManager.GetInstanceByName("SysFile");
var fileEntity =  entity.CreateEntity(UserConnection);
fileEntity.SetDefColumnValues();
fileEntity.SetColumnValue("RecordId", recordId);
fileEntity.SetColumnValue("TypeId", attachFileType);
fileEntity.SetColumnValue("Name",fileName);
fileEntity.SetBytesValue("Data", Convert.FromBase64String(base64FileString));
fileEntity.Save();
return true; 

Laura Jurado,

You also need to populate the RecordSchemaName property - refer back to my previous comment.

Ryan

Show all comments