This code is creating an endpoint in Odoo that receives parameters in JSON format and uses those parameters to create a new record in the CRM module. The endpoint is designed to accept POST requests and validates if all required fields are provided before attempting to create the new record. But for some reasons I recive:
Method Not Allowed
The method is not allowed for the requested URL.
from odoo import http
from odoo.http import request
from odoo.exceptions import ValidationError
class ApiController(http.Controller):
@http.route('/api/create_crm_lead', type='json', auth='none', methods=['POST'])
def create_crm_lead(self, **kwargs):
# Extract JSON data from the request
data = request.json
# Validate required fields
required_fields = ['name', 'contact_name'] # Adjust as per your requirements
missing_fields = [field for field in required_fields if field not in data]
if missing_fields:
return {'error': f'Missing required fields: {", ".join(missing_fields)}'}
try:
# Create a new CRM lead
new_lead = request.env['crm.lead'].sudo().create({
'name': data.get('name'),
'contact_name': data.get('contact_name'),
# Add more fields as needed
})
return {'success': True, 'id': new_lead.id}
except ValidationError as e:
return {'error': e.name}
EADPDEV is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.