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
ParamParam 

Create a Task for a Case

Hi

i created a case and i have case id  and case number. now i am trying to create a task on this existing case number.

so do i have to create task or update task for that existing case number.

i am doing in this way :

        Task task = new Task();
          
           task.setWhatId(case_Id);  // case_id contains the case id for the existing case where i want to create a task    
           task.setDescription("sample description ");
           task.setRecordTypeId(id_record_type);
           task.setOwnerId(id_owner);
          
            // Create an array of SObjects to hold the accounts
            SObject[] sObjects = new SObject[1];
            // Add the accounts to the SObject array
            sObjects[0] = task;
           
            // Invoke the create call
            SaveResult[] saveResults = sfdc.create(sObjects);
            // Handle the results
            for (int i=0;i<saveResults.length;i++) {
            // Determine whether create succeeded or had errors
                if (saveResults[i].isSuccess()) {
                    // No errors, so we will retrieve the id created for this index
                    System.out.println(saveResults[i].getId().getValue());
                    }
                else {
                    System.out.println("Object is not Created");               
                }
            }
                    

however its not working. Could any body please help me on this.

thank u

 

SuperfellSuperfell
The SaveResult includes error information, what does it say ?
ParamParam

Hi

thank u  for reply

               if (saveResults[i].isSuccess()) {
                    // No errors, so we will retrieve the id created for this index
                    System.out.println(saveResults[i].getId().getValue());
                    }
                else {
                    System.out.println("Object is not Created");               
                }

it is not going into if, it is directly coming to else. is this the right way to create a task under a case or there is other way?

thank u

 

 

EnderEnder
I think he means to get the error code from the result.

if (saveResults[i].isSuccess()) {
// No errors, so we will retrieve the id created for this index
System.out.println(saveResults[i].getId().getValue());
} else {
Error err = saveResults[i].getErrors()[0];
System.out.println("Error code: " + err.getStatusCode() + " Error Message: " + err.getMessage());
}

Cheers.
SuperfellSuperfell
There's an error collection on the SaveResult object (see all the sample code) that'll tell you why the create failed.
ParamParam

Hi

thank u

i got it. what's the problem was

thanks again

 

ParamParam

Hi

thank u for ur help

i got it what's the problem was

thanks again