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
AnidevAnidev 

Update Master record with child value

Hi All,

 

Following is a code i have written to update value of a field (Test__c) on master object "Invoice" with value from child object "Mail" field (Signature__c)

 

/* Assumption - only one child per parent record */

 

------------------------------------------------ Code------------------------------------------------------

 

trigger InventoryItemRollup on Mail__c (after delete, after insert, after update) {
 
    Set<id> shipmentIds = new Set<id>();
    List<Mail__c> ma = new List<Mail__c>();
    List<Invoice__c> shipmentsToUpdate = new List<Invoice__c>();
    List<string> s = new List<string>();
    for (Mail__c m:ma)
    {
    s.add(m.Invoice__c);
    for(string s1:s)
    {
    for (Mail__c item : Trigger.new)
        shipmentIds.add(item.Invoice__c);
 
    if (Trigger.isUpdate || Trigger.isDelete) {
        for (Mail__c item : Trigger.old)
            shipmentIds.add(item.Invoice__c);
    }
 
    Map<id,Invoice__c> shipmentMap = new Map<id,Invoice__c>([select id, Test__c from Invoice__c where id=:s1]);
 
    for (Invoice__c ship : [select Id,Test__c,(select Id,Signature__c from Mails__r)from Invoice__c where Invoice__c =:s1]) {
       shipmentMap.get(ship.Id).Test__c = shipmentMap.get(ship.Id).Mails__r.Signature__c;
         shipmentsToUpdate.add(shipmentMap.get(ship.Invoice__c));
    
    }
    update shipmentsToUpdate;
 
}
}
}

 

 

---------------------------------------------------- Code -------------------------------------------------------------------

 

Above not working.

The error must be very basic.

Please pardon my naivety.

 

Thanks in advance,

Anidev

 

 

Ref: Code by Jeff Douglas on roll-ups

 

 

 

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger InventoryItemRollup on Mail__c (before delete, after insert, after update)
{
  if(Trigger.isInsert || Trigger.isUpdate)
  {
    Set<id> shipmentIds = new Set<id>();
    List<Invoice__c> shipmentsToUpdate = new List<Invoice__c>();

    for (Mail__c item : Trigger.new)
    {
      shipmentIds.add(item.Invoice__c);
    }

    Map<id,Invoice__c> invoiceMap = new Map<id,Invoice__c>([SELECT Id, Test__c FROM Invoice__c WHERE Id IN :shipmentIds]);

    for (Mail__c mc : Trigger.new)
    {
      Invoice__c inv = invoiceMap.get(mc.Invoice__c);
      // For update and insert copies the value in Signature__c field on Mail Object to Test__c field on
      // Invoice Object.
      inv.Test__c = mc.Signature__c;

      shipmentsToUpdate.add(inv);
    }
    update shipmentsToUpdate;
  }

  if (Trigger.isDelete)
  {
    Set<id> shipmentIds = new Set<id>();
    List<Invoice__c> shipmentsToUpdate = new List<Invoice__c>();

    for (Mail__c item : Trigger.old)
    {
      shipmentIds.add(item.Invoice__c);
    }

    for (Invoice__c inv : [SELECT Id, Test__c FROM Invoice__c WHERE Id IN :shipmentIds])
    {
      // I am assuming that the parent record will not have any value in the test__c field if there is no child.
      // (One child per parent assumption.)
      inv.Test__c = null;
      shipmentsToUpdate.add(inv);
    }
    update shipmentsToUpdate;
  }
}

 I hope this would work.

 

    for (Mail__c m:ma)   
    {
        s.add(m.Invoice__c);
        .....

 you have nothing in ma, and so never enters the loop.