Adding partners to calendar in Odoo - calendar

I need to create an event to the user's calendar in Odoo v.9 but it is not creating the attendees.
event={
'start':start_time.strftime('%Y-%m-%d %H:%M:%S'),
'stop':end_time.strftime('%Y-%m-%d %H:%M:%S'),
'duration':hours,
'allday':False,
'partner_ids': [emp.employee_id.id],
'name': myshift.account_id.name,
'user_id': emp.employee_id.user_id.id,
}
event=self.env['calendar.event'].create(event)
The collection partner_ids is a Many2Many relationship with res.partner, which is the way to write the partner id in calendar.event?

You cant create values in a many2many field just giving it the id (this is only for many2one).
If the field is a one2many or many2many:
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
(1, ID, { values }) update the linked record with id = ID (write values on it)
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID) link to existing record with id = ID (adds a relationship)
(5) unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
So, what you should add is instead of a list, [(4, emp.employee_id.id)]

This is the code that works
'partner_ids': [(4,[emp.employee_id.user_id.partner_id.id])],

Related

Can you insert or update a row based on multiple values being identical in SQLite?

I'm not very experienced with SQLite, and I want to perform some operations based on a local database. My data consists of a lot of entries of one ID, and there are some additional IDs to specify the data. Based on multiple IDs, there should be a unique combination for each entry in the table.
I would like to select (if it exists) a row based on a combination of IDs, and either insert or update some columns in that row.
I haven't really tried anything because I can't find where to start.
But to illustrate what I mean, I would think of something like this:
UPDATE OR REPLACE INTO my_table (ID,Part_ID,Location_ID,Torque) VALUES (2,6,4,100) WHERE (ID,Part_ID,Location_ID) = (2,6,4)
First, you need a unique constraint for the combination of the columns ID, Part_ID and Location_ID, which you can define with a unique index:
CREATE UNIQUE INDEX idx_my_table ON my_table (ID, Part_ID, Location_ID);
Then use UPSERT:
INSERT INTO my_table (ID, Part_ID, Location_ID, Torque) VALUES (2, 6, 4, 100)
ON CONFLICT(ID, Part_ID, Location_ID) DO UPDATE
SET Torque = EXCLUDED.Torque;
See the demo.

Salesforce Customer

• Create a Custom Field on the Standard Object called “Accounts” on the setup menu
• Add the following custom filed
Field Name Datatype Constraint
Priority Picklist (1, 2, 3, 4, 5)
• Create a Custom Object called “Customer” on the setup menu
• Add the following custom fields
Field Name Datatype Constraint
Title Picklist (Mr, Mrs, Miss)
First_Name Text(100) Required
Last_Name Text(100) Required
Age Number
Address Text(255)
Gender Picklist(Male, Female)
Ref. Account Lookup(Account)
• Create a new Trigger class based on the newly created Custom Object “Customer” to handle duplicate Customer Records.
• Trigger should handle bulk operations as well
• Business requirement as follows:
o Trigger should handle both Insert and Update operations on “Customer”
o Title, First_Name, Last_Name combination must be unique
o At any given time, there should be only unique records in the system
o If a duplicate record found, latest record should be in the system while the previous should get deleted
o If the latest Customer record has a “Ref. Account” with lesser priority than the duplicate found record; change the “Ref. Account” of the latest Customer record to the higher priority account if found on the duplicate record. Always the latest customer record should have the highest priority account with respective to its duplicate. (Note: Priority 1 is the min, Priority 5 is the max)
Please try to provide Answer for this in Salesforce solution as soon as possible. Please advice how to delete and add new record in triggers.
I have done up to handle duplicate records and delete duplicate records.I stuck with get Ref.Account with lesser priority account. how to access Account object priority field and compare with current record Account Priority field? Please reply as soon as possible.
trigger CustomerTigger on Customer__c(Before insert, Before update) {
List < Customer__c > StaActiList = [Select ID, First_Name__c, Last_Name__c, Title__c, Address__c, Gender__c, RefAccount__c
from Customer__c];
for (Customer__c opp: Trigger.new) {
for (Customer__c sa: StaActiList) {
try {
if (sa.First_Name__c == opp.First_Name__c && sa.Last_Name__c == opp.Last_Name__c && sa.Title__c == opp.Title__c) {
List < Id > lstId = new List < Id > ();
List < Customer__c > existoppList = [Select Id from Customer__c where Id = : sa.Id];
delete existoppList;
}
} catch (Exception Ex) {
}
}
}
}

Postgres - remove element from jsonb array

I have an array of jsonb elements (jsonb[]), with id and text. To remove an element I could use:
UPDATE "Users" SET chats = array_remove(chats, '{"id": 2, "text": "my message"')
But I want to delete the message just by the id, cause getting the message will cost me another query.
Assuming missing information:
Your table has a PK called user_id.
You want to remove all elements with id = 2 across the whole table.
You don't want to touch other rows.
id is unique within each array of chats.
UPDATE "Users" u
SET chats = array_remove(u.chats, d.chat)
FROM (
SELECT user_id, chat
FROM "Users", unnest(chats) chat
WHERE chat->>'id' = '2'
) d
WHERE d.user_id = u.user_id;
The following explanation matches the extent of provided information in the question:

MSSQL - OLD to NEW table without duplicates

I have two old tables and wanna "synch" (or better setting new) two new tables... like that:
tbl_old_event_categories (id, title)
tbl_old_events (id, title, cat_id)
tbl_new_event_categories (id, category)
tbl_new_events (event_id, event, category_id)
The problem is that the new tables might already have values. So the IDs will changing (maybe). Because that I can't use ON DUPLICATE KEY UPDATE. :( I need to check it separate for each record. The tables are not unique (and I can't change that). :/
I created a JOIN to get:
tbl_old_fullevents (event, category) //no IDs (integer) only the NAMEs (string)
But how to create an INSERT INTO [tbl_new_events] with checking for existing [event]- and [category]-value? It's something like:
IF(tbl_old_fullevents.event IS NOT IN(tbl_new_events.events)) {
INSERT INTO new_events VALUE(
NULL, //ID
tbl_old_fullevents.event,
IF(tbl_old_fullevents.category IN(new_event_categories.categories)) {
new_event_categories.id //matched
}
ELSE {
INSERT INTO new_event_categories VALUE (
NULL, //ID
old_fullevents.category
);
new_event_categories.id //last INSERT-ID
}
);
}
Use the MERGE syntax. See http://technet.microsoft.com/en-us/library/bb510625.aspx for examples.
You can't use ON DUPLICATE KEY UPDATE because that's mysql.

How to insert data into 3 custom objects using apex code

I have 3 custom objects, Object1, Object2, Object3.
Object2 is the child of Object1.
Object3 is the child of Object2.
So I want to insert multiple records into Object1, Object2, Object3.???
Well, have you actually tried anything?
Simplest action (without using advanced tricks like upsert, external ids etc) is to do it in correct sequence. On successful insert the Id of the record will be returned to the object and you can use it in the lookups to build the relationship.
Account a = new Account(Name = 'test acc');
insert a;
Contact c = new Contact(LastName = 'Test', AccountId = a.Id);
insert c;
AccountContactRole acr = new AccountContactRole(Role = 'President', AccountId = a.Id, ContactId = c.Id);
insert acr;
Alternative would be to do it in whatever order you want and later update the child records with proper references...

Resources