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
Deepak Sharma 184Deepak Sharma 184 

updating child object field with the parent object field.

Hi Expert,

There r two objects "project member" and "resource" in which under "project member" object i have to update the field "hour rate" with "resource" object field "hour cost", when something is written in "hour cost" field, otherwise "hour rate" field not update and able to enter something and save it.

my code is- 

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> afc2 = new set<ID>();
 public static void processAfterUpdateOrInsertHourlyCost(){
      affectedProd = (List<CloudbyzITPM__Team_Member__c>)Trigger.New;
     if(affectedProd.size()>0){
     for(CloudbyzITPM__Team_Member__c  affc : affectedProd){
      afc2.add(affc.CloudbyzITPM__Team_Member__c);
     }
     }
     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 CloudbyzITPM__Resource__c = :fn.id];
    // Decimal i =0 ;
     if(affectedProd1.size()>0){
     for(CloudbyzITPM__Team_Member__c  affc : affectedProd1){

     affc.CloudbyzITPM__Hourly_Rate__c = fn.CloudbyzITPM__Hourly_Cost__c;
    affectedProd_final.add(affc);
    }
     }

     update affectedProd_final;
 }
}

trigger- trigger HourRate on CloudbyzITPM__Resource__c (before insert, before update, before delete, after insert, after update, after delete) 
{
if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter)   
{
updateHourlyRate.processAfterUpdateOrInsertHourlyCost();

}

when i change the field "hourly cost" , it gives an error.
"Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger HourRate caused an unexpected exception, contact your administrator: HourRate: execution of AfterUpdate caused by: System.TypeException: Invalid conversion from runtime type List to List: Class.updateHourlyRate.processAfterUpdateOrInsertHourlyCost: line 10, column 1"
Agustina GarciaAgustina Garcia
Look at this line:
 
affectedProd = (List<CloudbyzITPM__Team_Member__c>)Trigger.New;
and your trigger
 
trigger HourRate on CloudbyzITPM__Resource__c (before insert, before update, before delete, after insert, after update, after delete)
is based on CloudbyzITPM__Resource__c object. So that, you cannot create a List of CloudbyzITPM__Team_Member__c elements. It shoudl be list of CloudbyzITPM__Resource__c

In any case, I'm not sure how this line
affectedProd = (List<CloudbyzITPM__Team_Member__c>)Trigger.New;
compile because Trigger.New and all Trigger context are only valid on trigger files rather than classes.

Hope this helps
Agustina


 
Matt SmelserMatt Smelser
Have you thought about using a process builder to handle this versus a trigger? 
Deepak Sharma 184Deepak Sharma 184
Hi Augtina,

Thanks for reply.

I modified the code. 

public class updateHourlyRate2 
{

 public static List<CloudbyzITPM__Resource__c> affectedProd= new
     List<CloudbyzITPM__Resource__c>();
 public static List<CloudbyzITPM__Team_Member__c> affectedProd_final=
new List<CloudbyzITPM__Team_Member__c>();

 public static set<ID> afc2 = new set<ID>();
 public static void processAfterUpdateOrInsertHourlyCost(){
      affectedProd = (List<CloudbyzITPM__Resource__c>)Trigger.New;
     if(affectedProd.size()>0){
     for(CloudbyzITPM__Resource__c  affc : affectedProd){
      afc2.add(affc.Cloudbyz__Hourly_cost__c);
     }
     }
     CloudbyzITPM__Resource__c fn = [Select id,
CloudbyzITPM__Hourly_Cost__c from CloudbyzITPM__Resource__c where id
IN :afc2 limit 1];
     List<CloudbyzITPM__Resource__c> affectedProd1 = [Select
id,name,CloudbyzITPM__Hourly_Cost__c from CloudbyzITPM__Resource__c
where id in: fn];
    // Decimal i =0 ;
     if(affectedProd1.size()>0){
     for(CloudbyzITPM__Team_Member__c  affc : affectedProd1){

     affc.CloudbyzITPM__Hourly_Rate__c = fn.CloudbyzITPM__Hourly_Cost__c;
    affectedProd_final.add(affc);
    }
     }

     update affectedProd_final;
 }
}

Can u plz tell where iam wrong here.

error- 

Invalid field Cloudbyz__Hourly_cost__c for SObject CloudbyzITPM__Resource__c

Can u plz tell where iam wrong here.

Deepak Sharma 184Deepak Sharma 184
Hi Matt,

How to do it with process builder. can u plz give the logic, because it needs to be updated when some value would be there in "hour cost" field.

 
Agustina GarciaAgustina Garcia
Look at this link (https://www.youtube.com/watch?v=APiJg7908jk) for a quick introduction to Process Builder. Also it would be good if you do Trailhead (https://trailhead.salesforce.com/en/business_process_automation/process_builder

The idea is that:

1. Process Builder is based on your Resource object. When a record is created or updated
2. The action to fire is that Resource.HourCost field is not null (someone has written a value there)
3. The action after the fire is to set ProjectMember.HourRate field with the value you need

Hope this helps
Deepak Sharma 184Deepak Sharma 184
Hi Agustina garcia,

Thanks for the Help, I will look into this.

Regards,
Deepak