Yetiforcecrm: [How-To] Custom function to update multiple records

Created on 29 Jun 2020  ยท  7Comments  ยท  Source: YetiForceCompany/YetiForceCRM

Hi,

Use case : let's say I want to update the "description" field with the word "Hello" for Contacts.

1 - Create your script like this and put it on your web server, for example in modules/Contacts/workflows/MyCustomClassName.php :

`

/* +**************************

  • The contents of this file are subject to the vtiger CRM Public License Version 1.0
  • ("License"); You may not use this file except in compliance with the License
  • The Original Code is: vtiger CRM Open Source
  • The Initial Developer of the Original Code is vtiger.
  • Portions created by vtiger are Copyright (C) vtiger.
  • All Rights Reserved.
  • ************************** */

class MyCustomClassName

{

function myCustomMethod(Vtiger_Record_Model $recordModel)

{

    $recordModel->set('description', 'Hello');

    $recordModel->setHandlerExceptions(['disableWorkflow' => true]);

    $recordModel->save();


    }

}
`

2 - Register your custom function #10667 where myCustomMethod is the method name and MyCustomClassName is the function name.

3 - Create/Activate a workflow

That's it, it should work either for updating a single records, mass edit or execution schedule.

โ” question

Most helpful comment

Here's another example : let's say I want to update a custom field which count the number of related records.

class MyCustomClassName

{

function myCustomMethod(Vtiger_Record_Model $recordModel)

{

    $relatedModuleName = 'Replace_With_Related_Module_Name';

    $relationListView = Vtiger_RelationListView_Model::getInstance($recordModel, $relatedModuleName);

    $count = $relationListView->getRelatedEntriesCount();

    $recordModel->set('replace_with_your_custom_field_name', $count); 

$recordModel->setHandlerExceptions(['disableWorkflow' => true]);

$recordModel->save();

}

All 7 comments

Here's another example : let's say I want to update a custom field which count the number of related records.

class MyCustomClassName

{

function myCustomMethod(Vtiger_Record_Model $recordModel)

{

    $relatedModuleName = 'Replace_With_Related_Module_Name';

    $relationListView = Vtiger_RelationListView_Model::getInstance($recordModel, $relatedModuleName);

    $count = $relationListView->getRelatedEntriesCount();

    $recordModel->set('replace_with_your_custom_field_name', $count); 

$recordModel->setHandlerExceptions(['disableWorkflow' => true]);

$recordModel->save();

}

Hi,
Thanks for Your examples. I have question - in second example workflow will calculate all related records. Can I define custom value - fo example if checkbox Status is TRUE - so, it will calculate this related record, if FALSE - go to next record ?

@IgorRSU yes

@vovpff but how? Create a new function like getRelationQuery() in modules/Vtiger/models/RelationListView.php ?

@IgorRSU by applying conditions before updating records

@vovpff when creating workflow in system?

@IgorRSU no. You need programmer for this. You need to write some custom function for workflow mechanism and put there some conditions to update only related records what you need instead all related records.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

liberox picture liberox  ยท  3Comments

canomogollon picture canomogollon  ยท  3Comments

ezbank picture ezbank  ยท  3Comments

rskrzypczak picture rskrzypczak  ยท  3Comments

scsikid picture scsikid  ยท  3Comments