[SalesForce] Creating a new Task in Apex

I am trying to create, and assign a task related to an account whose page the user is on.

I am not getting any errors, and I can't make sense of the Debug Log.

The snippet of code I am trying to use is

            Task packTask = new Task();
            packTask.WhatId = thisAccount.Id;
            packTask.ActivityDate = packs.Check_Back__c;
            packTask.Type = 'Follow Up';
            packTask.OwnerId = UserInfo.getUserId();
            packTask.Status = 'Not Started';
            packTask.Priority = 'Normal';
            packTask.Description = 'Please call the person in charge of the training curriculum with '+ thisAccount.Name +' and take some time to speak with them regarding '+packs.Name;
            packTask.Subject = 'Check back regarding '+ packs.Name;

            insert packTask;

In the Send Task function it checks the "Check back date" field of a custom object to see if it has been changed.

I put in a "System.Debug" statement for it but it's not in the Debug log.

Full Apex:

public class packageinformationcontroller {

    public Account thisAccount {get;set;}

    public List<Package_Information__c> accountPackages {get;set;}

    public List<Package_Information__c> accountPackagesClone {get;set;}

    public List<Package__c> totalPackages {get;set;}

    public List<Id> currentPackages {get;set;}

    public packageinformationcontroller(ApexPages.StandardController controller) {
        this.thisAccount = (Account)controller.getRecord();
        totalPackages = [SELECT Id, Name FROM Package__c];
    }

    public void checkCurrentPackages(){
        currentPackages = new List<Id>();
        accountPackages = [SELECT Id, Name, Check_Back__c, Applicable__c, Account__c, Date_Offered__c, Notes__c, Package__c, Status__c FROM Package_Information__c WHERE Account__c = :thisAccount.Id];
        accountPackagesClone = accountPackages.clone();
        for (Package_Information__c pack : accountPackages){
            currentPackages.add(pack.Package__c);
        }
    }

    public void addMissingPackages(){

        checkCurrentPackages();

        List<Package__c> packsToAdd = new List<Package__c>();

        if (currentPackages != null){
            packsToAdd = [SELECT Id, Name FROM Package__c WHERE Id NOT IN :currentPackages];
        }
        else{
            packsToAdd = [SELECT Id, Name FROM Package__c];
        }

        for (Package__c pack : packsToAdd){
            Package_Information__c addPack = newPackageInformation(pack);
            insert addPack;
        }

        checkCurrentPackages();
    }

    public ApexPages.PageReference saveChanges(){
        for (Package_Information__c packs : accountPackages){
              ApexPages.StandardController controller = new ApexPages.StandardController(packs);
              try {
                sendTask();
                controller.save();

              }
              catch(Exception e) {
                return null;
              }
        }
        return null;
    }


    public void sendTask(){

        List<Package_Information__c> PacksToSend;

        if (accountPackages.equals(accountPackagesClone)){
            System.Debug('The Clone Equals the Original.');
            return;
        }
        else{

            for (Integer i = 0; i >= accountPackages.size(); i++){
                if (accountPackages[i].Check_Back__c != accountPackagesClone[i].Check_Back__c){
                    PacksToSend.add(accountPackages[i]);
                }
            }
        }

        for (Package_Information__c packs : PacksToSend){
            Task packTask = new Task();
            packTask.WhatId = thisAccount.Id;
            packTask.ActivityDate = packs.Check_Back__c;
            packTask.Type = 'Follow Up';
            packTask.OwnerId = UserInfo.getUserId();
            packTask.Status = 'Not Started';
            packTask.Priority = 'Normal';
            packTask.Description = 'Please call the person in charge of the training curriculum with '+ thisAccount.Name +' and take some time to speak with them regarding '+packs.Name;
            packTask.Subject = 'Check back regarding '+ packs.Name;

            insert packTask;
        }

    }

    public Package_Information__c newPackageInformation( Package__c pPackage){
        Package_Information__c newPack = new Package_Information__c();
        newPack.Account__c = thisAccount.Id;
        newPack.Package__c = pPackage.Id;
        newPack.Name = pPackage.Name;
        return newPack;
    }
}

Visualforce Page:

<apex:page standardController="Account" extensions="packageinformationcontroller" action="{!addMissingPackages}">
    <apex:form >
        <apex:pageBlock >
        <apex:commandButton action="{!saveChanges}" value="Save & Send any new Tasks"/>
            <apex:pageBlockTable value="{!accountPackages}" var="Package">



                <apex:column headerValue="Applicable">
                    <apex:inputCheckbox value="{!Package.Applicable__c}"/>
                </apex:column>

                <apex:column headerValue="Package Name" value="{!Package.Name}"/>

                <apex:column headerValue="Date Offered">
                    <apex:inputField value="{!Package.Date_Offered__c}"/>
                </apex:column>

                <apex:column headerValue="Status">
                    <apex:inputField value="{!Package.Status__c}"/>
                </apex:column>

                <apex:column headerValue="Check Back on:" >
                    <apex:inputField value="{!Package.Check_Back__c}"/>
                </apex:column>

                <apex:column headerValue="Notes" >
                    <apex:inputField value="{!Package.Notes__c}"/>
                </apex:column>

            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>   
</apex:page>

Finally, the Debug Log:

34.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
17:42:21.031 (31733217)|EXECUTION_STARTED
17:42:21.031 (31757544)|CODE_UNIT_STARTED|[EXTERNAL]|066c000000028xT|VF: /apex/packageinformation
17:42:21.031 (31939936)|VF_DESERIALIZE_VIEWSTATE_BEGIN|066c000000028xT
17:42:21.038 (38596522)|HEAP_ALLOCATE|[71]|Bytes:3
17:42:21.038 (38641519)|HEAP_ALLOCATE|[76]|Bytes:152
17:42:21.038 (38661518)|HEAP_ALLOCATE|[272]|Bytes:408
17:42:21.038 (38682998)|HEAP_ALLOCATE|[285]|Bytes:408
17:42:21.038 (38703230)|HEAP_ALLOCATE|[379]|Bytes:48
17:42:21.038 (38733910)|HEAP_ALLOCATE|[131]|Bytes:6
17:42:21.040 (40364093)|VF_DESERIALIZE_VIEWSTATE_END
17:42:21.041 (41286211)|CODE_UNIT_STARTED|[EXTERNAL]|01pc0000000I7Ve|packageinformationcontroller get(accountPackages)
17:42:21.041 (41301847)|SYSTEM_MODE_ENTER|true
17:42:21.041 (41337525)|CODE_UNIT_STARTED|[EXTERNAL]|01pc0000000I7Ve|accountPackages
17:42:21.041 (41353280)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:192
17:42:21.041 (41361706)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:28
17:42:21.041 (41365882)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:2
17:42:21.041 (41381866)|METHOD_ENTRY|[1]|01pc0000000I7Ve|packageinformationcontroller.packageinformationcontroller()
17:42:21.041 (41387793)|STATEMENT_EXECUTE|[1]
17:42:21.041 (41394159)|STATEMENT_EXECUTE|[1]
17:42:21.041 (41400167)|METHOD_EXIT|[1]|packageinformationcontroller
17:42:21.041 (41411437)|CODE_UNIT_FINISHED|accountPackages
17:42:21.041 (41431618)|CODE_UNIT_FINISHED|packageinformationcontroller get(accountPackages)
17:42:21.052 (52400597)|CODE_UNIT_STARTED|[EXTERNAL]|01pc0000000I7Ve|packageinformationcontroller invoke(saveChanges)
17:42:21.052 (52452553)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:24
17:42:21.052 (52468995)|VARIABLE_SCOPE_BEGIN|[48]|this|packageinformationcontroller|true|false
17:42:21.052 (52541078)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
17:42:21.052 (52593943)|VARIABLE_ASSIGNMENT|[48]|this|{"accountPackages":"0x3b4e1dc","accountPackagesClone":"0x5858346a","currentPackages":"0x6d94876","thisAccount":"0x23243e7d","totalPackages":"0x20829291"}|0x2a582c02
17:42:21.052 (52611365)|STATEMENT_EXECUTE|[48]
17:42:21.052 (52628787)|SYSTEM_METHOD_ENTRY|[49]|packageinformationcontroller.__sfdc_accountPackages()
17:42:21.052 (52689210)|VARIABLE_ASSIGNMENT|[-1]|this|{"accountPackages":"0x3b4e1dc","accountPackagesClone":"0x5858346a","currentPackages":"0x6d94876","thisAccount":"0x23243e7d","totalPackages":"0x20829291"}|0x2a582c02
17:42:21.052 (52706617)|SYSTEM_METHOD_EXIT|[49]|packageinformationcontroller.__sfdc_accountPackages()
17:42:21.052 (52780010)|SYSTEM_METHOD_ENTRY|[49]|List<Package_Information__c>.iterator()
17:42:21.052 (52926733)|SYSTEM_METHOD_EXIT|[49]|List<Package_Information__c>.iterator()
17:42:21.052 (52953347)|SYSTEM_METHOD_ENTRY|[49]|system.ListIterator.hasNext()
17:42:21.053 (53008807)|HEAP_ALLOCATE|[50]|Bytes:5
17:42:21.053 (53032995)|HEAP_ALLOCATE|[56]|Bytes:5
17:42:21.053 (53043936)|HEAP_ALLOCATE|[63]|Bytes:7
17:42:21.053 (53065653)|HEAP_ALLOCATE|[49]|Bytes:5
17:42:21.053 (53074951)|SYSTEM_METHOD_EXIT|[49]|system.ListIterator.hasNext()
17:42:21.053 (53112163)|HEAP_ALLOCATE|[49]|Bytes:36
17:42:21.053 (53126459)|VARIABLE_SCOPE_BEGIN|[49]|packs|Package_Information__c|true|false
17:42:21.053 (53178849)|VARIABLE_ASSIGNMENT|[49]|packs|{"s":1,"v":{"Account__c":"001c000000kBVHSAA4","Name":"Test Package","Applicable__c":true,"Check_Back__c":"2015-06-06T00:00:00.000Z","Date_Offered__c":"2015-06-01T00:00:00.000Z","Package__c":"a0nc0000003cUMpAAM","Id":"a0pc0000001zl7dAAA","Status__c":"Offered"}}|0x4637c64b
17:42:21.053 (53187372)|STATEMENT_EXECUTE|[49]
17:42:21.053 (53190149)|STATEMENT_EXECUTE|[50]
17:42:21.053 (53298598)|VARIABLE_SCOPE_BEGIN|[50]|controller|ApexPages.StandardController|true|false
17:42:21.053 (53418340)|VARIABLE_ASSIGNMENT|[50]|controller|"StandardController [[Package_Information__c (Account__c:001c000000kBVHSAA4, Name:Test Package, Applicable__c:true, Check_Back__c:Sat Jun 06 00:00:00 GMT 2015, Date_Offered__c:Mon Jun 01 00:00:00 GMT 2015, Package__c:a0nc0000003cUMpAAM, Id:a0pc0000001zl7dAAA, Status__c:Offered)]]"|0x295401cb
17:42:21.053 (53428638)|STATEMENT_EXECUTE|[51]
17:42:21.053 (53431356)|STATEMENT_EXECUTE|[51]
17:42:21.053 (53433483)|STATEMENT_EXECUTE|[52]
17:42:21.053 (53456651)|SYSTEM_METHOD_ENTRY|[52]|ApexPages.StandardController.save()
17:42:21.053 (53496060)|SYSTEM_MODE_ENTER|false
17:42:21.053 (53518095)|DML_BEGIN|[52]|Op:ControllerSave|Type:Package_Information__c|Rows:1
17:42:21.095 (95432406)|DML_END|[52]
17:42:21.095 (95552030)|SYSTEM_METHOD_EXIT|[52]|ApexPages.StandardController.save()
17:42:21.095 (95563209)|STATEMENT_EXECUTE|[53]
17:42:21.095 (95587848)|METHOD_ENTRY|[53]|01pc0000000I7Ve|packageinformationcontroller.sendTask()
17:42:21.095 (95617473)|VARIABLE_SCOPE_BEGIN|[63]|this|packageinformationcontroller|true|false
17:42:21.095 (95658390)|VARIABLE_ASSIGNMENT|[63]|this|{"accountPackages":"0x3b4e1dc","accountPackagesClone":"0x5858346a","currentPackages":"0x6d94876","thisAccount":"0x23243e7d","totalPackages":"0x20829291"}|0x2a582c02
17:42:21.095 (95667792)|STATEMENT_EXECUTE|[63]
17:42:21.095 (95670744)|STATEMENT_EXECUTE|[65]
17:42:21.095 (95675646)|VARIABLE_SCOPE_BEGIN|[65]|PacksToSend|List<Package_Information__c>|true|false
17:42:21.095 (95690248)|VARIABLE_ASSIGNMENT|[65]|PacksToSend|null|
17:42:21.095 (95700564)|SYSTEM_METHOD_ENTRY|[67]|packageinformationcontroller.__sfdc_accountPackages()
17:42:21.095 (95743422)|VARIABLE_ASSIGNMENT|[-1]|this|{"accountPackages":"0x3b4e1dc","accountPackagesClone":"0x5858346a","currentPackages":"0x6d94876","thisAccount":"0x23243e7d","totalPackages":"0x20829291"}|0x2a582c02
17:42:21.095 (95756627)|SYSTEM_METHOD_EXIT|[67]|packageinformationcontroller.__sfdc_accountPackages()
17:42:21.095 (95767902)|SYSTEM_METHOD_ENTRY|[67]|packageinformationcontroller.__sfdc_accountPackagesClone()
17:42:21.095 (95804372)|VARIABLE_ASSIGNMENT|[-1]|this|{"accountPackages":"0x3b4e1dc","accountPackagesClone":"0x5858346a","currentPackages":"0x6d94876","thisAccount":"0x23243e7d","totalPackages":"0x20829291"}|0x2a582c02
17:42:21.095 (95817237)|SYSTEM_METHOD_EXIT|[67]|packageinformationcontroller.__sfdc_accountPackagesClone()
17:42:21.095 (95837914)|SYSTEM_METHOD_ENTRY|[67]|List<Package_Information__c>.equals(Object)
17:42:21.095 (95992801)|SYSTEM_METHOD_EXIT|[67]|List<Package_Information__c>.equals(Object)
17:42:21.096 (96001519)|STATEMENT_EXECUTE|[67]
17:42:21.096 (96004967)|STATEMENT_EXECUTE|[68]
17:42:21.096 (96010947)|HEAP_ALLOCATE|[68]|Bytes:63
17:42:21.096 (96035607)|SYSTEM_METHOD_ENTRY|[68]|System.debug(ANY)
17:42:21.096 (96056717)|USER_DEBUG|[68]|DEBUG|We have had cloning in the South for years. Its called cousins.
17:42:21.096 (96064087)|SYSTEM_METHOD_EXIT|[68]|System.debug(ANY)
17:42:21.096 (96068359)|STATEMENT_EXECUTE|[69]
17:42:21.096 (96073533)|METHOD_EXIT|[53]|01pc0000000I7Ve|packageinformationcontroller.sendTask()
17:42:21.096 (96083738)|SYSTEM_METHOD_ENTRY|[49]|system.ListIterator.hasNext()
17:42:21.096 (96095156)|HEAP_ALLOCATE|[49]|Bytes:5
17:42:21.096 (96102434)|SYSTEM_METHOD_EXIT|[49]|system.ListIterator.hasNext()
17:42:21.096 (96121002)|VARIABLE_ASSIGNMENT|[49]|packs|null|
17:42:21.096 (96126371)|STATEMENT_EXECUTE|[59]
17:42:21.098 (98842033)|CODE_UNIT_FINISHED|packageinformationcontroller invoke(saveChanges)
17:42:21.098 (98889068)|VF_APEX_CALL|j_id3|{!saveChanges}|PageReference: none
17:42:21.100 (100584163)|CODE_UNIT_STARTED|[EXTERNAL]|01pc0000000I7Ve|packageinformationcontroller get(accountPackages)
17:42:21.100 (100599269)|SYSTEM_MODE_ENTER|true
17:42:21.100 (100609836)|CODE_UNIT_STARTED|[EXTERNAL]|01pc0000000I7Ve|accountPackages
17:42:21.100 (100619989)|CODE_UNIT_FINISHED|accountPackages
17:42:21.100 (100634579)|CODE_UNIT_FINISHED|packageinformationcontroller get(accountPackages)
17:42:21.130 (130207726)|VF_SERIALIZE_VIEWSTATE_BEGIN|066c000000028xT
17:42:21.132 (132003806)|VF_SERIALIZE_VIEWSTATE_END
17:42:21.135 (135790611)|CUMULATIVE_LIMIT_USAGE
17:42:21.135 (135790611)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

17:42:21.135 (135790611)|CUMULATIVE_LIMIT_USAGE_END

17:42:21.135 (135825466)|CODE_UNIT_FINISHED|VF: /apex/packageinformation
17:42:21.137 (137554054)|EXECUTION_FINISHED

Best Answer

First off, to debug this, I would take the VisualForce page out of the equation. That's what unit tests are for. Create a test class that inserts the necessary records (Accounts, Package information records...), exercises your controller and asserts that the tasks are created. Once you know your controller logic is working, creating the VF page is just markup and data binding.

I would also remove some code that, in my opinion, is a bit redundant and just complicates reading the code. Apologies if I sound pedantic (that is not my intention), but in the past I've seen many times how cleaning up your code makes bugs in the code more obvious to see.

For instance, you have a catch block that returns null, when the normal method exit point returns null. You instantiate a standard controller just to save a record... can't that be done with a simple update statement? Your saveChanges method could look like this:

public ApexPages.PageReference saveChanges(){
    for (Package_Information__c packs : accountPackages){
          try {
            sendTask();
            update packs;
          }
          catch(Exception e) {}
    }
    return null;
}

Incidentally, it might be worth adding a debug statement in that catch. Perhaps your code is ending there?

If you do this with all your code, there's fewer lines of code and fewer things to go wrong.

Having said that, my guess is that accountPackages.equals(accountPackagesClone) is evaluating to true and you're exit the statement. Is that funny debug statement (as highlighted by @brovasi) supposed to be the indicator that 'The clone equals the original'?

Related Topic