Set values ON duplicating record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arthurmaciel
    Junior Member
    • Jan 2019
    • 15

    Set values ON duplicating record

    Hi! Is there a way to set values for specific attributes when duplicating a record?

    Formulas and hooks set values to fields only when saving records, so the new values are not visible to the user until saving record. But I need to set values dynamically when opening record for edition with the 'Duplicate' button. It would be equivalent to a 'default value' but applicable only when duplicating (at the moment 'default value' only works when creating new records).

    Thanks!

  • Kyle
    Senior Member
    • May 2020
    • 143

    #2
    I know its been a long time since this question was asked but I had the same issue today and I solved it like this:


    In the controller for the entity I overrode "postActionGetDuplicateAttributes". Basically I copied it from the parent controller and then added the line to change the status before returning the data.


    PHP Code:
    
        public function postActionGetDuplicateAttributes($params, $data, $request)
        {
            if (empty($data->id)) {
                throw new BadRequest();
            }
            if (!$this->getAcl()->check($this->name, 'create')) {
                throw new Forbidden();
            }
            if (!$this->getAcl()->check($this->name, 'read')) {
                throw new Forbidden();
            }
            $data = $this->getRecordService()->getDuplicateAttributes($data->id);
            $data->status ='XXXX';
            return $data;
        } 
    

    Comment

    Working...