Question

How to create or update a record using Entity Schema Query on server side?

How to create or update a record using Entity Schema Query on server side?

Like 0

Like

2 comments

Hi Nagaraju!



Here are some samples:

 

//Update via entity:
 
var entity = UserConnection.EntitySchemaManager.GetInstanceByName("MyObjectName");
var assignersEntity = entity.CreateEntity(UserConnection);
if (assignersEntity.FetchFromDB(recordId))//recordId - id of the record you want to udpate
{
    assignersEntity.SetColumnValue("MyColumnNameId", newContactId);//for lookup columns use "Id" at the end of a column
    assignersEntity.Save();
}
 
 
//Insert via entity:
 
var entity = UserConnection.EntitySchemaManager.GetInstanceByName("MyObjectName");
var assignersEntity = entity.CreateEntity(UserConnection);
assignersEntity.SetDefColumnValues();
assignersEntity.SetColumnValue("MyColumnNameLookupDataTypeId", projectAccountId);
assignersEntity.SetColumnValue("MyColumnNameStringDataType", "text");
assignersEntity.Save();
 
 
//Select data using enity schema query:
 
var folderESQ = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "MyObjectName");
var exchangeColumn = folderESQ.AddColumn("ColumnToTake").Name;
 
var contactInFolderFilter = folderESQ.CreateFilterWithParameters(FilterComparisonType.Equal,
    "[ContactInFolder:Folder].Contact", contactId);
folderESQ.Filters.Add(contactInFolderFilter);
 
var groups = new List<ItemId>();
 
var folders = folderESQ.GetEntityCollection(UserConnection);
foreach (var folder in folders)
{
    var folderId = folder.GetTypedColumnValue<string>(exchangeColumn);
    //process data
}
 
 
//Delete record:
EntitySchema adminUnitSchema = SystemUserConnection.EntitySchemaManager.GetInstanceByName("MyObjectName");
Entity adminUnitEntity = adminUnitSchema.CreateEntity(SystemUserConnection);
if (adminUnitEntity.FetchFromDB(userId)) {
	adminUnitEntity.Delete();
}
 
//Another sample:
EntitySchema entitySchema = UserConnection.EntitySchemaManager.GetInstanceByName("Contact");
Entity entity = entitySchema.CreateEntity(UserConnection);
if (!entity.FetchFromDB("ColumnName", extId, false))
{
	entity.SetDefColumnValues();
	entity.SetColumnValue("ColumnName", extId);
}
 
entity.SetColumnValue("AnotherColumn", "text");
entity.Save(false);

For additional info - you use Visual studio intellisense on your local env or you can use this online reference.

Dmytro Smishchenko,

This was helpful. Is there a way we can update records using a where clause instead of record id? Example : Update all records where Age > 18 using ESQ in server side. I tried using filters but looks like EntitySchema class does not have filters.



Any help would be much appreciated. Thanks

Show all comments