Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

OPENERP 7 , CRM MODULE, CRM_LEAD, CHANGE CONVERT TO OPPORTUNITY BUTTON'S TYPE FROM “ACTION” TO “OBJECT”

By
Yassine TEIMI
on 11/12/14, 5:48 AM 1,402 views

                                                                           A. Fonctionnal Side :

The Reason for this change, is that I want converting to opportunity button always work with this conversion options : 

Conversion Action : Convert to opportunity

Action : Create Partner.

So for me there is no need to open a wizard so as to select these options, therefore, I decided to change the button's type to "object" wich does always the conversion with the options above.

                                                                                   B. Technical Side :

I changed the type of the button "CONVERT TO OPPORTUNITY" from action to object on the crm_lead_view.xml, and on the crm_lead.py, I added the action_apply_directly method, associeted with my new button of type "object"

Here is The Method :

def action_apply_directly(self, cr, uid, ids, context=None):
        """
        Convert lead to opportunity and open
        the freshly created opportunity view.
        """

        res = self.pool.get('crm.lead2opportunity.partner').action_apply(cr, uid, ids)

        return res

Si I just called the action_apply method on the "crm.lead2opportunity.partner" object, wich I updated like this :

def action_apply(self, cr, uid, ids, context=None):
        """
        Convert lead to opportunity or merge lead and opportunity and open
        the freshly created opportunity view.
        """
        if context is None:
            context = {}

       # w = self.pool.get('crm.lead').browse(cr, uid, ids, context=context)[0]
       # lead_ids = [o.id for o in w]
       # if w.name == 'merge':
       #     lead_id = self.pool.get('crm.lead').merge_opportunity(cr, uid, opp_ids, context=context)
       #     lead_ids = [lead_id]
       #     lead = self.pool.get('crm.lead').read(cr, uid, lead_id, ['type'], context=context)
       # if lead['type'] == "lead":
       #            context.update({'active_ids': lead_ids})
       #         self._convert_opportunity(cr, uid, ids, {'lead_ids': lead_ids}, context=context)

        lead_ids = context.get('active_ids', [])
        self._convert_opportunity_wiz(cr, uid, ids, {'lead_ids': lead_ids}, context=context)

        return self.pool.get('crm.lead').redirect_opportunity_view(cr, uid, lead_ids[0], context=context)

I commented all the treatments related to merge opportunities option, and keep only the convert to opportunity option treatment only.

When I run click on the button, it shows me the following error : 

"/opt/openerp/server/openerp/addons/crm/wizard/crm_lead_to_opportunity.py", line 134, in action_apply return self.pool.get('crm.lead').redirect_opportunity_view(cr, uid, lead_ids[0], context=context) IndexError: list index out of range

I think There is something wrong with : lead_ids = context.get('active_ids', []), so that it doesn't give data to lead_ids list.

Could you please give me a hand. I'm here if you want more explanations.

 

0
Yassine TEIMI
On 11/12/14, 9:51 AM

Thank you John for your answer , HERE AFTER MY COMMENT MORE CLEAR !

 

  1. So to summarise what I've got from your answer :

 

if I decide to implement my method on the crm.lead object it will generate two problems :

 

P1 : the active_id passed on the context is the active_id of the crm.lead object, and not of the object crm.lead2opportunity.partner.

 

P2 : Passing "ids" of crm.lead instead of lead2opportunity object.

 

  1. My notices about P1 AND P2 :

About P1, I noticed that when the conversion options window show up, generated by the old action button “CONVERT TO AN OPPORTUNITY”, the active_id wich represent the current record id, on the URL still the same, can we say that the active_id for crm.lead is the same for crm.lead2opportunity.partner object ?

Now about P2, I forced the redirect_opportunity_view method to work by writing:

 

  Self. redirect_opportunity_view (cr, uid, 79, context=context), 

“79” is the active_id of the current record; I copied it from the URL.

 

But as you said the operation is working on the crm.lead object instead of normal treatment when the method was on crm.lead2opporunity.partner object.

 

CODE :

 Now my crm.lead.py file contains the button’s method action_apply_directly as follow :

def action_apply_directly(self, cr, uid, ids, context):

        """

        Convert lead to opportunity and open

        the freshly created opportunity view.

        """

        lead_ids = context.get('active_ids', [])

        self.pool.get('crm.lead2opportunity.partner')._convert_opportunity_wiz(cr, uid, ids, {'lead_ids': lead_ids}, context=context)

 

        return self.redirect_opportunity_view(cr, uid, lead_ids[0], context=context)

ERROR RISING :

File "/opt/openerp/server/openerp/addons/crm/crm_lead.py", line 752, in action_apply_directly

    return self.redirect_opportunity_view(cr, uid, lead_ids[0], context=context)

IndexError: list index out of range

 

QUESTION :

HOW TO PASS CORRECT PARAMETERS ( USED IN THE LEAD2OPPORTUNITY) THROUGH CRM.LEAD OBJECT WICH CONTAINS THE BUTTON’S METHOD ?

Thanks.

Yassine, crm.lead2opportunity.partner's ID is NOT the same as crm.lead's ID. But after further research on the crm.lead2opportunity.partner, I guess it is meant to be the crm.lead's ID as crm.lead2opportunity.partner is a wizard (orm.TransientModel / osv.osv_memory). As to answer your question: crm.lead2opportunity.partner is a wizard and is meant to be pop up. Assuming that you still want to do this but want to "default" certain fields' value you have 2 options. First option the crm.lead2opportunity.partner instance first with the values you want, then call the action passing the id if the instance that you have created. Sample for this option can be found in copy_quotation method of sale/sale.py. It is not a wizard, but the principle still applies. Second option is to call/return the action with special keywords in context. In actions if they receive context which key is in the 'default_[field_name]' format, it will use the value as the default of the field when the window pops up. Method action_quotation_send in sale/sale.py is a sample for this approach.

Ivan
on 11/14/14, 3:29 AM
0

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 11/12/14, 6:41 AM

Yassine,

First, you did not pass context when res = self.pool.get('crm.lead2opportunity.partner').action_apply(cr, uid, ids) is called.  That's why the context is empty.

Second, be very careful if you pass the context because the context will contain the active_ids of crm.lead instead of crm.lead2opportunity.partner (which maybe what you intend to do anyway.

Third you pass the ids argument from crm.lead when calling self.pool.get('crm.lead2opportunity.partner').action_apply(cr, uid, ids).  While this also possible, you need to be very careful as usually other people will assume that ids and context.get('active_ids') belongs to the model.

Fourth, I see that most of your code in action_apply of crm.lead2opportunity.partner uses the crm.lead model anyway, any reason why you don't just put the customization in crm.lead?

Fifth, when calling self._convert_opportunity_wiz(cr, uid, ids, {'lead_ids': lead_ids}, context=context), you passed the ids of crm.lead, as I've mentioned earlier.  This may cause problem if _convert_opportunity_wiz assumes that the ids is of crm.lead2opportunity.partner.

Thank you John for your answer, A. So to summarise what I've got from your answer : if I decide to implement my method on the crm.lead object it will generate two problems : P1 : the active_id passed on the context is the active_id of the crm.lead object, and not of the object crm.lead2opportunity.partner. P2 : Passing "ids" of crm.lead instead of lead2opportunity object. B. My notices about P1 AND P2 : About P1, I noticed that when the conversion options window show up, generated by the old action button “CONVERT TO AN OPPORTUNITY”, the active_id wich represent the current record id, on the URL still the same, can we say that the active_id for crm.lead is the same for crm.lead2opportunity.partner object ? Now about P2, I forced the redirect_opportunity_view method to work by writing: Self. redirect_opportunity_view (cr, uid, 79, context=context), “79” is the active_id of the current record; I copied it from the URL. But as you said the operation is working on the crm.lead object instead of normal treatment when the method was on crm.lead2opporunity.partner object. CODE : Now my crm.lead.py file contains the button’s method action_apply_directly as follow : def action_apply_directly(self, cr, uid, ids, context): """ Convert lead to opportunity and open the freshly created opportunity view. """ lead_ids = context.get('active_ids', []) self.pool.get('crm.lead2opportunity.partner')._convert_opportunity_wiz(cr, uid, ids, {'lead_ids': lead_ids}, context=context) return self.redirect_opportunity_view(cr, uid, lead_ids[0], context=context) ERROR RISING : File "/opt/openerp/server/openerp/addons/crm/crm_lead.py", line 752, in action_apply_directly return self.redirect_opportunity_view(cr, uid, lead_ids[0], context=context) IndexError: list index out of range QUESTION : HOW TO PASS CORRECT PARAMETERS ( USED IN THE LEAD2OPPORTUNITY) THROUGH CRM.LEAD OBJECT WICH CONTAINS THE BUTTON’S METHOD ? Thanks.

Yassine TEIMI
on 11/12/14, 9:50 AM

You will find above your answer, my comment more clear. Thanks in advance.

Yassine TEIMI
on 11/12/14, 10:20 AM

You will find it below, more clear.

Yassine TEIMI
on 11/12/14, 10:22 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/12/14, 5:48 AM
Seen: 1402 times
Last updated: 3/16/15, 8:10 AM