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
CSchaezCSchaez 

How to update a lead field based on associated activities

I'm fairly new to apex, so if this is really simple and I am just missing it, I apologize.

 

What I'm trying to do is take a boolean value field from the activity, use it to increment a field in the associated lead, and then toggle the boolean. Any help would be awesome.

sforce2009sforce2009

You did not say, you are using trigger or visualforce or scontrol

Anyway

    Task objTask = [select yourbooleanfield, WhoId from Task limit 1] //I am taking a task record

    if(yourbooleanfield == true) 

    {

        Lead objLead = new Lead(Id = objTask.WhoId);

        objLead.yourIncremtField = objLead.yourIncremetnField++;

        update objLead;

        objTask.yourbooleanfield = false;

        update objTask;
 

    } 

 

Hope this helps. 

Message Edited by sforce2009 on 06-27-2009 11:00 PM
CSchaezCSchaez

 Ok so during my trial and error process, I've made a few changes... 

 

The goal at this point is just to increment the increment field if bool__c is true.

 

for some reason, I keep on getting a "De-referencing a null object" error, even though there is a default value set.

 

Field Label# of CallsObject NameLead
Field NameinfrementField
Data TypeNumber
API NameincrementField__c  
Description 

 

 

trigger incrementStuff on Task(before insert)
{
    private Task[] newTask = Trigger.new;
    string subject = newTask[0].Subject;
    subject.toLowerCase();
    if    (subject.contains ('stuff'))
        newTask[0].bool__c = false;
  

    else
    {
        newTask[0].bool__c=true;
        string realId = newTask[0].WhoId;
        realId = realId.substring(0,15);
        Lead parent = new Lead(Id = realId);
        parent.incrementField__c = parent.incrementField__c + 1;
        update parent;
    }
   
}