You need to sign in to do that
Don't have an account?
Jancy Mary
How to fetch the Parent's parent Id in Apex code.
Hi All,
Placement object has a lookup relationship to Account and Contact, I want to show the number of placements on each Account, however if multiple Placements are associated to the same Contact in different interval of time period which intern is associated to particular Account, then all those Placements which are associated to that Contact should be considered as 1 count under that particular Account.
I tried writing the below apex class, but stuck in the middle. With the below code I am able to fetch the ContactID and the AccountID when a placement record is inserted, however I am unable to compare the Contact's Account with the fetched AccountID. Or if there is any alternative way to achieve this please suggest.
public class totalOfPlacementsTriggerHelper{
public static void handleAfterInsert(List<rnm__Placement__c> placementlist)
{
List<Account> acc= new List<Account>();
Set<Id> accActiveId= new Set<Id>();
Set<Id> conId= new Set<Id>();
for(rnm__Placement__c p: placementlist)
{
if(p.Active__c==True)
{
if(p.rnm__Employer__c!=null && p.rnm__Contact__c!=null) //rnm__Employer__c is a lookup to Account
{
accActiveId.add(p.rnm__Employer__c);
conId.add(p.rnm__Contact__c);
system.debug('TESTING'+accActiveId+' '+conId);
}
}
}
}
Thanks in Advance,
Jancy Mary
Placement object has a lookup relationship to Account and Contact, I want to show the number of placements on each Account, however if multiple Placements are associated to the same Contact in different interval of time period which intern is associated to particular Account, then all those Placements which are associated to that Contact should be considered as 1 count under that particular Account.
I tried writing the below apex class, but stuck in the middle. With the below code I am able to fetch the ContactID and the AccountID when a placement record is inserted, however I am unable to compare the Contact's Account with the fetched AccountID. Or if there is any alternative way to achieve this please suggest.
public class totalOfPlacementsTriggerHelper{
public static void handleAfterInsert(List<rnm__Placement__c> placementlist)
{
List<Account> acc= new List<Account>();
Set<Id> accActiveId= new Set<Id>();
Set<Id> conId= new Set<Id>();
for(rnm__Placement__c p: placementlist)
{
if(p.Active__c==True)
{
if(p.rnm__Employer__c!=null && p.rnm__Contact__c!=null) //rnm__Employer__c is a lookup to Account
{
accActiveId.add(p.rnm__Employer__c);
conId.add(p.rnm__Contact__c);
system.debug('TESTING'+accActiveId+' '+conId);
}
}
}
}
Thanks in Advance,
Jancy Mary
Related object fields are not directly available in trigger. you will have to query them. e.g. if you want to fetch Contact's Account. You need to put all contact ids in set first, write a query against that set of contacts and fetch required fields. You can store this data in a map.
Now again you can iterate through placement list and do required comparison.
By having a junction object called Placement, technically the Account and Contact is now have capability for m:n relationship. So just want to clarify few scenarios on your business case.
Scenario 1:
Record 1 : Account1 is related to Contact1 in the year 2000
Record 2 : Account1 is related to Contact2 in the year 2000
So what is the count of Account1 should be in this case?
Scenario 2:
Record 1 : Account1 is related to Contact1 in the year 2000
Record 2 : Account1 is related to Contact1 in the year 2002
The count on Account1 should be 2 right?
Scenario 3:
Record 1 : Account1 is related to Contact1 in the year 2000
Record 2 : Account1 is related to Contact1 in the year 2002
Record 3 : Account1 is related to Contact2 in the year 2002
So what is the count of Account1 should be in this case?
Thanks for the reply, I would like my code to work in below scenarios:
Consider I have the below records.
Account Record: TestAccount1
Contact Record: TestContact1
TestContact2
Scenario 1:
1. If I insert a Placement1 record accoiating with TestAccount1 & TestContact1, the "Total Placements" on Account should show 1.
2. If I insert a Placement2 associating with TestAccount1 & TestContact2, the "Total Placements" on Account should now show 2.
(I want to know how many Contacts(Candidates) are been processed(Placed) for each Account, if I have for example 4 placements processed through TestContact1 for the same TestAccount1, now it should show the total placemets as 1 on Account, if I have another 2 placements processed for a different contact TestContact2 and for the same TestAccount1, now the total placemets on Account should show 2.
If in future I insert a new Placement with TestContact1 and TestAccount2, it should still show me the count as 2. But if I insert another placement with TestContact3 and TestAccount1 the total placemets on Account should show 3 as the contact is different this time.
I really appreciate your help, eager to resolve this with your guidance.
Thanks a lot,
Jancy Mary