function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
k practicek practice 

How to move contact related tasks and events to Account?

Hi,
List < Task > listofTaskRelatedtoBusinessAccountContact = [SELECT Id,WhoId,Whatid FROM Task WHERE WhoId = : contactid];
List < Event > listofEventsRelatedtoBusinessAccountContact = [SELECT Id,WhoId,Whatid FROM Event WHERE WhoId = : contactid];

 for (Task iterateContactTask: listofTaskRelatedtoBusinessAccountContact) {
                  iterateContactTask.WhatId = pAccount.id;
            }
            for (Event iterateContactEvent: listofEventsRelatedtoBusinessAccountContact) {
              iterateContactEvent.WhatId = pAccount.id;
            }        
 try {
                update listofTaskRelatedtoBusinessAccountContact;
            } catch (Exception e) {}
            try {
                update listofEventsRelatedtoBusinessAccountContact;
            } catch (Exception e) {}

How to update Contact related Tasks and events To Personal account?

help me........
Chidambar ReddyChidambar Reddy
Hi,

What do you mean by Move? Do you want to remove them from Contact? 
k practicek practice
In business Account i have one contact .I try to update This contact related Tasks and Events to PersonAccount object
Chidambar ReddyChidambar Reddy
okay,

In that case you need to also change the Who Id of Activities with Person Contact Id
 

pAccount = [SELECT Id, PersonContactId from Account where Id = : pAccount.Id ];

List < Task > listofTaskRelatedtoBusinessAccountContact = [SELECT Id,WhoId,Whatid FROM Task WHERE WhoId = : contactid];
List < Event > listofEventsRelatedtoBusinessAccountContact = [SELECT Id,WhoId,Whatid FROM Event WHERE WhoId = : contactid];

 for (Task iterateContactTask: listofTaskRelatedtoBusinessAccountContact) {
                  iterateContactTask.WhatId = pAccount.id;
                  iterateContactTask.WhoId = pAccount.PersonContactId;

            }
            for (Event iterateContactEvent: listofEventsRelatedtoBusinessAccountContact) {
              iterateContactEvent.WhatId = pAccount.id;
              iterateContactEvent.WhoId = pAccount.PersonContactId;

            }        
 try {
                update listofTaskRelatedtoBusinessAccountContact;
            } catch (Exception e) {}
            try {
                update listofEventsRelatedtoBusinessAccountContact;
            } catch (Exception e) {}

 
k practicek practice
But here WhatId  i was getting NULL Value
Chidambar ReddyChidambar Reddy
pAccount = [SELECT Id, PersonContactId from Account where Id = : pAccount.Id ];

List < Task > listofTaskRelatedtoPersonAccount = new List < Task >;
List < Event > listofEventsRelatedtoPersonAccount = new List < Event>;

for (Task iterateContactTask: [SELECT Id,WhoId,Whatid FROM Task WHERE WhoId = : contactid]) {
    iterateContactTask.WhatId = pAccount.id;
    iterateContactTask.WhoId = pAccount.PersonContactId;
    listofTaskRelatedtoPersonAccount.add(iterateContactTask);
}
for (Event iterateContactEvent: [SELECT Id,WhoId,Whatid FROM Event WHERE WhoId = : contactid]) {
    iterateContactEvent.WhatId = pAccount.id;
    iterateContactEvent.WhoId = pAccount.PersonContactId;
    listofEventsRelatedtoPersonAccount.add(iterateContactEvent);
}        
try {
    update listofTaskRelatedtoPersonAccount ;
} 
catch (Exception e) {
    system.debug(e);
}
try {
    update listofEventsRelatedtoPersonAccount;
} 
catch (Exception e) {
    system.debug(e);
}

How about this? Also check the debug log for errors
 
k practicek practice
Hi,
      Thank you for your reply.I tried like below its working .Please expline why whoid NULL Here.

or (Task iterateContactTask: listofTaskRelatedtoBusinessAccountContact) {
                  iterateContactTask.Whoid=NULL;
                  iterateContactTask.WhatId = pAccount.id;
            }
            for (Event iterateContactEvent: listofEventsRelatedtoBusinessAccountContact) {
              iterateContactEvent.Whoid=NULL;
              iterateContactEvent.WhatId = pAccount.id;
            }        
 try {
                update listofTaskRelatedtoBusinessAccountContact;
            } catch (Exception e) {}
            try {
                update listofEventsRelatedtoBusinessAccountContact;
            } catch (Exception e) {}
Chidambar ReddyChidambar Reddy
Hi, I guess the system is automatically assigning Contact Id when you choose the Person Account as What Id

That was why, we were not able to give WhoId, 
I guess my reply should have worked for you, have you tried it?
k practicek practice
This field PersonContactId is not available in person account
Chidambar ReddyChidambar Reddy
Who told you?