0

I have a field status on my parent object. If the status in its child is updated, the parent status is updated accordingly. Ex: if one of the childStatus == 'sad', parentStatus = 'sad' if one of the childStatus == 'cry', parentStatus = 'cry' if all childStatus == 'happy', parentStatus = 'happy'.

I'm new to salesforce, please help.

Here's my sample code so far.

public static String updatejobApplicationStatus(Set<Id> offerIdSet){
        if(offerIdSet != null || offerIdSet.size()> 0){
            set<id> jobApplicationIdSet = new set <id>();
            String status = '';

        //----------------using offer id, get the application id of those offer id---------------   
        list<Offer__c> offersList = [Select Id,Job_Application__c FROM Offer__c WHERE id in:offerIdSet];
        for(Offer__c jobAppId: offersList){
            jobApplicationIdSet.add(jobAppId.Job_Application__c);                
        }
        //------------------- get the status of these offers------------------------             
        list<Offer__c> offersUpdateList = [Select Id,Job_Application__c,Status__c FROM Offer__c WHERE Job_Application__c in:jobApplicationIdSet];
        system.debug('----------------offersUpdateList---------'+ offersUpdateList); 
        map<id, string> jobMapOffer = new map<id, string>();    
        map<id, list<string>> mapList= new map<id, list<string>>();
        list<String> offerStatusList = new list<String>();

        Set<Job_Application__c> jobAppUpdate = new Set<Job_Application__c>();

            for(Offer__c off : offersUpdateList){

              if(off.Status__c == 'Part Time'){                     
                    Job_Application__c newJob = new Job_Application__c(Status__c = off.Status__c ,Id = off.Job_Application__c);
                    jobAppUpdate.add(newJob);
                    break;  
                }                                     

                else if(off.Status__c == 'Night'){                      
                    Job_Application__c newJob = new Job_Application__c(Status__c = off.Status__c,Id = off.Job_Application__c);
                    jobAppUpdate.add(newJob);
                    break;

                }
                else if(off.Status__c == 'Day'){                

                    Job_Application__c newJob = new Job_Application__c(Status__c =off.Status__c ,Id = off.Job_Application__c);
                    jobAppUpdate.add(newJob);

                }
                else{                       
                    Job_Application__c newJob = new Job_Application__c(Status__c = 'Full day' ,Id = off.Job_Application__c);
                    jobAppUpdate.add(newJob);
                    break; 

                }


            }
             List<Job_Application__c> jobList = New List<Job_Application__c>(jobAppUpdate);
             update jobList;            
    }
    return null;
}    
  • Is the relationship of Offer__c and Job_Application__c master-detail or lookup? And do you have flexibility to change fields, such as changing Job_Application__c.Status__c from text to formula? – Thomas Crouse Apr 3 '15 at 16:00
  • You can refer @animesh answer. I believe it might work. – snehakem Apr 6 '15 at 7:40
3

You can try this one. It is made on Contact which is child and account as its parent.

trigger UpdateChildRecord on Contact(after update) {

 Set <ID> parentSet = new Set <ID>();
 for(Contact c : Trigger.new)
 {
  parentSet.add(c.AccountId);
 }

 List <Account> accList = [SELECT Id, Parent_Status__c, (SELECT Id, Child_Status__c FROM Contacts) FROM Account WHERE Id IN : parentSet];

 for(Account a : accList)
  {
   integer count=0;
   for(Contact c : a.Contacts)
   {
    if(c.Child_Status__c == 'Cry')
    {
     a.Parent_Status__c = 'Cry';
    }
    else if(c.Child_Status__c == 'Sad')
    {
     a.Parent_Status__c = 'Sad';
    }
    else if(c.Child_Status__c == 'Happy')
    {
     count++;
     //a.Parent_Status__c = 'Happy';
    }   
   }  
   if(a.contacts.size()==count){
       a.Parent_Status__c = 'Happy';
   }

  }
  update accList;
}
0

You will have to try something. We cant provide the whole code here. I can provide you the logic though,

Write a trigger on Child object after insert and using inner query for parent to update its status.

You can learn writing trigger here, https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm

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.