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
DFDF 

Apex Trigger - Task(log a call) updating Lead status

I'm kinda new to apex, and im fairly stranded. Trying to basically create a trigger that on the completion of a log a call task, reads a particular field, and updates status based on it. More specifically, I'm not sure if what i want to do is possible.

 

To update  the lead shouldn't be too difficult. I would just run a SOQL on the lead Id and update the status field. I do this all day long from the API. The problem I'm really having is how to get information out of the current task. That is, how do i know what lead and what the contents of the tasks field are. 

 

Any help or direction would be much appreciated!

thecrmninjathecrmninja

This should work, I've included the test class.  I rewrote something I did for updating accounts based on related tasks so you may need to tweak it before it compiles.

 

trigger UpdateAccountRequester on Task (after insert) { for (Task t:System.Trigger.new) {if (t.subject=='ACCOUNT PACKAGE APPROVAL') { Lead LD = new Lead(Id=t.LeadId, CustomLeadField__c=t.Status);update LD; } }} //////////////////////////////Test Class////////////////////////////////////////////////////// /** * This class contains unit tests for validating the behavior of Apex classes * and triggers. * * Unit tests are class methods that verify whether a particular piece * of code is working properly. Unit test methods take no arguments, * commit no data to the database, and are flagged with the testMethod * keyword in the method definition. * * All test methods in an organization are executed whenever Apex code is deployed * to a production organization to confirm correctness, ensure code * coverage, and prevent regressions. All Apex classes are * required to have at least 75% code coverage in order to be deployed * to a production organization. In addition, all triggers must have some code coverage. * * The @isTest class annotation indicates this class only contains test * methods. Classes defined with the @isTest annotation do not count against * the organization size limit for all Apex scripts. * // TO DO: implement unit test /** *Insert two accounts, one with RP Active and *One With RP Inactive. Insert an Opportunity *and set o.rpaccount field to first Account *and verify system.assert does NOT equal */ /* * See the Apex Language Reference for more information about Testing and Code Coverage. */ @isTest private class AcPkReqeTester { static testMethod void myUnitTest() { /* pssst, lifted this from ... http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=11984 */ //First, prepare opportunity for the test data Lead Lead1 = new LEad(name='test account1'); insert Lead1; Account Lead2 = new Account(name='Not Updated'); insert Lead2; //System.debug('Created Account with Id: ' + acct1.id); //acct.Health_Status__c = 'Customer'; //update acct; Task TaskToCreate = new Task(WhatId=Lead1.Id,Status='YOURVALUEHERE'); insert TaskToCreate; ; // Verify correct action Lead LeadUpd = [select id, Status from Lead where id = :Lead1.Id]; //System.debug('THE VALUE OF HEALTH STATUS IS: ' + 'status'); System.assert(acctUpd.CustomField__C == TaskToCreate.Status; System.assert(Lead2.CustomField__c == null); } }

 

 

 

vandemanjwvandemanjw

Is it possible to do this in the opposite direction?  I would like to have the lead Status field appear on a task or event at the time that the activity is created.  I'm new to Apex, so any suggestions you have would be appreciated.

jpb@uhcjpb@uhc

Hi, I was looking for a trigger that did the same exact thing. However I am getting the following error message when running a test on the class:

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Opportunity/Account ID: id value of incorrect type: 00QQ0000005utbpMAA: [WhatId]

 

Any ideas?

Derek Anderson sbDerek Anderson sb

It looks like you are trying to put a lead id into the What field.  Put Lead and Contact Ids into the Who field.  This post is old enough you probably fiured that out, but someone else might stumble upon this, so I thought I'd contribute.