function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siddharth LakhotiaSiddharth 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





 
Best Answer chosen by Siddharth Lakhotia
Bhargavi TunuguntlaBhargavi Tunuguntla
Actually we there is change in ActSales in Inquiry then it should reflect the sum value in InquiryDetails,So I felt it would be better to write a trigger on Inquiry.

If you think you can run the trigger when there are chnages in InquiryDetails then you can use the below code:
 
trigger InquiryDetailsTrigger on InquiryDetails__c  (before insert,before update,after insert,after update) {
    
    Map<Id,Integer> InquiryCushionNum=new Map<Id,Integer>();
    if(trigger.isAfter)
    {
        if(trigger.isInsert || trigger.isUpdate)
        {
            for(InquiryDetails__c i:trigger.new)
            {
                InquiryCushionNum.put(i.id,i.blanketNumber__c);
            }
            
            List<Inquiry__c> RelatedInquiry =new List<Inquiry__c>([Select id,CushionNumber__c,ActSales__c from Inquiry where CushionNumber__c in: InquiryCushionNum.values()]);
            Map<Integer,Integer> CushionSales=new Map<Integer,Integer>();
            for(Inquiry__c i:RelatedInquiry)
            {
                if(CushionSales.containsKey(i.CushionNumber__c))
                {
                    CushionSales.put(i.CushionNumber__c,CushionSales.get(i.CushionNumber__c)+i.ActSales__c);
                }
                else
                {
                    CushionSales.put(i.CushionNumber__c,i.ActSales__c);
                }
            }
            for(InquiryDetails__c i:trigger.new)
            {
                i.ActualSales__c=CushionSales.get(InquiryCushionNum.get(i.id));
            }
            
        }
    }
}

 

All Answers

Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Siddharth,

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:
 
trigger InquiryTrigger on InquiryDetails__c  (before insert,before update,after insert,after update) {
    
    Map<Id,Integer> InquiryCushionNum=new Map<Id,Integer>();
    if(trigger.isAfter)
    {
        if(trigger.isInsert || trigger.isUpdate)
        {
            for(InquiryDetails__c i:trigger.new)
            {
                InquiryCushionNum.put(i.id,i.InquiryId__r.CushionNumber__c);
            }
            
            List<Inquiry__c> RelatedInquiry =new List<Inquiry__c>([Select id,CushionNumber__c,ActSales__c from Inquiry where CushionNumber__c in: InquiryCushionNum.values()]);
            Map<Integer,Integer> CushionSales=new Map<Integer,Integer>();
            for(Inquiry__c i:RelatedInquiry)
            {
                if(CushionSales.containsKey(i.CushionNumber__c))
                {
                    CushionSales.add(i.CushionNumber__c,CushionSales.get(i.CushionNumber__c)+i.ActSales__c);
                }
                else
                {
                    CushionSales.add(i.CushionNumber__c,i.ActSales__c);
                }
            }
            for(InquiryDetails__c i:trigger.new)
            {
                i.blanketNumber__c=CushionSales.get(InquiryCushionNum.get(i.id));
            }
            
        }
    }
}

Thanks
Hope this will be helpful.
​​​​​​​
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

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
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

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... 
 
Bhargavi TunuguntlaBhargavi Tunuguntla
blanketNumber__c is the field where we need to keep the sum of ActualSales right? 
Siddharth LakhotiaSiddharth Lakhotia
No there is a separate field. Actual sales. Also the logic is getting at line 10. I need to clarify something , I need values against cushion no. So if cushion number 123 has 3 different values. I need sum of those values and store it in actual sales present on inquiry details
Bhargavi TunuguntlaBhargavi Tunuguntla
then what is blanketNumber__c there?
Siddharth LakhotiaSiddharth Lakhotia
I am sorry blanket number and cushion number is same.. The real idea is that for a cushion no. There are different values of ActSales present. I want the summation of those values in a field (Actual sales)
Bhargavi TunuguntlaBhargavi Tunuguntla
Do we need to give the value to blanketNumber__c  in this logic

or

write the logic based on blanketNumber__c ?
Siddharth LakhotiaSiddharth Lakhotia
No. We don't need to.. we need to store the summation in actual sales field present on inquiry detail
Bhargavi TunuguntlaBhargavi Tunuguntla
ok . But last one clarification !!! this would be better if we write trigger on Inquiry right?
Siddharth LakhotiaSiddharth Lakhotia
How ?? I mean how does that help, when I need sum to be contacted on the inquiry details. Also , multiple inquiries are received in a day.. so we tend to delete inquiries every 3 months.
Bhargavi TunuguntlaBhargavi Tunuguntla
Actually we there is change in ActSales in Inquiry then it should reflect the sum value in InquiryDetails,So I felt it would be better to write a trigger on Inquiry.

If you think you can run the trigger when there are chnages in InquiryDetails then you can use the below code:
 
trigger InquiryDetailsTrigger on InquiryDetails__c  (before insert,before update,after insert,after update) {
    
    Map<Id,Integer> InquiryCushionNum=new Map<Id,Integer>();
    if(trigger.isAfter)
    {
        if(trigger.isInsert || trigger.isUpdate)
        {
            for(InquiryDetails__c i:trigger.new)
            {
                InquiryCushionNum.put(i.id,i.blanketNumber__c);
            }
            
            List<Inquiry__c> RelatedInquiry =new List<Inquiry__c>([Select id,CushionNumber__c,ActSales__c from Inquiry where CushionNumber__c in: InquiryCushionNum.values()]);
            Map<Integer,Integer> CushionSales=new Map<Integer,Integer>();
            for(Inquiry__c i:RelatedInquiry)
            {
                if(CushionSales.containsKey(i.CushionNumber__c))
                {
                    CushionSales.put(i.CushionNumber__c,CushionSales.get(i.CushionNumber__c)+i.ActSales__c);
                }
                else
                {
                    CushionSales.put(i.CushionNumber__c,i.ActSales__c);
                }
            }
            for(InquiryDetails__c i:trigger.new)
            {
                i.ActualSales__c=CushionSales.get(InquiryCushionNum.get(i.id));
            }
            
        }
    }
}

 
This was selected as the best answer
Siddharth LakhotiaSiddharth Lakhotia
why a blanket number in line 10, if you can explain
Bhargavi TunuguntlaBhargavi Tunuguntla
Because blanket number is like a reference at present . Cushionnumber in Inquiry ---- blanket Number in InquiryDetails . If both are same then the ActSales should be added to ActiualSales field in InquiryDetails.
Siddharth LakhotiaSiddharth Lakhotia
i dont have any field called blanket number on Inquiry details.  Can you please help me in understnaing how does this trigger work , and will it calculate the Act Sales present against any paticular blanket number and store in Actual Sales on Inquiry details
Siddharth LakhotiaSiddharth Lakhotia
hi,

I can find the following error , while trying to run trigger

execution of AfterInsert caused by: System.FinalException: Record is read-only
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

Thanks for your help. Unfortunately value is not getting stored in Actual Sales field.  Can you please help me on it.
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

Trigger is not working. Can you please help me to understand the logic behind last line
Bhargavi TunuguntlaBhargavi Tunuguntla
----i.ActualSales__c=CushionSales.get(InquiryCushionNum.get(i.id));

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.

 
trigger InquiryDetailsTrigger on InquiryDetails__c  (before insert,before update,after insert,after update) {
    
    Map<Id,Integer> InquiryCushionNum=new Map<Id,Integer>();
    if(trigger.isAfter)
    {
        if(trigger.isInsert || trigger.isUpdate)
        {
            for(InquiryDetails__c i:trigger.new)
            {
			//getting all the id's and it's blanketNumber__c
                InquiryCushionNum.put(i.id,i.blanketNumber__c);
            }
            //Fetching all the Inquires with the above feteched blanketNumbers
            List<Inquiry__c> RelatedInquiry =new List<Inquiry__c>([Select id,CushionNumber__c,ActSales__c from Inquiry where CushionNumber__c in: InquiryCushionNum.values()]);
            Map<Integer,Integer> CushionSales=new Map<Integer,Integer>();
            //Consider the sum of ActSales based on CushionNumber__c
			for(Inquiry__c i:RelatedInquiry)
            {
                if(CushionSales.containsKey(i.CushionNumber__c))
                {
                    CushionSales.add(i.CushionNumber__c,CushionSales.get(i.CushionNumber__c)+i.ActSales__c);
                }
                else
                {
                    CushionSales.add(i.CushionNumber__c,i.ActSales__c);
                }
            }
			//Fetching the sum calculated above by Comparing the blanketNumber with the CushionNumber__c
            for(InquiryDetails__c i:trigger.new)
            {
                i.ActualSales__c=CushionSales.get(InquiryCushionNum.get(i.id));
            }
            bbb
        }
    }
}

 
Siddharth LakhotiaSiddharth Lakhotia
So should i creatre a new field called blanket number?

Since i do not have any such field?
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavim

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
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

Can you please reply
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Siddharth,

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
Siddharth LakhotiaSiddharth Lakhotia
Hi, I creatred a field called blanket no. and updated via workflow. i didnt got any results in Actual Sales field. Also can you let me understand how does the last line provides the summed up value to the Actual Sales field.
Siddharth LakhotiaSiddharth Lakhotia
Hello Bhargavi, Sorry to trouble you, but the code doesn't work. Can you please help me identify , where it could have gone wrong On Fri, Oct 26, 2018, 5:14 PM siddharth lakhotia
Siddharth LakhotiaSiddharth Lakhotia
Hello Bhargavi, Sorry to trouble you, I have applied both techquies, nothing seems to be working. Can you please comment
Bhargavi TunuguntlaBhargavi Tunuguntla
Can you please post me your code once .
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

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}
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

Your take on it?
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

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??
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

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??
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

The trigger works now.. Can you help me in making test class for it
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Siddharth,

Happy to listen taht it is working.Please try the below code for your test class:
 
@isTest
private class test_InquiryDeatilsTrigger {
  
    @isTest static void test_method_InquiryDetails() {
	
	//required fields
	List<Inquiry__c> inqList=new List<Inquiry__c>();
	for(integer i=0;i<5;i++)
	{
	Inquiry__c inq=new Inquiry__c(CushionNumber__c='123',ActSales__c=5000+i);
	inqList.add(inq);
	
	}
	insert inqList;
	//requried fields
	Inquiry_Details__c in=new Inquiry_Details__c(name='',blanketNumber__c=inqList[0].CushionNumber__c);
	test.startTest();
	insert in;
	test.stopTest();
	
	
	
	
	}
	
	}

Can I know how is it working now? (before you had small issues right?)
Siddharth LakhotiaSiddharth Lakhotia
Basically , it still has a small issue.  when the same cushion no. is selected twice.. its adding value 2 times.. which should not be the case..

Because cushion no. value is already taken once in account 
Siddharth LakhotiaSiddharth Lakhotia
Hi Bhargavi,

Just a small help, Account is a mandatory field on Inquiry.. How do I include that ?
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Siddharth,

Find the below code:
 
@isTest
private class test_InquiryDeatilsTrigger {
  
    @isTest static void test_method_InquiryDetails() {
	
	Account acc=new account(Name='test');
	insert acc;
	//required fields
	List<Inquiry__c> inqList=new List<Inquiry__c>();
	for(integer i=0;i<5;i++)
	{
	Inquiry__c inq=new Inquiry__c(Account__c=acc.id,CushionNumber__c='123',ActSales__c=5000+i);
	inqList.add(inq);
	
	}
	insert inqList;
	//requried fields
	Inquiry_Details__c in=new Inquiry_Details__c(name='',blanketNumber__c=inqList[0].CushionNumber__c);
	test.startTest();
	insert in;
	test.stopTest();
	
	
	
	
	}
	
	}

Thanks.
Siddharth LakhotiaSiddharth Lakhotia
Hi bhargavi, I tried my luck before your solution. And it worked out.. Thanks a lot for your unconditional help.
Siddharth LakhotiaSiddharth Lakhotia
I have some changes in trigger, Do you require me to start a new thread or can I ask on the same one.

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