I have customized the lead generation through incoming email by the below code. I am fetching the content of the body in email and matching to the fields present in lead.The template will be same every time but some times the data will not present. Example; template is Caller Name: abcd Phone: 1234567891 email: [email protected] date :16/12/16 The data in email body will be same everytime but sometimes the email will not present or any other.the email will not generate to lead.I want the condition that above data will present then that fields should fill otherwise left it. Ca anybody help me how to write that condition
My Code:
def message_new(self, cr, uid, msg, custom_values=None, context=None):
_logger.info("________________________MESSAGE_NEW________________________")
_logger.info(msg.get('subject', ''))
#myString = msg.get('subject', '')
#myString.index('Phone:')
#mobileNumber = myString[myString.index('Phone:')+6:myString.index('Phone:')+16]
myString = html2plaintext(msg.get('body', ''))
_logger.info(myString)
myString.index('Caller Name:')
newstring=myString.split("*")
_logger.info("--------------------------------------------------------")
_logger.info(newstring)
indx=newstring.index('Caller Name:')
mobileNumber = newstring[indx+1]
indx=newstring.index('Caller\nPhone:')
mobile=newstring[indx+1]
indx=newstring.index('City:')
city=newstring[indx+1]
indx=newstring.index('Branch Info:')
branch=newstring[indx+1]
indx=newstring.index('Caller\nEmail:')
emailstr=newstring[indx+1]
emailarr=emailstr.split("[")
email=emailarr[0]
indx=newstring.index('Caller\nRequirement:')
requirement=newstring[indx+1]
indx=newstring.index('Caller\nRequirement:')
requirement=newstring[indx+1]
indx=newstring.index('Call Date &\nTime:')
date=newstring[indx+1]
if context is None:
context = {}
data = {}
if isinstance(custom_values, dict):
data = custom_values.copy()
model = context.get('thread_model') or self._name
model_pool = self.pool[model]
fields = model_pool.fields_get(cr, uid, context=context)
if 'name' in fields and not data.get('name'):
data['name'] = requirement
if 'contact_name' in fields and not data.get('contact_name'):
data['contact_name'] = mobileNumber
if 'city' in fields and not data.get('city'):
data['city'] = city
if 'mobile' in fields and not data.get('mobile'):
data['mobile'] = mobile
if 'street' in fields and not data.get('street'):
data['street'] = branch
if 'email_from' in fields and not data.get('email_from'):
data['email_from'] = email
if 'x_date' in fields and not data.get('x_date'):
data['x_date'] = date
res_id = model_pool.create(cr, uid, data, context=context)
return res_id