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
Srivat 11Srivat 11 

How to update dependent picklist in Leads from Tasks

Hello Trailblazers,

I have the following dependent picklist fields on the Lead object-

DNS_Drop_Down__c
Unqualified_List__c

I have created similar dependent picklist fields on the Activity/Task object-

DNS_Drop_Down_update__c
Unqualified_List_update__c

The following trigger on the Task object updates the 'Lead Status' picklist of Leads whenever the corresponding Task picklist field 'Lead Status Update' is selected. 
The requirement now is to update the DNS_Dro_Down__c and the Unqualified_List__c dependent picklists on the Leads whenever the corresponding task picklist fields - DNS_Drop_Down_update__c, Unqualified_List_update__c contain a value.

Here is the **Task Trigger** code-


trigger Task on Task (after insert, after update) 
   {
    

      
      Set<Id> whoIds = new Set<Id>();
    //Set<Id> whatIds = new Set<Id>();
     
     Map<Id,String> leadIdToStatus = new Map<Id,String>();
    //Map<Id,String> oppIdToStatus = new Map<Id,String>();

    for(Task t : Trigger.New){
        whoIds.add(t.WhoID);
        //whatIds.add(t.WhatId);
        if(t.Lead_Status_Update__c != null){
            leadIdToStatus.put(t.WhoId, t.Lead_Status_Update__c);             
        }
        /*if(t.Stage__c != null){
            oppIdToStatus.put(t.WhatId, t.Stage__c);
        }*/
    }

    List<Lead> leads = [SELECT Id FROM Lead WHERE Id =:whoIds];
    //List<Opportunity> opps = [SELECT Id FROM Opportunity WHERE Id =:whatIds];

    for(Lead l : leads){
        IF(leadIdToStatus.get(l.Id) != null){
            l.Status = leadIdToStatus.get(l.Id);
        }
        
    }

    /*for(Opportunity o : opps){
        o.StageName = oppIdToStatus.get(o.Id);
    }*/

    update leads;
    //update opps;
}

Here is the **Test class** for the Task Trigger-

@isTest
private class TestTaskTrigger {
    
    @isTest static void test_method_one() {
    
        Lead l = new Lead();
        l.LastName = 'Test';
        l.Company = 'Company';
        l.Status = 'new';
        insert l;

        Account a = new Account();
        a.Name = 'Test';
        insert a;

        Opportunity o = new Opportunity();
        o.AccountId = a.Id;
        o.Name = 'Test';
        o.StageName = 'New';
        Date d = Date.Today();
        o.CloseDate = d;
        insert o;

        Opportunity o1 = new Opportunity();
        o1.AccountId = a.Id;
        o1.Name = 'Test';
        o1.StageName = 'New';
        Date d1 = Date.Today();
        o1.CloseDate = d;
        insert o1;

        Task t = new Task();
        t.Lead_Status_Update__c = 'This';
        t.WhoId = l.Id;
        insert t;

        Task t2 = new Task();
        t.WhatId = o.Id;
        //t.Status__c = 'that';
        insert t2;
    }
}
Deepali KulshresthaDeepali Kulshrestha
Hi Srivat,
Please go through the below Link:
https://success.salesforce.com/answers?id=90630000000gifWAAQ

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha