+ Start a Discussion
Fiona HunterFiona Hunter 

Can someone help me with an Apex Code Amendment please?

I will start by saying I have no idea about Apex Code we have not long got salesforce and it was developed for our needs but as i come accross changes required I have found its within a large area of code so hoping someone can help me out.

Wondering if some kind soul can help me out as I am totally lost. I need to roll up information from a custom object to an opportunity. There are already fields that do this currently.

The structure we have is we create an opportunity but instead of adding multiple products it is multiple policies so we have a custom object called policy and dependant on a customers requirements there can be many policies for one opportnity so we currently have fields that calculate the gross premiums over all policies nad places this information onto the opportunity. I now need this to be done on another couple fields but this has been achieved through apex classes as there is not master detail relatonship between the objects.

Part of the Class which relates to what I am describing is below but where would I add additional fields to roll up the max days a policy is live ( there is already a field called days live)


//Roll up of Total Premium sold and average premium sold field
public static void onAfterDelete(List<Policy_Information__c> policyInfoList){
//unique opporutnity Object Ids
Set<Id> opportunityIdSet = new Set<Id>();
Set<Id> proposedProductIdSet = new Set<Id>();
Set<Id> proposalIdSet = new Set<Id>();
List<Proposal__c> proposalList= new List<Proposal__c>();
List<PreSales_Product__c> presalesProductList= new List<PreSales_Product__c>();
//hold list of Opporutnity Record to be update
List<Opportunity> opportunityListUpdatable = new List<Opportunity>();


for(Policy_Information__c policyObj : policyInfoList){
if(policyObj.Back_Office_Status__c=='On Risk'){
opportunityIdSet.add(policyObj.Opportunity__c);
}
}
Map<ID,Decimal> opportunityMap = new Map<ID,Decimal> ();
Map<ID,Decimal> opportunityMapCount = new Map<ID,Decimal> ();
for(AggregateResult q : [select Opportunity__c,sum(Premium__c),count(Id) exp1
from Policy_Information__c where Opportunity__c IN :opportunityIdSet group by Opportunity__c]){
OpportunityMap.put((Id)q.get('Opportunity__c'),(decimal)q.get('expr0'));
opportunityMapCount.put((Id)q.get('Opportunity__c'),(decimal)q.get('exp1'));

}


//Run the for loop on Opportunity using the non-duplicate set of Opportunities Ids
//Get the sum value from the map and create a list of Opportunities to update
for(Opportunity o : [Select Id, Total_Premiums_Sold_Policy__c,Total_Product_Sold_Policy__c,Number_of_Premiums_Sold_Policy__c from Opportunity where Id IN :opportunityIdSet]){
Decimal PremiumSum = OpportunityMap.get(o.Id);
Decimal PremiumCount= opportunityMapCount.get(o.Id);
o.Number_of_Premiums_Sold_Policy__c=PremiumCount;
o.Total_Premiums_Sold_Policy__c = PremiumSum;
o.Total_Product_Sold_Policy__c=PremiumCount;
opportunityListUpdatable.add(o);
}
try{
update opportunityListUpdatable;
}catch(Exception ex) { ex.getMessage(); }
}

}
 
John PipkinJohn Pipkin
Fiona, 

I'm pretty sure I understood your question, but I haven't had my morning coffee yet so forgive me if I misread:
 
//Roll up of Total Premium sold and average premium sold field
public static void onAfterDelete(List<Policy_Information__c> policyInfoList){
	//unique opporutnity Object Ids
	Set<Id> opportunityIdSet = new Set<Id>();
	Set<Id> proposedProductIdSet = new Set<Id>();
	Set<Id> proposalIdSet = new Set<Id>();
	List<Proposal__c> proposalList= new List<Proposal__c>();
	List<PreSales_Product__c> presalesProductList= new List<PreSales_Product__c>();
	//hold list of Opporutnity Record to be update
	List<Opportunity> opportunityListUpdatable = new List<Opportunity>();


	for(Policy_Information__c policyObj : policyInfoList){
		if(policyObj.Back_Office_Status__c=='On Risk'){
			opportunityIdSet.add(policyObj.Opportunity__c);
		}
	}
	Map<ID,Decimal> opportunityMap = new Map<ID,Decimal> ();
	Map<ID,Decimal> opportunityMapCount = new Map<ID,Decimal> ();
	Map<ID,Integer> opportunityMaxDays = new Map<ID,Integer>();
	for(AggregateResult q : [select Opportunity__c,sum(Premium__c),count(Id) exp1, max(days_live__c) exp2
			from Policy_Information__c where Opportunity__c IN :opportunityIdSet group by Opportunity__c]){
		OpportunityMap.put((Id)q.get('Opportunity__c'),(decimal)q.get('expr0'));
		opportunityMapCount.put((Id)q.get('Opportunity__c'),(decimal)q.get('exp1'));
		opportunityMaxDays.put((Id)q.get('Opportunity__c'),(integer)q.get('exp2'))
	}


	//Run the for loop on Opportunity using the non-duplicate set of Opportunities Ids
	//Get the sum value from the map and create a list of Opportunities to update
	for(Opportunity o : [Select Id, Total_Premiums_Sold_Policy__c,Total_Product_Sold_Policy__c,Number_of_Premiums_Sold_Policy__c from Opportunity where Id IN :opportunityIdSet]){
		Decimal PremiumSum = OpportunityMap.get(o.Id);
		Decimal PremiumCount= opportunityMapCount.get(o.Id);
		Integer daysLive = opportunityMaxDays.get(o.Id);
		o.Number_of_Premiums_Sold_Policy__c=PremiumCount;
		o.Total_Premiums_Sold_Policy__c = PremiumSum;
		o.Total_Product_Sold_Policy__c=PremiumCount;
		o.days_live__c = daysLive;
		opportunityListUpdatable.add(o);
	}
	try{
		update opportunityListUpdatable;
	}catch(Exception ex){ 
		ex.getMessage(); 
	}
}
i added lines 20, 21(edit), 25, 31(edit), 34, and 38

Hope that helps
Fiona HunterFiona Hunter
Thanks John :-) will give this a try and see how I get on.

Can you confirm for me its only at line 21 and 38 that I need to change to field name i have in salesforce Live_Days__c

Thanks
Fioma
John PipkinJohn Pipkin
Yes, you'll need to change that to reflect the correct field API name. Good luck!
Fiona HunterFiona Hunter
Hi John

Just getting round to trying this and i am getting the error
Can't alter metadata in an active org
Can you help please.
Thanks
John PipkinJohn Pipkin
Fiona,

You cannot alter apex triggers or classes in a production org. You'll have to make the changes in a sandbox and deploy to production after testing.
Fiona HunterFiona Hunter
Thanks for all your help will amend this in the sandbox and see how I get on :-)
Fiona HunterFiona Hunter
Sorry John being a pest now  i am getting an error 

Error: Compile Error: unexpected token: for at line 188 column 2

line 188 is shown below no idea what this means?

for(AggregateResult q : [select Opportunity__c,sum(Premium__c) ,count(Id) exp1
John PipkinJohn Pipkin
That's strange. I don't see any errors. Try this edit:
//Roll up of Total Premium sold and average premium sold field
public static void onAfterDelete(List<Policy_Information__c> policyInfoList){
	//unique opporutnity Object Ids
	Set<Id> opportunityIdSet = new Set<Id>();
	Set<Id> proposedProductIdSet = new Set<Id>();
	Set<Id> proposalIdSet = new Set<Id>();
	List<Proposal__c> proposalList= new List<Proposal__c>();
	List<PreSales_Product__c> presalesProductList= new List<PreSales_Product__c>();
	//hold list of Opporutnity Record to be update
	List<Opportunity> opportunityListUpdatable = new List<Opportunity>();


	for(Policy_Information__c policyObj : policyInfoList){
		if(policyObj.Back_Office_Status__c=='On Risk'){
			opportunityIdSet.add(policyObj.Opportunity__c);
		}
	}
	Map<ID,Decimal> opportunityMap = new Map<ID,Decimal> ();
	Map<ID,Decimal> opportunityMapCount = new Map<ID,Decimal> ();
	Map<ID,Integer> opportunityMaxDays = new Map<ID,Integer>();
	AggregateResult[] agResult = [select Opportunity__c,sum(Premium__c),count(Id) exp1, max(days_live__c) exp2
			from Policy_Information__c where Opportunity__c IN :opportunityIdSet group by Opportunity__c];
	for(AggregateResult q : agResult) {
		OpportunityMap.put((Id)q.get('Opportunity__c'),(decimal)q.get('expr0'));
		opportunityMapCount.put((Id)q.get('Opportunity__c'),(decimal)q.get('exp1'));
		opportunityMaxDays.put((Id)q.get('Opportunity__c'),(integer)q.get('exp2'))
	}


	//Run the for loop on Opportunity using the non-duplicate set of Opportunities Ids
	//Get the sum value from the map and create a list of Opportunities to update
	for(Opportunity o : [Select Id, Total_Premiums_Sold_Policy__c,Total_Product_Sold_Policy__c,Number_of_Premiums_Sold_Policy__c from Opportunity where Id IN :opportunityIdSet]){
		Decimal PremiumSum = OpportunityMap.get(o.Id);
		Decimal PremiumCount= opportunityMapCount.get(o.Id);
		Integer daysLive = opportunityMaxDays.get(o.Id);
		o.Number_of_Premiums_Sold_Policy__c=PremiumCount;
		o.Total_Premiums_Sold_Policy__c = PremiumSum;
		o.Total_Product_Sold_Policy__c=PremiumCount;
		o.days_live__c = daysLive;
		opportunityListUpdatable.add(o);
	}
	try{
		update opportunityListUpdatable;
	}catch(Exception ex){ 
		ex.getMessage(); 
	}
}