+ Start a Discussion
Deepak Sharma 184Deepak Sharma 184 

trigger scenerio-

Hi,

I have a requirnment in which "hour rate" field should be updated with the "hour cost" of differrent object and we can also able to update the "hour rate" field manually and save it.

I have written the trigger for this. in which it always updating the field "hour rate" with the "hour cost". i can't able to update the "hour rate" field manually.

anybody can help with the logic.

public class updateHourlyRate
{
 public static List<CloudbyzITPM__Team_Member__c> affectedProd= new
List<CloudbyzITPM__Team_Member__c>();
 public static List<CloudbyzITPM__Team_Member__c> affectedProd_final=
new List<CloudbyzITPM__Team_Member__c>();
public static set<id> afc_test = new Set<id>();
 public static set<ID> afc2 = new set<ID>();
 public static void processAfterUpdateOrInsertHourlyCost(){
 affectedProd_final.clear();
      affectedProd = (List<CloudbyzITPM__Team_Member__c>)Trigger.New;
      System.debug('This is IT @@'+affectedProd);
     if(affectedProd.size()>0){
     for(CloudbyzITPM__Team_Member__c  affc : affectedProd){
      afc2.add(affc.CloudbyzITPM__Resource__c);
      afc_test.add(affc.id);
     }
     }
     CloudbyzITPM__Resource__c fn = [Select id,
CloudbyzITPM__Hourly_Cost__c from CloudbyzITPM__Resource__c where id
IN :afc2 limit 1];
     List<CloudbyzITPM__Team_Member__c> affectedProd1 = [Select
id,name,CloudbyzITPM__Hourly_Rate__c from CloudbyzITPM__Team_Member__c
where id IN :afc_test limit 1];
    // Decimal i =0 ;
     if(affectedProd1.size()>0){
     for(CloudbyzITPM__Team_Member__c  affc : affectedProd1){
     //i = i+ affc.CloudbyzITPM__Total_Actual_Cost__c;
    if(fn.CloudbyzITPM__Hourly_Cost__c != 0){
     affc.CloudbyzITPM__Hourly_Rate__c = fn.CloudbyzITPM__Hourly_Cost__c;
    affectedProd_final.add(affc);
    }
    }
     }
      System.debug('This is IT @@'+affectedProd);
     
if(affectedProd_final.size()>0)
     update affectedProd_final;
 }
  public static void processbeforeUpdateHourlyCost(){
 affectedProd_final.clear();
      affectedProd = (List<CloudbyzITPM__Team_Member__c>)Trigger.New;
      System.debug('This is IT @@'+affectedProd);
     if(affectedProd.size()>0){
     for(CloudbyzITPM__Team_Member__c  affc : affectedProd){
      afc2.add(affc.CloudbyzITPM__Resource__c);
      afc_test.add(affc.id);
     }
     }
     CloudbyzITPM__Resource__c fn = [Select id,
CloudbyzITPM__Hourly_Cost__c from CloudbyzITPM__Resource__c where id
IN :afc2 limit 1];
    /* List<CloudbyzITPM__Team_Member__c> affectedProd1 = [Select
id,name,CloudbyzITPM__Hourly_Rate__c from CloudbyzITPM__Team_Member__c
where id IN :afc_test limit 1];
    // Decimal i =0 ;
     if(affectedProd1.size()>0){*/
     for(CloudbyzITPM__Team_Member__c  affc : affectedProd){
     //i = i+ affc.CloudbyzITPM__Total_Actual_Cost__c;
    if(fn.CloudbyzITPM__Hourly_Cost__c != 0){
     affc.CloudbyzITPM__Hourly_Rate__c = fn.CloudbyzITPM__Hourly_Cost__c;
   // affectedProd_final.add(affc);
    }
    }
    // }
  //    System.debug('This is IT @@'+affectedProd);
     
//if(affectedProd_final.size()>0)
  //   update affectedProd_final;
 }
}

Trigger is- trigger hrtest on CloudbyzITPM__Team_Member__c (after insert,before update) {
if(Trigger.isInsert && Trigger.isAfter)   
{
System.debug('Trigger');
updateHourlyRate.processAfterUpdateOrInsertHourlyCost();
}
if(Trigger.isUpdate && Trigger.isBefore)   
{
System.debug('Trigger');
updateHourlyRate.processbeforeUpdateHourlyCost();

}