How can I use incrementCounter from c# SDK

Hi, I knew that there is the incrementCounter in the Scripts. How can I use incrementCounter from c# SDK? I wanna auto increment when I create a new data.
Thanks

The auto increment is a server side feature, you have to configure it in the UI and then it is generated for you.

image
I have set the incrementCounter in Create situation.
However, when I use C# SDK create a new data, it did not fill in the number.
I tried in UI. It works well.

How have you verified it in the SDK? Can you show me the code?

foreach(var appointment in cmsAppointmentResult.Items)   
var notification = new CmsNotificationData()
                    {
                        //id 
                        type = new CmsString { Iv = "direct" },
                        loginID = member.loginID,
                        msgTitle = inboxMessageTitle,
                        msgBody = inboxMessageBody,
                        bSent = new CmsBoolean { Iv = false }
                    };
                    notificationList.Add(notification);
                }

                var updateJob = new BulkUpdate
                {
                    Jobs = notificationList.Select(x => new BulkUpdateJob
                    {
                        Data = x,
                        Type = BulkUpdateType.Upsert
                    }).ToList(),
                    Publish = true
                };

                /* Bulk update the selected data. */
                var result = await _cmsClientManager.CreateContentsClient<CmsNotification, CmsNotificationData>("notification").BulkUpdateAsync(updateJob);

The id will be null in Squidex. The script did not update the id. All of these event is insert.

Why should the id get updated? The requests is transferred to the Server and then handled there, but your local copy of the data stays on the client. You have to pull the content again or use the normal endpoint where you get the new content back after creation or upserting.

I mean that I am going to insert new items in squidex and I blank the id in order to waiting the script to fill in it.
I tried use createAsync. The script did not help me to fill in the id as well.

Have you checked the new content that you get back from the API. The result of the method? The argument is not touched by the API

"response": [
    {
        "contentId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
        "error": null
    },
    ...
]

BulkUpdateAsync returns like this.

I try to explain it again.

It is a server client application, your client has totally different objects than the server. When you create content using the API in any form you are sending a command to the server (CreateContent!).

The command is

  1. Serialized to JSON
  2. Sent over the cable
  3. Deserialized from JSON to an object.
  4. Validated
  5. Executed and the database gets updated.
  6. Sent back to the client in form of an acknowledgment
  7. Discarded

The content is added to the DB with hopefully the correct values but you still do not have this new value.

There are 2 ways to get this:

1. Use the normal endpoint.

When you use the normal create endpoint you get the content back:

// Don't create this for every request, it is an expensive operation.
var client = _cmsClientManager.CreateContentsClient<CmsNotification, CmsNotificationData>("notification");

var newData = new CmsNotificationData()
{
    type = new CmsString { Iv = "direct" },
}

var newContent = await client.CreateAsync(newData);

// This should be 1 or 2 or ... now.
var id = newContent.Data.Id;

2. Use the bulk endpoint and query for contents.

When you use the normal create endpoint you get the content back:

// Don't create this for every request, it is an expensive operation.
var client = _cmsClientManager.CreateContentsClient<CmsNotification, CmsNotificationData>("notification");

var updateJob;

var result = await client.BulkUpdateAsync(newData);

var succeededIds = result.Where(x => x.Error != null).Select(x => x.Id).ToHashSet();

if (succeededIds.Count > 0)
{
    var newContents = await client.GetAsync(succeededIds );
    var newContent1 = newContents.Items[0];
     
    // This should be 1 or 2 or ... now.
    var id = newContent1.Data.Id;
}

Sorry, do you mean that my client is different from server client?
So I cannot use or trigger the scripts when I use bulkUpdate to create data.

Sorry, I was confused because I have not seen how you verify that the ID is set. By default scripting is disabled in bulk updates to improve performance.

But there is a property in the BulkUpdate object, called “DoNotScript”, which is set to true by default.

Sorry to make you confused.
I want to import a lot of data in the same time in stead of loop insert.
So I use bulk update.

What I am thinking is

  1. I will pass null value to id (this id is not the squidex id. It is an integer number for myself)
  2. The script will run and help to fill in the increment integer to id

I tried to set the DoNotScript to true and false. Both value did not trigger the script.

I will have a look about the DoNotScript thing, could be a bug.

If you want to use custom Ids you can also create them client side. You do not have to use the squidex generated Ids.

Yes I generate id in my program, but there may be duplicate id in some situation. Therefore, I wanna use the script in squidex so that the id will not be duplicated.

There is no guarantee that the Ids are not duplicate when using scripts. It is very unlikely, but there is not guarantee.

I have extended my api tests and it works as expected:

1 Like

Oh, it works now after filled in the id with a number. That means I should not put null in the field which I wanna trigger.

The solution of this topic is

  1. set DoNotScript to false in BulkUpdate
  2. put any integer in the field when you send BulkUpdate

Many thanks!

I think 2. is not needed, I just set it to a value so that my other tests still work.

When I did not set anything, the id won’t change, so I think it is needed.