2

I have several opportunities, which all have a record field assigned to a specific project. The project, which they are assigned to is mapped through a look up field. My goal is to create a trigger, which sums every opportunity's CS_Capacity_Allocated__C field (this is the individual's capacity reservation) that is assigned to the same project and create a total capacity value to be mapped into a newly created CS_Reserved_Capacity__C field (this is all the individual's capacity reservations added together). One could then view the total project capacity on each opportunity.

A Roll Up Summary would be ideal for this situation; however I cannot establish a Master-Detail relationship on the opportunity with the project object, nor can I establish a Roll Up Summary on the project because the "Summarized Object" drop-down menu does not allow me to acces the opportunities option.

This has left me, from what I believe, with the need to develop a trigger. I have authored the following code; however it has confronted me with several error messages, which I have been fixing along the way. It has just recently sent me the following error message

Error: Compile Error: Expression cannot be assigned at line -1 column -1

I cannot seem to work this one out. The code is as follows:

Trigger addReservedCapacity on opportunity (before insert, before update) {
    set <string> projectNames = new Set <string>( ); 
    List <decimal> projectAllocations = new List <decimal>( );
    decimal reservedCapacity = 0;
    integer i = 0;

    // instantiate the variables.
    for (opportunity opportunity: trigger.new) {

        If (opportunity.assigned_cs_project__c != null) {
            projectNames.add (opportunity.assigned_cs_project__c);}
        }

        // if the opportunity has an assigned project add the name of the project to a set.


        if (projectNames.size( ) > 0 ) {
            map <string, decimal> mapNames = new map <string, decimal> ( );

            for (opportunity obj : [SELECT Id, name, assigned_cs_project__c, cs_capacity_allocated__c FROM opportunity WHERE assigned_cs_project__c IN : projectNames] ) {
                mapNames.put(obj.assigned_cs_project__c,obj.cs_capacity_allocated__c);
            }

            // create a map and query the opportunity records looking for records that have matching project assignments to the created set. 
            // return the found record’s id, name, project assignment and reserved capacity
            // put the project assignment into the map as the key and the reserved capacity as the value.

            projectAllocations = mapNames.values();

            // set the projectAllocations list to the reserved capacity values in the map.

            for(i = 0; i <= mapNames.size(); i++){
                reservedCapacity = reservedCapacity  + projectAllocations.get(i);
            }

            // sum the lines of the list

            opportunity.cs_reserved_capacity__c = reservedCapacity;

            // set the CS Reserved Capacity field in the Projects object to the calculated reserved capacity.
        }
    }
}
1

If i understand your requirement properly, you want to get the sum of CS_Capacity_Allocated__C of all opportunities related to a project record.(the project of the opportunity in trigger.new). right ?

I guess the below trigger will do it, not tested though. Let me know if this helps.

Trigger addReservedCapacity on opportunity (before insert, before update) {

set <string> projectNames = new Set <string>( ); 
set<id> opId = new set<id>();
List <opportunity> projectAllocations = new List <opportunity>( );
decimal reservedCapacity = 0;
integer i = 0;
for (opportunity opportunity: trigger.new) {

    If (opportunity.assigned_cs_project__c != null) {
        projectNames.add (opportunity.assigned_cs_project__c);
    }
}

for(opportunity op : [select id,cs_reserved_capacity__c ,cs_capacity_allocated__c, assigned_cs_project__c from opportunity where assigned_cs_project__c IN :projectNames])
{
    opId.add(op.id);
}
decimal totalsum;
for(Aggregateresult ag : [select SUM(cs_capacity_allocated__c)sn from opportunity where id IN: opId]){
          totalsum = decimal.valueOf(ar.get('sn'));
}
for(opportunity o : [select id,cs_reserved_capacity__c ,cs_capacity_allocated__c, assigned_cs_project__c from opportunity where assigned_cs_project__c IN :projectNames]){
    o.cs_reserved_capacity__c  = totalsum;
    projectAllocations.add(o);
}

if(projectAllocations != null){
    update projectAllocations
}
  • Hi Ajay, I appreciate your help. I am walking through the code now. I am getting the following error on the following line. Error: Compile Error: Variable does not exist: decimal at line 21 column 22 totalsum = decimal.valueOf(ag.get('sn')); – Cole Swain Jun 22 '15 at 14:02
  • Remove line 19, and try using this instead line 21 : string totalsum = string.valueOf(ar.get('sn')); Also, the list "projectAllocations" in line 5 , make it of opportunity type. – Ajay Dubedi Jun 22 '15 at 14:12
  • Ajay, it just saved without error. I am going to to do a few test cases. Thank you. I do appreciate the help. I will post and mark this answer as best when I find out if it works as needed. Again, thank you. – Cole Swain Jun 22 '15 at 14:17
  • Does this mean anything to you? It just came up when i tried to edit an opportunity.... – Cole Swain Jun 22 '15 at 14:19
  • Error: Invalid Data. Review all error messages below to correct your data. Apex trigger addReservedCapacity caused an unexpected exception, contact your administrator: addReservedCapacity: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006g0000008WbrhAAC; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 006g0000008Wbrh) is currently in trigger addReservedCapacity, therefore it cannot recursively update itself: []: Trigger.addReservedCapacity: line 31, column 1 – Cole Swain Jun 22 '15 at 14:19
0

I think you cannot use opportunity and Opportunity (notice the capitalized O) because Apex is not case-sensitive. Have you tried naming your variables inside those for loops something else than 'opportunity'? Like 'opp'?

for (Opportunity opp : Trigger.new)
{
    ...
}

This naming might be causing your errors, as the compiler might be trying to reference the class instead of the object.

  • Hi Renato, doesn't seem to be the solution; but thank you for the help! – Cole Swain Jun 22 '15 at 14:01
  • Are you still getting a compilation error? Is it the same as before? – Renato Oliveira Jun 22 '15 at 14:07
  • Yes it is, or was, I am trying Ajay's (the above comment) new code currently – Cole Swain Jun 22 '15 at 14:22
0

Andrew Fawcett( salesforce MVP) has developed a Declarative Rollup Summary Tool which does Roll up summary calculations for look up relationship.

You can read about this in Salesforce Development libary through this link or by visiting his blog

Advantages of this approach is : it is more declarative in nature and you don't need to know much apex coding to use this, plus this is available via managed package.

I was also trying to find any limitations. Found rules to limit records for roll up calculations are still not available. Check comments in his blog to know more about that but he is defintely planning future enhancments on this.

Lastly a proper testing in sandbox is always recommended.

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.