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
Supriyo Ghosh 9Supriyo Ghosh 9 

trigger related issue

Hi,

I want to copy one value from child obj to parent obj.

trigger Copyleadvaluetoacc on Lead (after insert, after update) {
    
    List<Id> accIds = new List<Id>();
    List<Account> accounts = new List<Account>();
    
    for(Lead L : trigger.new){
        accIds.add(L.Id);
    }
    
    for(Account a : [SELECT Id, Lead_Status__c FROM Account WHERE Id IN :accIds]){
        for(Lead ld:  trigger.new){
            a.Lead_Status__c=ld.Status;
            accounts.add(a);
        }
    }
    if(accounts.size()>0){
        Update accounts;
    }

But it is not working.Please help
Best Answer chosen by Supriyo Ghosh 9
ANUTEJANUTEJ (Salesforce Developers) 
I tried writing this simple snippet below, this only works for one record and in case if you want to bulkify it then you need to fetch the multiple lead records for that account id and then assign the value after concatination, you can modify the below snippet as per your use case:
trigger Copyleadvaluetoacc on Lead  (after insert, after update) 
{ 
  if(trigger.isafter && (trigger.isinsert || trigger.isupdate)){}
  Map<ID, String> LeadMap  = new Map<ID, String>();
  list<Account> aclist     = new list<Account>();

  for (Lead childObj : Trigger.new) {
    LeadMap.put(childObj.AccountId , childObj.Lead_Status__c);
    }
  
  for (Id key : LeadMap.keySet()) {
    Account a         = new Account();
    a.id              = key;
    a.Lead_Status__c  = LeadMap.get(key);
    aclist.add(a);
    }

  if(aclist.size()>0)
  {update aclist;}
  }
}
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Supriyo,

I think this is something that can be solved simply using a process builder instead of trigger, below is the link to an implementation you can use as reference:

>> https://trailblazers.salesforce.com/answers?id=9063A000000DwewQAC

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Supriyo Ghosh 9Supriyo Ghosh 9
Actually I cannot resolve it with process builder.So f you can help me with my trigger code.
ANUTEJANUTEJ (Salesforce Developers) 
I tried writing this simple snippet below, this only works for one record and in case if you want to bulkify it then you need to fetch the multiple lead records for that account id and then assign the value after concatination, you can modify the below snippet as per your use case:
trigger Copyleadvaluetoacc on Lead  (after insert, after update) 
{ 
  if(trigger.isafter && (trigger.isinsert || trigger.isupdate)){}
  Map<ID, String> LeadMap  = new Map<ID, String>();
  list<Account> aclist     = new list<Account>();

  for (Lead childObj : Trigger.new) {
    LeadMap.put(childObj.AccountId , childObj.Lead_Status__c);
    }
  
  for (Id key : LeadMap.keySet()) {
    Account a         = new Account();
    a.id              = key;
    a.Lead_Status__c  = LeadMap.get(key);
    aclist.add(a);
    }

  if(aclist.size()>0)
  {update aclist;}
  }
}
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer