You need to sign in to do that
Don't have an account?
Sourav P
Autopopulate summation of fields from a lookup object on a Main object field
Hi All, any one plz help me out on this,
I have a custom object called " Quotation", with a field called " Total Premium". The object has a look up relationship with another custom object " Cover". In the cover object i have field called " Gross Premium". I need that all the Cover object records addded to the Quotation object, The gross premium from those will sum up and autopopulate in teh Total premium field. Deleting or adding the records, should affect the Total premium field value.
I have written the below trigger, but its showing the error as,
Error: Compile Error: unexpected token: trigger trgSummarizeCover at line 1 column 0
Can anyone plz figure it out whats wrong in my code and rectify it. Thanks
I have a custom object called " Quotation", with a field called " Total Premium". The object has a look up relationship with another custom object " Cover". In the cover object i have field called " Gross Premium". I need that all the Cover object records addded to the Quotation object, The gross premium from those will sum up and autopopulate in teh Total premium field. Deleting or adding the records, should affect the Total premium field value.
I have written the below trigger, but its showing the error as,
Error: Compile Error: unexpected token: trigger trgSummarizeCover at line 1 column 0
Can anyone plz figure it out whats wrong in my code and rectify it. Thanks
trigger trgSummarizeCover on Cover__c (after delete, after insert, after update) { //Limit the size of list by using Sets which do not contain duplicate elements set<Id> QuotationIds = new set<Id>(); //When adding new CustomObject or updating existing CustomObject if(trigger.isInsert || trigger.isUpdate){ for(Cover__c p : trigger.new){ QuotationIds.add(p.Quotation__c); } } //When deleting CustomObject if(trigger.isDelete){ for(Cover__c p : trigger.old){ QuotationsIds.add(p.Quotation__c); } } //Map will contain one Quotation Id to one sum value map<Id,Double> QuotationMap = new map <Id,Double>(); //Produce a sum of Cover__c and add them to the map //use group by to have a single Quotation Id with a single sum value for(AggregateResult q : [select Id,sum(Gross_premium_policy__c) from Cover__c where Quotation__c IN :QuotationIds group by Quotation__c]){ QuotationMap.put((Id) q.get('Quotation__c'),(Double)q.get('expr0')); } List<Quotation__c> QuotationsToUpdate = new List<Quotation__c>(); //Run the for loop on Quotation using the non-duplicate set of Quotation Ids //Get the sum value from the map and create a list of Quotations to update for(Quotation__c o : [Select Id, Total_Premium__c from Quotation__c where Id IN :QuotationIds]){ Double Cover__c sum = QuotationMap.get(o.Id); o.Total_Premium__c = Cover__c sum; QuotationsToUpdate.add(o); } update QuotationsToUpdate; }
All Answers
https://developer.salesforce.com/page/Declarative_Rollup_Summary_Tool_for_Force.com_Lookup_Relationships
I have done the insert and update portion try it and let me know if you can complete the delete portion also,(please note that i have used the child relationship name as 'pobs__r' (line 17 and 19)change it to your name Also i assume your lookup field name is 'Quotation__c')
Please close the thread as solved if it helped you.
Regards
Also change the Cover__c in line 19 and 27 to Gross_premium_policy__c
Parent is : Quotation__c ( field here where sum should reflect is " Total premium"
Child is : Cover__c ( field to be summed up " Policy Gross premium"
Thanks, Let me try your suggestion.
Error: Compile Error: unexpected token: 'SELECT' at line 16 column 50" is till keep on showing.
replace line 16 with this
Regards
Error: Compile Error: unexpected token: ) at line 16 column 98
Sorry, i purposefully removed that comma, to compensate an error.
Ya, Childe relationship name is ok,
Error: Compile Error: Invalid field Quotation__c for SObject Cover__c at line 5 column 27
Quotation__c is the object, it should come here ?
Do you mean ,
"Gross_premium_policy__c", this fields are going to sum up and fill the " Total premium" in Quotation. But if i am giving this field , its showing error as " Error: Compile Error: Incompatible element type Decimal for collection of Id at line 5 column 9"
Or, Do you mean, Quotation Number Quotation_Number__c Lookup(Quotation)
which is a look up field in the Cover object. If i put this one, then its showing me no more errors, But the trigger is not working. Its not giving a cover summations in the " Total premium" field.
Sorry, but where i was not giving the update opton in IsInsert part ? , I can see that the above code is same to my previous one. May i know plz where you changed the update option ?. But i tried the above code, No error but till its not giving the summation. I replace the above 19 & 33 with
as per your earlier suggestion, So my final code is as per below now,
Excellent, It starts working fine now, thank you so much for your support.
https://appexchange.salesforce.com/listingDetail?listingId=a0N30000009i3UpEAI