You need to sign in to do that
Don't have an account?
Nick Speyer
Roll Up Summary Trigger Replacement
Hello,
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:
If anybody could help I would really appreciate it. Regardless, thank you for your time in reading this.
All the best.
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. } }
If anybody could help I would really appreciate it. Regardless, thank you for your time in reading this.
All the best.
1. You have Porject record which can be associated with many opportunity.
Project 1 =>
1. Opportunity 1 - Capacity 4 - reservedCapacity (4+4+5) = 13
2. Opportunity 2 - Capacity 4 - reservedCapacity (4+4+5) = 13
3. Opportunity 3 - Capacity 5 - reservedCapacity (4+4+5) = 13
if this is the case you have to right trigger on After event.
Yes, that looks like what I am seeking.
What is this that I have to write?
{
Set<String> setProject = new Set<String>();
Map<ID,List<Opportunity>> mapProVslstOpportunity = new Map<id,List<Opportunity>>();
List<Opportunity> lstOpportunity = new List<Opportunity>();
for(opportunity opportunity: trigger.new) {
if(opportunity.assigned_cs_project__c == null) continue;
// Collect ProjectId
setProject(opportunity.assigned_cs_project__c);
}
if (!setProject.isEmpty())
{
//Collect all Opportunities related with project
for (opportunity obj : [SELECT Id, name, assigned_cs_project__c, cs_capacity_allocated__c FROM opportunity WHERE assigned_cs_project__c IN : projectNames]){
if(mapProVslstOpportunity.ContainsKey(obj.assigned_cs_project__c)){
mapProVslstOpportunity.get(obj.assigned_cs_project__c).add(obj);
}else{
List<Opportunity> lstOpp = new List<Opportunity>();
lstOpp.add(obj);
mapProVslstOpportunity.put(obj.assigned_cs_project__c,lstOpp);
}
}
}
if(!mapProVslstOpportunity.isEmpty()){
for(Id ProjectId : mapProVslstOpportunity.Keyset()){
decimal reservedCapacity = 0;
List<Opportunity> lstOpp = mapProVslstOpportunity.get(ProjectId);
if(lstOpp.isEmpty()) continue;
for(Opportunity objOpp : lstOpp){
reservedCapacity = reservedCapacity + objOpp.cs_capacity_allocated__c;
}
// Create the List to update the Opportunities
for(Opportunity objOpp : lstOpp)
{
Opportunity objOpportunity = new Opportunity(Id = objOpp.Id, cs_reserved_capacity__c = reservedCapacity);
lstOpportunity.add(objOpportunity);
}
}
}
// Update all Opportunities related with Project
if(!lstOpportunity.isEmpty())
update lstOpportunity;
}
/*
Please check this code. Apologies for any syntax or Null pointer exception.
*/
I am getting the following error:
Error: Compile Error: Method does not exist or incorrect signature: setProject(Id) at line 11 column 9
Line 11:
// Collect ProjectId
setProject(opportunity.assigned_cs_project__c);
Also,
Shouldn't the following be a != instead of a ==?
for(opportunity opportunity: trigger.new) {
if(opportunity.assigned_cs_project__c == null) continue;
Thank you for the fix. To be honest, I am a bit lost in this code, as it differs quite substantially from the one I had written. Some of the mapping code is throwing me off. I understand you are likely busy, but if you could would you help with another error the code has returned:
Error: Compile Error: Invalid initial expression type for field Opportunity.CS_Reserved_Capacity__c, expecting: String at line 44 column 104
for(Opportunity objOpp : lstOpp)
{
Opportunity objOpportunity = new Opportunity(Id = objOpp.Id, cs_reserved_capacity__c = reservedCapacity);
lstOpportunity.add(objOpportunity);
}
}