You need to sign in to do that
Don't have an account?
Siddharth Lakhotia
Need Help in writting Trigger
Hi ,
I have 2 objects Inquiry and Inquiry Details.
On Inquiry I capture , 2 fields : Cushion No. and Act. Sales
Irrepsective of inquiry recrords, Cushion no. can be same . It acts like a code , and can have different Act. Sales against every inquiry.
so lets say
For Inquiry 1 , Cushion no : 123 , ActSales : 100
For Inquiry 2 , Cushion no : 123 , ActSales : 200
For Inquiry 3 , Cushion no : 123 , ActSales : 300
For Inquiry 4 , Cushion no : 456 , ActSales : 350
For Inquiry 5 , Cushion no : 456 , ActSales : 450
I have a lookup at inquiry details that looks up to the Inquiry object,
I need to pull in the blanket no. against the inquiry and sum of Act Sales against blanket number(Cushion No ; 123 Actual Sales :600, Cushion Number : 456 , Actual Sales : 800).
How can I achieve this logic via trigger.
I know the soql queries that can work for this.. But somehow having problem in cracking the logic for this one
I have 2 objects Inquiry and Inquiry Details.
On Inquiry I capture , 2 fields : Cushion No. and Act. Sales
Irrepsective of inquiry recrords, Cushion no. can be same . It acts like a code , and can have different Act. Sales against every inquiry.
so lets say
For Inquiry 1 , Cushion no : 123 , ActSales : 100
For Inquiry 2 , Cushion no : 123 , ActSales : 200
For Inquiry 3 , Cushion no : 123 , ActSales : 300
For Inquiry 4 , Cushion no : 456 , ActSales : 350
For Inquiry 5 , Cushion no : 456 , ActSales : 450
I have a lookup at inquiry details that looks up to the Inquiry object,
I need to pull in the blanket no. against the inquiry and sum of Act Sales against blanket number(Cushion No ; 123 Actual Sales :600, Cushion Number : 456 , Actual Sales : 800).
How can I achieve this logic via trigger.
I know the soql queries that can work for this.. But somehow having problem in cracking the logic for this one
If you think you can run the trigger when there are chnages in InquiryDetails then you can use the below code:
All Answers
As per your requirement, based on the lookup field's Cushion Number ,the sum must be retrieved.If that's the case then here is the below code taht you can use to get the sum of values in InquiryDetails:
Thanks
Hope this will be helpful.
thanks for the code. However I am facing error in the 10th line. related to 'InquiryCushionNum.put(i.id,i.InquiryId__r.CushionNumber__c)';
Also, I want the sum of all the Act Sales against one comman cushion no. does this code achieve the same
Can you please help in resolving my doubt.
Also , i.blanketNumber__c=CushionSales.get(InquiryCushionNum.get(i.id)) in last line , Actually the blanket number is same as cushion number...
or
write the logic based on blanketNumber__c ?
If you think you can run the trigger when there are chnages in InquiryDetails then you can use the below code:
I can find the following error , while trying to run trigger
execution of AfterInsert caused by: System.FinalException: Record is read-only
Thanks for your help. Unfortunately value is not getting stored in Actual Sales field. Can you please help me on it.
Trigger is not working. Can you please help me to understand the logic behind last line
Here the Actual sales should be the sum of ActSales which has same CushionNumber.
Firstly I have taken a Map(CushionSales) with --- InquiryId and BlanketNumber
In the Second stage -- Another Map(InquiryCushionNum) with CushionNumber and sum of ActSales
In the last Stage, Comparing the blanketNumber with CushionNumber --- I am giving the sum of ActSales to the ActualSales__c.
Since i do not have any such field?
Thanks for your help throughoiut. I just want to inform you there is no field like blanket number on any of the objects, Do you require me to create a field like that ? If yes, on which object.
I just want the sum of ActSales against Cushion No. to get stored in a child object field Actual Sales
Can you please reply
Okay let's query the lookup field Cushion No. and then get the all the records which have that Cushion No. .. and perform the above logic without any blanket Number field.
or
You can even create a field blanket NUmber which will be a formulae field on InquiryDetails object to get Cushion No. of the related Inquiry record.Any of the technique works for us.
Thanks
please find the trigger... I am updating blanket number of Invoice details via workflow rule
trigger InquiryDetailsTrigger on InquiryDetails__c (before insert,before update,after insert,after update) {
02
03 Map<Id,String> InquiryCushionNum=new Map<Id,String>();
04 if(trigger.isAfter)
05 {
06 if(trigger.isInsert || trigger.isUpdate)
07 {
08 for(InquiryDetails__c i:trigger.new)
09 {
10 InquiryCushionNum.put(i.id,i.blanketNumber__c);
11 }
12
13 List<Inquiry__c> RelatedInquiry =new List<Inquiry__c>([Selectid,CushionNumber__c,ActSales__c from Inquiry where CushionNumber__c in: InquiryCushionNum.values()]);
14 Map<String,Double> CushionSales=new Map<String,Double>();
15 for(Inquiry__c i:RelatedInquiry)
16 {
17 if(CushionSales.containsKey(i.CushionNumber__c))
18 {
19 CushionSales.put(i.CushionNumber__c,CushionSales.get(i.CushionNumber__c)+i.ActSales__c);
20 }
21 else
22 {
23 CushionSales.put(i.CushionNumber__c,i.ActSales__c);
24 }
25 }
26 for(InquiryDetails__c i:trigger.new)
27 {
28 i.ActualSales__c=CushionSales.get(InquiryCushionNum.get(i.id));
29 }
30
31 }
32 }
33}
Your take on it?
The Actual Sales field is having a value now.. but its not fetching the summation of Act Sales against cushion no.
Is it something related to bulkification of trigger??
The Actual Sales field is having a value now.. but its not fetching the summation of Act Sales against cushion no.
Is it something related to bulkification of trigger??
The trigger works now.. Can you help me in making test class for it
Happy to listen taht it is working.Please try the below code for your test class:
Can I know how is it working now? (before you had small issues right?)
Because cushion no. value is already taken once in account
Just a small help, Account is a mandatory field on Inquiry.. How do I include that ?
Find the below code:
Thanks.
Basically , what I require is , once a cushion no. is selected, it should not be selected again, because it will result in dubious calculation
Or , whenever a new entry with that cushion no. is made, the old entry corresponding to that cushion no. gets deleted. The second one makes more sense to me