This question has been flagged
1 Reply
2149 Views

Hi everyone.

Need help with 'action'.

What I would like to achieve is on click 'BUTTON' change state in each record in table depending on value in specified field.

For example: if value in <field name="pnp"/> = 'rom' change state in <field name='state'/> to arm else change state to wrh.

Can you help me with ?


Avatar
Discard
Best Answer

From what I understood you want a method on a button which changes state based on some value

@api.multi
def change_state(self):
    #serach all records
    records = self.env['your.object.name'].search([('state','=','new_order')])#this will search all records in the given object with state == 'new_order
    #you have record set of all records in state 'new_order' now you can iterate over using for loop
    for record in records:
        #now record is single recordset you can access any field of your object with dot ('.') operator
        if record.pnp == 'rom': # no need to check state more becuase now you have only filtered records with state 'new_order' only
            record.write({'state' : 'royal'}) #'royal' state must be present in options
        else:
            record.write({'state' : 'in'}) #'in' state must be present in options    


Avatar
Discard
Author

Great thanx Yogesh, But in this form it's doing only one action (for one record) and all it's doing is .... a new empty record instead of changing state. Also what if I would like to select records by more than one field ? For example and ? and if i would like to check a value with 'like' or 'ilike' ?

Author

Let Me explain what do I need to achieve: I have a list of records with 'state' = 'new order', second field which is important in this process is 'pp' which can held either 'Royal...' or 'Other' values, based on this two fields i would like to change 'state' accordingly to the value held by 'pp'. In one click whole table where 'state' = 'new order' and 'pp' = 'Royal..' should get a new 'state' = 'royal' and records with 'state' = 'new record' and 'pp' = 'Other...' should get a new 'state' = 'in' ..... something like that.

Author
in loop

for each record where 'state' = 'new_order'

if 'state' == 'new_order' && 'pp' = 'Royal....'
    change_state to 'royal'

if 'state' == 'new_order' && 'pp' = 'Other....'
   change_state to 'in'
Something like that :) If would you help me with ...
Author

And how to apply loop which will change all records accordingly to the if statements ?

please check again, I have updated answer based on your last comments.

Author

I'm pretty sure that everything is placed correctly but it gives me an error:

    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
AttributeError: 'mymodel.mane' object has no attribute 'change_state'

@Rob: Please check if you are defining the method under "mymodel.mane" class only or not.

Author

Hi Pawan, Yes I checked it, everything looks ok, now it doesn't give me any errors but still "doing nothing" :(

Author

Look:

#----------------- Main class -----------------    
class iprodstep_log(models.Model):
    _name = 'iprodstep.log'
    _description = 'iprodstep log'
    _inherit = 'ir.needaction_mixin'

    @api.multi
    def sort_order(self):
        records = self.env['iprodstep.log'].search([('state','in',['draft'])])
        for record in records:
            if record.paid_on_date == '':
                record.write({'state' : 'unpaid'})
        if record.postage_service == 'Royal Mail 1st Class':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail 2nd Class':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'International Tracked Postage':
            record.write({'state' : 'royal','urgent_id': True})
        elif record.postage_service == 'Royal Mail 1st Class Signed For':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail 2nd Class Signed For':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail International Signed':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail International Tracked':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail International Standard (Small Packets)':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Other Courier 3-5 days':
            record.write({'state': 'in'})
        elif record.postage_service == 'Other 24 Hour Courier':
            record.write({'state': 'in','urgent_id': True})
        else:
            record.write({'state' : 'in'})

Author

Ok, I managed to do it, it works now but adding one empty record to the list. Weird :)