• Vivian Ho 9
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hello All,

I am trying to write a trigger to update a parent object field when its child record is added/updated. I am quite new to Salesforce Apex and tried to read a few posts in the forum and still could not get it working. Any thoughts on this is appreciated. Thank you!

Example:
Parent Object: Sales_Event__c
Field to update: Cost_Item_Update__c
Child Object: Sales_Event_Cost_Item__c (Master-Detail(Sales Event))
Sales_Event_ID__c = Sales_Event__r.Id (I’m not too sure if there is proper way to get Sales_Event ID in Sales_Event_Cost_Item__c so I create a new formula field here which I think may not be necessary though..)
Fields to be CONCATENATED: Cost_Items__c and  Quantity__c
So I would like to get below in my Parent Object:
e.g. Sales Event A has 2 Cost items,
Cost_Item_Update__c = “Cost_Item one (Quantity__c), Cost_Item two (Quantity__c)”
But my Cost_Item_Update__c  even can’t get the Cost_Items__c (Cost_Item one, Cost_Item two iteration) right.. I am wondering if I map the objects incorrectly...
trigger TestSalesEventCostItemUpdate on Sales_Event_Cost_Item__c (after insert, after delete, after update) {

    list<id> parentIDstoUpdate = new list<id>();
    if(Trigger.isInsert||Trigger.isAfter||Trigger.isDelete ){
            for (Sales_Event_Cost_Item__c co : Trigger.new) {
                parentIDstoUpdate.add(co.Sales_Event__c);       
            } 
    }

    map<id,string>  mapParentIdtoString = new Map<ID,String>();
    string str;

    for (Sales_Event_Cost_Item__c co: [SELECT Sales_Event_ID__c, Cost_Items__c from Sales_Event_Cost_Item__c WHERE Sales_Event_ID__c =:parentIDstoUpdate ]){
        if(mapParentIdtoString.containsKey(co.Sales_Event_ID__c)){
            str = mapParentIdtoString.get(co.Sales_Event_ID__c);
            str = str + ';' + co.Cost_Items__c;  
            mapParentIdtoString.put(co.Sales_Event_ID__c,str);
        }else{
            mapParentIdtoString.put(co.Sales_Event_ID__c,co.Cost_Items__c);
        }
    }
    
    list<Sales_Event__c> recordstoUpdate = new list<Sales_Event__c>();
    for (Sales_Event__c p: [SELECT ID, Cost_Item_Update__c FROM Sales_Event__c WHERE id =:recordstoUpdate ]){ 
        p.Cost_Item_Update__c = mapParentIdtoString.get(p.id);
        recordstoUpdate.add(p);
    }

    if (recordstoUpdate != null && recordstoUpdate.size()>0){
        update recordstoUpdate;
    }
}
I want to create a many-to-many relationship between Accounts and  a custom object “Package”

The data model is like
Company A (account): package01,02,03 (Package)
Company B(account): package01,02 (Package)
Company C(account): package03,04,05 (Package)
……

I have created a Junction Object “Package Linkage to Account” (as i want to link up the above 2 objects), it looks the object “Package Linkage to Account” is something i want,  but i come across to an issue inserting multiple records of one package under multiple accounts at a time.

Right now I can insert 1 record of 1 package (under the Master-Detail(Package)) and associate it with 1 Account (under the Master-Detail(Account) in the “Package Linkage to Account” layout.

But my intention is to input 1 package under multiple accounts at one time in the layout (i know data loader could help to load multiple rows data in one go), but i would like to achieve the same using the salesforce UI (say user can search the Accounts under the ‘search’ pop up screen and then selected multiple accounts at one time in the web UI)

I am not sure if there is existing fields/buttons to select multiple Accounts under custom object in one go. I am wondering if i need to further work on ‘Flow’ or new custom ‘List button’ to do this job (but i am not so good at coding). Appreciate if anyone could help to advise on how to insert multiple records under custom object relate to multiple Account in one go via UI (I am quite new to salesforce and can’t figure out the way after googling...). Thank you!
Hello All,

I am trying to write a trigger to update a parent object field when its child record is added/updated. I am quite new to Salesforce Apex and tried to read a few posts in the forum and still could not get it working. Any thoughts on this is appreciated. Thank you!

Example:
Parent Object: Sales_Event__c
Field to update: Cost_Item_Update__c
Child Object: Sales_Event_Cost_Item__c (Master-Detail(Sales Event))
Sales_Event_ID__c = Sales_Event__r.Id (I’m not too sure if there is proper way to get Sales_Event ID in Sales_Event_Cost_Item__c so I create a new formula field here which I think may not be necessary though..)
Fields to be CONCATENATED: Cost_Items__c and  Quantity__c
So I would like to get below in my Parent Object:
e.g. Sales Event A has 2 Cost items,
Cost_Item_Update__c = “Cost_Item one (Quantity__c), Cost_Item two (Quantity__c)”
But my Cost_Item_Update__c  even can’t get the Cost_Items__c (Cost_Item one, Cost_Item two iteration) right.. I am wondering if I map the objects incorrectly...
trigger TestSalesEventCostItemUpdate on Sales_Event_Cost_Item__c (after insert, after delete, after update) {

    list<id> parentIDstoUpdate = new list<id>();
    if(Trigger.isInsert||Trigger.isAfter||Trigger.isDelete ){
            for (Sales_Event_Cost_Item__c co : Trigger.new) {
                parentIDstoUpdate.add(co.Sales_Event__c);       
            } 
    }

    map<id,string>  mapParentIdtoString = new Map<ID,String>();
    string str;

    for (Sales_Event_Cost_Item__c co: [SELECT Sales_Event_ID__c, Cost_Items__c from Sales_Event_Cost_Item__c WHERE Sales_Event_ID__c =:parentIDstoUpdate ]){
        if(mapParentIdtoString.containsKey(co.Sales_Event_ID__c)){
            str = mapParentIdtoString.get(co.Sales_Event_ID__c);
            str = str + ';' + co.Cost_Items__c;  
            mapParentIdtoString.put(co.Sales_Event_ID__c,str);
        }else{
            mapParentIdtoString.put(co.Sales_Event_ID__c,co.Cost_Items__c);
        }
    }
    
    list<Sales_Event__c> recordstoUpdate = new list<Sales_Event__c>();
    for (Sales_Event__c p: [SELECT ID, Cost_Item_Update__c FROM Sales_Event__c WHERE id =:recordstoUpdate ]){ 
        p.Cost_Item_Update__c = mapParentIdtoString.get(p.id);
        recordstoUpdate.add(p);
    }

    if (recordstoUpdate != null && recordstoUpdate.size()>0){
        update recordstoUpdate;
    }
}