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
ParidhiBindalParidhiBindal 

Trigger to update another object if made change in one object

Hi I am writing my first trigger. Please help.  I have two objects - CourseOffering and CourseConnection, there is a field in CourseOffering named 'Faculty. I want to write a trigger by which on updating Faculty, Course Connection's field named 'Status' changes to 'Former' from 'Current'.

Please help. Thanks.
Prema -Prema -
Is there any lookup relation between these two objects? Have you created any lookup?
Shubham NandwanaShubham Nandwana
Hi Paridhi,
First query the courseConnection sobject row which is to be updated using lookup field and update that.
Adding sample code for your reference. 
If it resolves your doubt select it as best answer otherwise reply to ask anything else.
trigger CourseOfferingFieldUpdated on CourseOffering (after update) {

List<CourseConnection> listToUpdate=new List<CourseConnection>();
    for(CourseOffering courses:Trigger.new){  
               //get CourseConnection object using query via lookup field 
               CourseConnection c = [select Id,status from CourseConnection where (condition) ];
              if(c.status=='Former')
               c.status='Current';
               listToUpdate.add(c);
    }
   update listToUpdate;

}
Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
 
Akshay_DhimanAkshay_Dhiman
Hi Paridhi, 
  
  We have to create a lookup field first between both of these objects.

Try this code. it may be helpful for you:

 
trigger: 

 trigger CourseOfferingFaculty on CourseOffering__c (after update) 
 {
  if(trigger.isAfter && trigger.isUpdate)
  {
   CourseAfterUpdate.toUpdateAnotherObject(trigger.new);
  }
 }
 
 
 
Class or Handler class:
 
 
 public class CourseAfterUpdate {

    public static void toUpdateAnotherObject(List<CourseOffering__c> newRecord)
    {
        List<CourseOffering__c> newRecords = new List<CourseOffering__c>();
        List<CourseConnection__c> listToUpdate=new List<CourseConnection__c>();
     for(CourseOffering__c courses:newRecord)   
        { 
              newRecords.add(courses);
        }
        For(Integer i=0; i<newRecords.size();i++)
        {
         CourseConnection__c courseObj = [SELECT Id,
                                  Status__c 
                                  FROM CourseConnection__c 
                                  WHERE CourseOffering__r.Id=: newRecords[i].id];  //fetch lookup related record 
            
          if(courseObj.status__c=='Former')                           //modification as per requirement
             courseObj.status__c='Current';
             listToUpdate.add(courseObj);
        
            
        }
        if(listToUpdate.size() >0)
        {
      update listToUpdate;                                   //update
        }
        else {
            System.debug('There is no look up with CourseOffering__c');
            
        }
    }
}

If you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay
Prema -Prema -
trigger FacultyChanged on CourseOffering__c (after update) {
List<CourseConnection__c> courscon=[select Status__c from CourseConnection__c WHERE CourseOffering__c IN:Trigger.new];
System.debug('CourseConnection Lookup value:'+courscon);
List<CourseConnection__c> updateList=new List<CourseConnection__c>();
for(CourseOffering__c c:[select Faculty__c from CourseOffering__c WHERE ID IN:Trigger.new])
{

    System.debug('Course orffering c'+c);
    if(!trigger.oldMap.get(c.id).Faculty__c.EQUALS(trigger.newMap.get(c.id).Faculty__c))
    {
           for(CourseConnection__c c2:courscon)
           {
               c2.Status__c='Former';
               updateList.add(c2);
               System.debug('Status Value'+c2.Status__c);
           }
    }
}
update updateList;
}

You can try this trigger..
 
ParidhiBindalParidhiBindal
Hello Prema, I deployed your trigger and it is creating a new Course Connection for the updated faculty and not updating the status. Please let me know what can be done. Thanks.
Prema -Prema -
Please check if there is any other trigger on that object. You can deactivate it and then let me know because this trigger was working fine in my org.
Prema -Prema -
The Trigger will work if the status is Current as you had mentioned on the very first question.