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
sravan alaparthisravan alaparthi 

assign a account to task.what

Hi i have to assign a task on the account selected by the user,i am stuck in between any help would be great

 

Page:

 

<apex:page controller="PractiseController">
    <apex:form >
      <apex:pageBlock >
      <apex:pageBlockSection columns="1">
      <apex:selectList size="1" value="{!selectValue}" label="Related to" onclick="">
        <apex:selectOptions value="{!Options}"></apex:selectOptions>
      </apex:selectList>
      <apex:inputTextarea value="{!comments}" label="Comments" />
      <apex:commandButton value="Done" action="{!assignTask}"/>
      </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>

 Sravan Alaparthi

 

 

Controller:

public with sharing class PractiseController{

   public List<SelectOption> options=new List<Selectoption>();
    public Id selectValue { get;set; }
     public string comments { get;set; }
//method to generate account names as picklist   
public List<SelectOption> getOptions(){

for(Account account:[Select ID,Name from Account]){
options.add(new SelectOption(account.id,account.Name));}

return options;
}

//method to assign a task
public pageReference assignTask(){


Task lt=new Task();
lt.what=//assign selected account to task

lt.Description=comments;

pageReference pr=new pageReference('/'+selectvalue);
return pr;
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Andrew WilkinsonAndrew Wilkinson

Place the insert in a try catch statement.

 

try(

insert lt;

{

catch(DMLException e){

System.debug('DMLException:' + e);

}

 

Turn on debug logs in the salesforce web ui. Try again and check the logs. Or run your unit test and check the debug log. Do a search for the string in the debug method and the error message of why will be displayed.

All Answers

Andrew WilkinsonAndrew Wilkinson

lt.WhatId = selectValue;

sravan alaparthisravan alaparthi

Hi Andrew,

 

Thanks for the reply,actually the problem was...

 

public pageReference assignTask(){


Task lt=new Task();
lt.whatID=SelectValue;
lt.Description=comments;
insert lt;

pageReference pr=new pageReference('/'+selectvalue);
return pr;
}

 everything works fine even the page is redirected to account detail page,but the task is not inserted...

Andrew WilkinsonAndrew Wilkinson

Place the insert in a try catch statement.

 

try(

insert lt;

{

catch(DMLException e){

System.debug('DMLException:' + e);

}

 

Turn on debug logs in the salesforce web ui. Try again and check the logs. Or run your unit test and check the debug log. Do a search for the string in the debug method and the error message of why will be displayed.

This was selected as the best answer