Skip to Content
Menu
This question has been flagged
2 Replies
10723 Views

odoo cron job when I fetch data in odoo from external api I set cron job for automatic insert data but when I insert a data in api and set cron job automatic by 1 minutes Its show the data repeatedly after 1 minute execution. that means same data inserting repeatedly after 1 minutes. But I need to insert 1 data in one time only. how can I do that. Please help me.

here is the code for fetching data from external api:

def get_fees_structure_data(self):
url = "http://localhost:8080/api/v1/admission/applicant/fees"

payload = json.dumps({
"status": "Unpaid"
})
headers = {
'Authorization': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImNyZWF0ZWQiOjE2NjUzODA1MzUyNjIsInJvbGVzIjpbIkFETUlOIiwiVVNFUiJdLCJleHAiOjE2NjYyNDQ1MzV9.AqRay2CKj1EpvOs4wtNfapdmWQdfdBQS7wwiu9W9CGEXeDmr3_3Lz5b6xRRseTo3Uz02hHeGndicM6c0N4bXIw',
'Content-Type': 'application/json',
'Cookie': 'session_id=7ff9ada3ad9660f00dbf51381e33ff414d7536f6'
}

response = requests.request("GET", url, headers=headers, data=payload)

data = response.json()
for i in data['payload']:
self.env['fees.structure'].sudo().create(
{'unique_id':i['applicantUniqueId'],'name':i['feesStructureName'], 'payment': i['amount'], 'paid': i['paid'], 'date': i['createdAt'], 'state': i['pstatus']}
)

cron job code:


Update Fees Structurefield>
1field>
minutesfield>
ir.actions.serverfield>
-1field>

model.get_fees_structure_data()field>
codefield>
record>


Avatar
Discard
Best Answer

Hi Nahid jawad Angon,

Try to replace the numbercall by 1 instead of -1.

    
    (field name="numbercall"> 1 (field)

Hope it will help you.
Thanks
Avatar
Discard
Author

brother after replacing 1 it will not fetch data automatically. It will execution when button clicked

-1 will call the scheduler for n number of time. 1 will execute for 1 time only.
Try to give the time to run the scheduler with 5 minutes, instead of 1 minute.
Thanks

Author

Nothing happend brother data not showing automatically

Best Answer

Hello Nahid Jawad Angon,

Please find code in comment. 

I Hope this will help you. 

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Discard

You need to filter out the records like this,

data = response.json()
self._cr.execute("SELECT id FROM fees_structure;")
import itertools
fees_structure_ids = list(itertools.chain(*self._cr.fetchall()))
for i in data['payload']:
if i['paid'] in fees_structure_ids:
data['payload'].remove(i)
for i in data['payload']:
self.env['fees.structure'].sudo().create(
{'unique_id':i['applicantUniqueId'],'name':i['feesStructureName'], 'payment': i['amount'], 'paid': i['paid'], 'date': i['createdAt'], 'state': i['pstatus']}
)

Author

brother its showing an error:
Traceback (most recent call last):
File "D:\Odoo\odoo-15.0\odoo\tools\safe_eval.py", line 330, in safe_eval
return unsafe_eval(c, globals_dict, locals_dict)
File "", line 1, in <module>
File "d:\odoo\odoo-15.0\projects\rtm\rtm_fees_structure\models\fees_structure.py", line 39, in get_fees_structure_data
if i['paid'] in fees_structure_ids[0]:
IndexError: list index out of range

Author

brother??

Actually, this error says that we are not getting any records in 'fees_structure_ids'. So, We need to add one more condition in the if statement like this,

if fees_structure_ids and i['paid'] in fees_structure_ids[0]

And one more thing is, Instead of 'id' we need to fetch 'paid' so we need to modify our query like this,

self._cr.execute("SELECT paid FROM fees_structure;")

Let me know if you face any issues.