N
N
Nurasyl Aldan2020-10-05 02:35:29
1C
Nurasyl Aldan, 2020-10-05 02:35:29

How to make a transaction and blocking in 1C?

Hi all!

Correctly I do transaction and blocking in 1C?
I want to check whether there is a phone number in the database, here I want to observe the uniqueness of the phone number in the table.

НачатьТранзакцию(РежимУправленияБлокировкойДанных.Управляемый);
      
Запрос = Новый Запрос;
Запрос.Текст = "ВЫБРАТЬ ПЕРВЫЕ 1
|Контакты.Код
|ИЗ
|Справочник.Контакты КАК Контакты
|ГДЕ ТелефонНомер = """ + Объект.ТелефонНомер + """ ДЛЯ ИЗМЕНЕНИЯ";
    
РезультатЗапроса = Запрос.Выполнить();
Выборка = РезультатЗапроса.Выбрать();
Количество = Выборка.Количество();

// Проверка существования телефон номера
Если Количество > 0 Тогда
  Если Объект.Код = "" Тогда
    // При новой записи
    Отказ = Истина;
    
    СообщитьТелефонНомерУжеСуществует();
  Иначе
    // Обновление существующей записи
    Пока Выборка.Следующий() Цикл
      Если Выборка.Код <> Объект.Код Тогда
        Отказ = Истина;
        
        СообщитьТелефонНомерУжеСуществует();
      КонецЕсли;
    КонецЦикла
  КонецЕсли;
КонецЕсли;

ЗафиксироватьТранзакцию();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-10-05
@nurasyl

1. The phone must be passed as a parameter to the request. I hope it's normalized.
2. A link can also be passed as a parameter with the <> condition. for a new one, it will be empty, and all objects in the database have a non-empty reference, so nothing will break. And there will be no extra branching.
3. comparison with Object. The code assumes that this is an object module. If this is a BeforeRecord event, then the transaction is not needed, it already exists. if CheckFill is needed, but it is worth remembering that during programmatic work, CheckFill must be called manually. Well, if in CheckFilling, then between the end of the check and the start of the write transaction, someone can also wedge in.
The lock itself is set up like this:

Блокировка = Новый БлокировкаДанных;
ЭлементБлокировки = Блокировка.Добавить("Справочник.Контакты");
ЭлементБлокировки.УстановитьЗначение("ТелефонНомер", Объект.ТелефонНомер);
Блокировка.Заблокировать();

It should be installed BEFORE requesting

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question