+ Start a Discussion
GRStevenBrookesGRStevenBrookes 

Easy Trigger Help

Hi,

 

Cant get my head around this!

 

I am trying to write what I perceive to be quite a simple trigger.

 

I have two objects, the standard Account object and a custom object (Master-Detail (Child)) Service_Agreement__c.

 

When the Owner is changed on the Accoutn object, I need the field Account_Manager_Senior_PA__c on all of the child Service_Agreement__c records (which is connected to the User Object) to be updated with the same 'Owner'.

 

I have started by using:

 

trigger UpdateServiceAgreement_WhenAccMgrAllocated on Account (after update) {

	List<Service_Agreement__c> si = [select Id, Account_Manager_Senior_PA__c from Service_Agreement__c
	 where Account in: Trigger.new ];


for(Service_Agreement__c sic : si){
     sic.Account_Manager_Senior_PA__c = ?????;
}
update si;

}

 ...but @ ?????? get stuck!! not even sure if this would work anyhow.

 

Any help MUCH apprecaited.

 

Steve

Saikishore Reddy AengareddySaikishore Reddy Aengareddy
Hope this will help... I have used sample instead of service_Agreement__c... and updated a field on sample__c with the owner of Account. It worked well. 

trigger UpdateServiceAgreement_WhenAccMgrAllocated on Account (after update) {

    set<Id> AccIds = new set<Id>();
    Map<Id,List<Sample__c>> AccIdMap = new Map<Id,List<Sample__C>>();
    List<Sample__c> updateList = new List<Sample__c>();
    
    for(Account a : Trigger.new){
        AccIds.add(a.Id);    
    }
    for(sample__c s : [select Id, Account__c,User__c from sample__c where Account__c in: AccIds ]){
        if(!AccIdMap.containsKey(s.Account__c)){
            List<sample__c> temp = new List<Sample__c>();
            temp.add(s);
            AccIdMap.put(s.Account__c,temp);
        }else{
            List<sample__c> temp1 = new List<sample__c>();
            temp1 = AccIdMap.get(s.Account__c);
            temp1.add(s);
            AccIdMap.put(s.Account__c,temp1);
        }
    }
    
    for(Account a : Trigger.new){
        if(AccIdMap.containsKey(a.Id)){
            for(Sample__c s : AccIdMap.get(a.Id)){
                s.User__c = a.OwnerId;
                updateList.add(s);
            }
        }
    }
    
    if(updateList.size()>0){
        update updateList;
    }
    
}