+ Start a Discussion
Deepak Sharma 184Deepak Sharma 184 

trigger scenario- need Help

have to update the "total actual cost field" in custom object called "project portfolio" from its associated custom object called "projects"'s fiels total actual cost. means all the "total actual cost" fields of different projects associated with the "project portfolio" should come into "total actual cost" field of "project portfolio" 

Both have total actual cost field.

 
salesforceMannsalesforceMann
Hi Deepak,
I am assuming that project portfolio is the parent object of project.
You dont need a trigger. The following solutions can be adopted:
1. If the relationship is master detail, simply create the total-cost field on project portfolio as a rollup field
2. If the relationship is lookup, create a workflow on project object to update total-cost field on project portfolio whenever project record is create or edited

If you need help in creating these, let me know.
Regards
Mann.

Please mark this as the best answer if you feel so!!
Deepak Sharma 184Deepak Sharma 184
Hi Mann _

"total actual cost" field of project is changable and have a formula in which this field is calculating some costs. hence that is changable. than how to  
set the criteria for that.

Regards,

Deepak 
salesforceMannsalesforceMann
Hi Deepak,

Create another rollup field - call it "rollup_total_cost__c". 
Then in the "total actual cost" formula field use the "rollup_total_cost__c" field

e.g. "total actual cost" = 2*rollup_total_cost__c
Deepak Sharma 184Deepak Sharma 184
Hi Mann-

Create roll up field on which object. there is a look up relationship between "Project Portfolio" and "Projects".
when i creating roll up field on "projects". it is asking for summarized objects which is required in which it is showing only "project members" object.

"total actual cost" field of projects there is a formula like-
CloudbyzITPM__Actual_Labor_Cost__c + CloudbyzITPM__Actual_Travel_Cost__c + CloudbyzITPM__Actual_HW_SW_Costs__c

and there is no option of creating roll up summary on "Project Portfolio" object.

Thanks & Regards

Deepak


 
salesforceMannsalesforceMann
You cant use rollups in this case. Go with optin2: Use a workflow.
 
EldonEldon
Hi Deepak,

Try the below trigger for roll up on look up field.(I did this for another req. I changed the object names to yours.I have assumed the child relationship name as pobs__r. Parent Object: project_portfolio__c  child object : projects__c,  Lookup field name on child : project_portfolio__c  )

 
trigger DevComPob3 on projects__c (after insert,after update, after delete,after undelete) {
    
    List<id> QuotationsIds = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
        For(projects__c con1 : Trigger.new){
            QuotationsIds.add(con1.project_portfolio__c);
        }
    }
    if(Trigger.isDelete){
        For(projects__c con1 : Trigger.old){
            QuotationsIds.add(con1.project_portfolio__c);
        }
    }
    List<project_portfolio__c> QuotationsToUpdate  = new List<project_portfolio__c>();
    decimal sum = 0;
    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
        For(Quotation__c q : [SELECT total_actual_cost__c ,(SELECT id,total_actual_cost__c FROM pobs__r) FROM project_portfolio__c WHERE id =: QuotationsIds]){
            sum = 0;
            for(projects__c p : q.pobs__r)
                sum = sum + p.total_actual_cost__c ;
            q.total_actual_cost__c = sum;
            QuotationsToUpdate .add(q);
        }
        try{
            update QuotationsToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
    }
}

let me know how it worked out.

Regards
Deepak Sharma 184Deepak Sharma 184
Hi Eldon,

Thanks for help.

what is pobs__r, in this ?

the relationship between them is look up.
like..in "project" there is a look up field for "project portfolio" means i can select "project portfolio" in "project" object.

Api name- 

Project- CloudbyzITPM__Project__c
Project Portfolio- CloudbyzITPM__Project_Portfolio__c
Field- CloudbyzITPM__Total_Actual_Cost__c



 
Deepak Sharma 184Deepak Sharma 184
and field type is currency.
EldonEldon
pobs__r is the childrelationship name of that lookup field. In the object manager select the look up field on the child object-CloudbyzITPM__Project__c. There you can see a child relation ship name. See below screenshot where pobs__c is the child and has a lookup to account with field name acc__c. Here its is pobs append it to pobs__r

User-added image


Regards
Deepak Sharma 184Deepak Sharma 184
Hi Eldon,

below is the screenshots attached.

User-added image

Child Relationship Name- Projects.

means i have to replace pobs__r with Projects__r

could u resend me the code.

Thanks & Regards,

Deepak
EldonEldon
trigger DevComPob3 on CloudbyzITPM__Project__c (after insert,after update, after delete,after undelete) {
    
    List<id> QuotationsIds = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
        For(CloudbyzITPM__Project__c con1 : Trigger.new){
            QuotationsIds.add(con1.CloudbyzITPM__Project_Portfolio__c);
        }
    }
    if(Trigger.isDelete){
        For(CloudbyzITPM__Project__c con1 : Trigger.old){
            QuotationsIds.add(con1.CloudbyzITPM__Project_Portfolio__c);
        }
    }
    List<CloudbyzITPM__Project_Portfolio__c> QuotationsToUpdate  = new List<CloudbyzITPM__Project_Portfolio__c>();
    decimal sum = 0;
    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
        For(CloudbyzITPM__Project_Portfolio__c q : [SELECT CloudbyzITPM__Total_Actual_Cost__c ,(SELECT id,CloudbyzITPM__Total_Actual_Cost__c FROM  projects__r) FROM CloudbyzITPM__Project_Portfolio__c WHERE id =: QuotationsIds]){
            sum = 0;
            for(CloudbyzITPM__Project__c p : q.projects__r)
                sum = sum + p.CloudbyzITPM__Total_Actual_Cost__c ;
            q.CloudbyzITPM__Total_Actual_Cost__c = sum;
            QuotationsToUpdate .add(q);
        }
        try{
            update QuotationsToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
    }
}

Regards
Deepak Sharma 184Deepak Sharma 184
Hi Eldon,

my code is 

trigger UpdateTotalActualCosts on CloudbyzITPM__Project__c (after insert,after update, after delete,after undelete) {

  List<id> QuotationsIds = new List<id>();

    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){

        For(CloudbyzITPM__Project__c con1 : Trigger.new){

            QuotationsIds.add(con1.CloudbyzITPM__Project_Portfolio__c);

        }

    }

    if(Trigger.isDelete){

        For(CloudbyzITPM__Project__c con1 : Trigger.old){

            QuotationsIds.add(con1.CloudbyzITPM__Project_Portfolio__c);

        }

    }

    List<CloudbyzITPM__Project_Portfolio__c> QuotationsToUpdate = new List<CloudbyzITPM__Project_Portfolio__c>();

    decimal sum = 0;

    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){

        For(CloudbyzITPM__Project_Portfolio__c q : [select CloudbyzITPM__Total_Actual_Cost__c,(SELECT id,CloudbyzITPM__Total_Actual_Cost__c  FROM Projects__r) FROM CloudbyzITPM__Project_Portfolio__c WHERE id =: QuotationsIds]){

            sum = 0;

            for(CloudbyzITPM__Project__c p : q.Projects__r)
                sum = sum + p.CloudbyzITPM__Total_Actual_Cost__c ;

            q.CloudbyzITPM__Total_Actual_Cost__c = sum;

            QuotationsToUpdate.add(q);

        }

        try{

            update QuotationsToUpdate ;

        }Catch(Exception e){

            System.debug('Exception :'+e.getMessage());

        }

    }

}

but now error it is showing is- 

Didn't understand relationship 'Projects__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
EldonEldon
Please tell me the line no where the error is occuring.
Also tell me the field names in both parent and child objects.Screenshots would be great
EldonEldon

Also try this, while creating your lookup field to the parent object CloudbyzITPM__Project_Portfolio__c give a different child relationship name.Then give that name appended with __r  instead of projects__r

User-added image


 
Deepak Sharma 184Deepak Sharma 184
HI eldon.

field names on both the objects are same-  
iam getting error on line no. 31
below is the screenshots attached
User-added imageUser-added imageUser-added image
User-added image
 
Deepak Sharma 184Deepak Sharma 184
Hi eldon,

look up relationship is already created.  can i create another look up relationship in this or can edit the current one.

Thanks 

Deepak

 
EldonEldon
Try editing it and giving a different child relationship name.Then use it with __r in the code