1

I have a situation where I have a two objects called Premises and Location. I have a master detail relationship from Premises to Location. I want to write a trigger so that whenever a recored is changed on the Location, it would also reflect on the related Premises records. Im not quite sure how to approach this. Any help. thanks

PS: Premise is the detail record

This is what i've tried so far:

global override void MainEntry(List<SObject> newlist, Map<ID, SObject> newmap, List<SObject> oldlist, Map<ID,SObject> oldmap)
    {
        if(Trigger.isAfter && Trigger.isUpdate){

            executeUpdated((List<Location__c>)newList, oldmap);
        }
    }

    private void executeUpdated(List<Location__c> newlist, Map<ID, SObject> oldmap)
    {
        List<String> locationList = new List<String>();
        for (Location__c loc : newList)
        {
            locationList.add(loc.Id);
        }

        Map<Id, Premises> locMap = new Map<Id, Premises>([SELECT Id, Location__c FROM Premises__c WHERE Id =: locationList]);

        for (Location__c loc : newList)
        {
            Location__c lookUpObj = locMap.get(loc.Id);
            if (lookUpObj != null){}
            //obj.Master1 = lookup1_Object.Lookup2;
        }
    }
  • Is Location the Detail and Premises the Master? The Detail record will have a field with the Id of the Master you can use to update the Master. If the reverse, you'll need to query the Detail object using the Master Object record's Id to get those records. What issue are you having? – crmprogdev May 1 '15 at 14:50
  • The Premises__c is the Detail. Could you please provide some code example @crmprogdev – codeSlayer May 1 '15 at 15:07
1

//Use it if Child Record to be updated only under certain Field update on the parent

private void executeUpdated(){
List<String> locationList = new List<String>();

    for (Location__c newLoc : Trigger.new) {
        Location__c oldLoc = Trigger.oldMap.get(newLoc.Id);
        // To check if a Particular field has been updated
        if (newLoc.Fieldname != oldLoc.Fieldname) {
            locationList.add(newLoc.Id);
        }
    }
    List<Premises__c> listToUpdate = new List<Premises__c>();
    Premises__c premiseToUpdate = new Premises__c();
    Map<Id, Premises> locMap = new Map<Id, Premises>([SELECT Id, Location__c FROM Premises__c **WHERE Id IN: locationList**]);

        for (Location__c loc : locationList)
        {
            if (locMap.get(loc.Id) != null){
                // The Update 
                premiseToUpdate = locMap.get(loc.Id);
                premiseToUpdate.fieldToUpdate = <updated Value>;
                listToUpdate.add(premiseToUpdate);
            }
        }
        database.update(listToUpdate);
}

Refer: http://developer.force.com/cookbook/recipe/mass-updating-contacts-when-an-account-changes

  • Good try at this, but it won't work. Your creation of the mapping like that won't work since Premise is a different object than location. Also, even if that did work it would only update a single premise instead of all related ones. You would need a Map<Id, List<Premise__c>> to do that. If that was fixed though it could be a viable answer. – dphil May 1 '15 at 15:21
  • Premise is the detail record. @dphil could you pls provide some code example? – codeSlayer May 1 '15 at 15:36
  • @ShashankShastri, when you post code, after pasting it into your post, please select it and click on the {} pre-formatted text icon so that it's legible to anyone who reads it. It's always a good practice to look at the preview pane of your post to fix any problems before posting it. Posts that are hard to read or understand are of little value as answers. – crmprogdev May 1 '15 at 15:48
  • Map<Id, List<Premise__c>> premiseMap = new Map<Id, List<Premise__c>>(); for (Premise__c pre: [SELECT Id FROM Premise__c WHERE Location__c IN :locationList]) {List<Premise__c> prems = premiseMap.get(pre.Location__c);if(prems == null){prems = new List<Premise__c>();premiseMap.put(pre.Location__c, prems);}prems.add(pre);} – dphil May 1 '15 at 17:34
  • @dphil I have an error of : Illegal assignment from Premises__c to List<RS_Premises__c>. How do I convert the map into a List? thx – codeSlayer May 5 '15 at 10:05
0

I assume Premise is master and locaion is detail.In future you may want to define relationship clearly.I type from my phone so I am not able to format the code.hopefully this will help.

  trigger AfterPremisUpdate on Premise__c (after update) {  
   Set<Id> premisIds = new Set<Id>();
   Map<Id,Premise__c> mapPremise= new Map<Id,Premise__c>()
   for(Premise__c s : Trigger.new) { 
     premisIds.add(s.Id);
     mapPremise.put(s.Id,s);
   } 
   //Inside the same trigger or utility class , just get back the related Permission object with location id.

   List < Location__c> pList = [select id,whateverrelatedfield,Premise__c from Location__c where Premise__c in : premisIds. ];
   List < Location__c> updatelist =new List < Location__c>();
   for(Location__c p: pList){
      if(mapPremise.containskey(p.Premise__c){
         p. whateverrelatedfield = updatevalue;
         updatelist.add(p);
   }
   update updatelist;    
 }
  • When you post code, after pasting it into your post, please select it and click on the {} pre-formatted text icon so that it's legible to anyone who reads it. It's always a good practice to look at the preview pane of your post to fix any problems before posting it. Posts that are hard to read or understand are of little value as answers. This is not the first time I've edited one of your posts to make your code readable. – crmprogdev May 1 '15 at 15:46
  • actually @unidha, the Premises__c is the Detail and Location is the Master. could you pls modify the code to. will mark answer your answer.. thx – codeSlayer May 1 '15 at 16:03
  • actually @crmprogdev I could not see that {} in my mobilephone ,anyway I update the answer using my laptop.@codeSlayer I already updated the answer. – unidha May 4 '15 at 4:29
0

If I understand your requirements, this is possible to do using the Process Builder as well. In this example, I created the Location object along with the child Premises object. Both objects have a lookup to the Account object. The process builder is set to trigger on the Location object "when a record is created or edited" and when the lookup "Account" field is changed. The immediate action is to take that Account value on Location and set it on all child Premises records.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.