Skip to Content
Menu
This question has been flagged
3 Replies
2669 Views

Hi

I'm trying to add a button "Generate new Lead" inside a calendar event that copy's everything about this opportunity and creates a new Lead with the same data and hours of the event.

this is my .py

class AnserCopy(models.TransientModel):
_name = 'anser.copy.wizard'

comments = fields.Text('Comments')
opportunity_id = fields.Many2one('crm.lead', string='Opportunity', domain="[('type', '=', 'opportunity')]")
tag_ids = fields.Many2many('crm.lead.tag', 'crm_lead_tag_rel', 'lead_id', 'tag_id', string='Tags', help="Classify and analyze your lead/opportunity categories like: Training, Service")
event_id = fields.Many2one('event.event', string='Event', required=True, ondelete='cascade')

@api.multi
def action_change_appointment_state(self):
wizards = self.browse(self._ids)

for wizard in wizards:
# get the lead to transform
event_id = wizard.event_id.copy()
opportunity_id = event_id.opportunity
opportunity_id = wizard.event_id.opportunity_id.copy()
opportunity_id.tag_ids = wizard.tag_ids

and my .xml

<record model="ir.ui.view" id="copy_wizard">
<field name="name">Copy Opportunity</field>
<field name="model">anser.copy.wizard</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Copy">
<group>
<field name="tag_ids" />
</group>
<footer>
<button type="object" name="action_change_appointment_state" string="Change State" class="btn-primary"/>
<button string="Cancel" class="btn-default" special="cancel"/>
</footer>
</form>
</field>
</record>

<record id="action_copy" model="ir.actions.act_window">
<field name="name">Copy</field>
<field name="res_model">anser.copy.wizard</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>

<record id="view_calendar_event_form_inherit6" model="ir.ui.view">
<field name="name">calendar.event.form.inherit6</field>
<field name="model">calendar.event</field>
<field name="inherit_id" ref="calendar.view_calendar_event_form"/>
<field name="arch" type="xml">
<sheet position="before">
<header>
<button name="%(action_copy)d" type="action" string="Gerar Nova Oportunidade" />
</header>
</sheet>
</field>
</record>


But my function is not working, can anyone help me?

Avatar
Discard

Do you want to create a record in CRM when you click on this button?

Author

yes!

Author Best Answer

class AnserMedicalCopy(models.TransientModel):
_name = 'anser_medical.copy_opportunity.wizard'

@api.model
def default_get(self, fields):
result = super(AnserMedicalCopy, self).default_get(fields)
event_id = self.env.context.get('active_id')
if event_id:
result['event_id'] = event_id
return result


tag_ids = fields.Many2many('crm.lead.tag', string='Tags', help="Classify and analyze your lead/opportunity categories like: Training, Service")
event_id = fields.Many2one('calendar.event', string='Event', required=True, ondelete='cascade')


@api.multi
def action_copy_opportunity(self):
wizards = self.browse(self._ids)

for wizard in wizards:
if wizard.event_id and wizard.event_id.opportunity_id:
event_id = wizard.event_id.copy()
event_id.opportunity_id = wizard.event_id.opportunity_id.copy()
event_id.opportunity_id.tag_ids = wizard.tag_ids

return {
'name': _('New Event'),
'view_type': 'form',
'view_mode': 'form,tree',
'res_model': 'calendar.event',
'view_id': False,
'res_id': event_id.id,
'type': 'ir.actions.act_window',
'nodestroy': True
}

Avatar
Discard
Related Posts Replies Views Activity
1
Sep 22
9232
1
May 19
3065
2
Nov 22
4363
1
Jun 22
6761
1
Sep 21
2040