+ Start a Discussion
Pritam ShekhawatPritam Shekhawat 

How to autopopulate whoid and whatid for task

Hello,
         I am using visualforce page for task and log a call.And i set some default value for subject , due date and assigned to. But am not able to autopopulate whoid and whatid for lead and opportunity. Without this it is working fine for every object except opportunity. Whenever i click on new task from opportunity it will give below error.Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Name ID: id value of incorrect type: 0011100000sysNbAAI".Below is my code am something missing here??
public with sharing class LogACallControllerExtension {

    public Task task;
    ApexPages.Standardcontroller controller;

    public LogACallControllerExtension(ApexPages.StandardController controller) {
    User u = [select firstname,Id ,LastName from user where id=:userinfo.getuserid()];    
            this.task = (Task)controller.getRecord();     
           
          
            this.task.status = 'Completed';
            this.task.Subject ='Call'; 
            this.task.OwnerId =u.Id;
            this.task.ActivityDate= date.today();
            this.controller = controller;

         
    }
   
    
    
    
   
}
Click here to return to the previous page.
Best Answer chosen by Pritam Shekhawat
Pritam ShekhawatPritam Shekhawat
@Manoj , I create a custom button for new task and add into opportunity related list and pass parameter
/apex/TaskCustom?who_id=''&what_id={! Opportunity.Id }&retURL={!Opportunity.Id }%2F&sfdc.override=1

Now it working fine. It is right apporach?? I am using custom button for New Task here only for opportunity and for other standard button is working fine.

All Answers

ManojjenaManojjena
Hi Pritam ,

I think you have created a custom button ,That button if you have over rid ethe Vf page and make available in the related list .Assume the button is there in Account related list .Then you need to get the account id in the constructor of the class as below .

Id parentId =ApexPages.currentPage.getParameters.get('id'); will give you the account id .

Task/event is related to Contact/Lead as who id apart from that all related with whatid .
You can check if the parentid startswith 003/00Q then assign the id to WhoId else assign to WhatId.

Let m eknow if it helps .

Thanks
Manoj

 
Pritam ShekhawatPritam Shekhawat
@Manoj , here am using vfpage for task and overwrite standard new Task button with my vfpgae. I am not able to find 
check if the parentid startswith 003/00Q then assign the id to WhoId else assign to WhatId.

If am using this 
task.WhoId= System.currentPageReference().getParameters().get('id');
then it works fine for account and opportunity but it will not autopopulated whoid for lead/contact.How to do this?
ManojjenaManojjena
Hi Pritam,

Do one thing modify the code like below .
 
Id ParentId=System.currentPageReference().getParameters().get('id');​
System.debug('*************'+ParentId);

And check the debug log by clicking the button under Lead and Contact .

As per my knowledge is concern the relationship of whoId is not there with Account and Opportunity .

You can cehck below link .
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_erd_activities.htm

Don't forget to create record under opportunity ,An dcontact and Lead .
And assign task.WhatId=OpportunityId not Task.WhoId .

Let me know if it helps .

Thanks
Manoj
Pritam ShekhawatPritam Shekhawat
@Manoj , I create a custom button for new task and add into opportunity related list and pass parameter
/apex/TaskCustom?who_id=''&what_id={! Opportunity.Id }&retURL={!Opportunity.Id }%2F&sfdc.override=1

Now it working fine. It is right apporach?? I am using custom button for New Task here only for opportunity and for other standard button is working fine.
This was selected as the best answer