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
Wes McCarthyWes McCarthy 

How can i set a value on a mapped record?

I have code that creates a mapping from a Lead record.  if i want to set values back on the Lead record, how can i do this?  The code i am using is below, however, nothing seems to happen.  Is this line of code possible?

  LeadtaskMap.get(tsk.whoid).Status = 'Contacted';  

Any suggestions?
Thanks.

trigger LeadTrigger on Task (before insert, before update) 
{
    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new)
    {
        if(t.Status=='Completed')
        {
            if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE) //check if the task is associated with a lead
            {
                leadIds.add(t.whoId);
            }//if 2
        }//if 1
    }//for
        
    map<id,Lead> LeadtaskMap = new map<id,Lead>([SELECT Id, Description, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE]);
    
    for(Task tsk:trigger.new)
    {
    
      if(LeadtaskMap.keyset().contains(tsk.whoid))
      {
            if(tsk.Status == 'Completed' && tsk.Subject == 'Make contact with Lead')
            {
                LeadtaskMap.get(tsk.whoid).Status = 'Contacted';
            }
            
            if(LeadtaskMap.get(tsk.whoid).Description == null && tsk.Status == 'Completed' && tsk.Subject == 'Qualify Opportuntity')
            {
                tsk.adderror('Task cannot be completed until the following fields have been populated: Description');
            }
            Else 
            {
                LeadtaskMap.get(tsk.whoid).Status = 'Qualified';
            }
      }
        
    } 
    
    }

Paul_BoikoPaul_Boiko
I think what is missing in this code is update of the leads.
Try adding this code as a last line of code in trigger: 
List<Lead> leads = LeadtaskMap.values();
​update leads;

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
http://amitsalesforce.blogspot.in/2014/11/dynamic-field-mapping-using-custom.html

Like below code you can do.
LeadObj=new Lead();
   MyLeadid=ApexPages.currentPage().getParameters().get('MyLeadid');
   getAllMapping();
   qry = 'select ' + qry + 'id FROM My_Lead__c where id =: MyLeadid';
   My_Lead__c MyLead = Database.query(qry);

   for(String sMyLeadField: MapMappingTable.keySet())
   {
    String sLeadField = MapMappingTable.get(sMyLeadField);
    LeadObj.put(sLeadField, MyLead.get(sMyLeadField));
   }
Please let us know if this will help  you

Thanks
Amit Chaudhary