Trigger scenario asked by PWC || Salesforce Developer 3 YOE #salesforce #interview

Trigger scenario asked by PWC || Salesforce Developer 3 YOE #salesforce #interview

Salesforce & Interviews

4 месяца назад

3,133 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@deepbhattacharjee6563
@deepbhattacharjee6563 - 10.01.2025 07:09

I would like to create a record triggered flow on after insert and before deletion of subscriber record.

After Insert : After a subscriber record is created, i will query subscriber records for the associated account order by created date in descending order. If list size is 1, then it is the recent subscriber. If list size is greater than 1,then 0th index is recent and will update recent to false for 1st index subscriber.


Before Delete : Will do same query on subscriber record order by created date in descending order. Will take 1st index subscription and mark it to true.

Ответить
@SolitudeStar2226
@SolitudeStar2226 - 10.01.2025 08:01

I will go through Trigger.

Steps:-
1. Trigger on subscriber before insert and after delete
2. Set of related Accounts.
3. Map of accounts and list of subscriber by soql loop on subscriber order by creation.
4. Update list of subscriber checkbox false.
5. For delete operation, we can fetch soql limit one one creation order and mark checkbox true.

Please let me know if this solution is profitable or not. I will be grateful.

Ответить
@prakharanandkushwaha4506
@prakharanandkushwaha4506 - 11.01.2025 15:44

trigger subscriberTrigger on Subscriber__c(before insert, after delete) {

if(trigger.isBefore && trigger.isInsert) {
Set<Id> accountIdSet = new Set<Id>();
for(Subscriber__c subs : trigger.new)
{
if(subs.Account__c!=null)
{
accountIdSet.add(subs.Account__c);
}
subs.Recent_Subscriber__c = true;
}
if(!accountIdSet.isEmpty()) {
List<Subscriber__c> oldRecentSubsToUpdate = new List<Subscriber__c>();
for(Subscriber__c subs : [SELECT Id FROM Subscribers__r WHERE Recent_Subscriber__c=true AND
Account__c IN :accountIdSet])
{
subs.Recent_Subscriber__c = false;
oldRecentSubsToUpdate.add(subs);
}
if(!oldRecentSubsToUpdate.isEmpty())
{
update oldRecentSubsToUpdate;
}
}
}

if(trigger.isAfter && trigger.isDelete) {
Set<Id> accountIdSet = new Set<Id>();
for(Subscriber__c subs : trigger.old)
{
if(subs.Account__c!=null)
{
accountIdSet.add(subs.Account__c);
}
}
if(!accountIdSet.isEmpty()) {
List<Subscriber__c> oldRecentSubsToUpdate = new List<Subscriber__c>();
for(Account acct : [SELECT Id, (SELECT Id FROM Subscribers__r ORDER BY CreatedDate DESC LIMIT 1)
FROM Account WHERE Id IN :accountIdSet])
{
if(acct.Subscribers__r.size()>0) {
oldRecentSubsToUpdate.add(new Subscriber__c(Id=acct.Subscribers__r[0].Id, Recent_Subscriber__c=true));
}
}
if(!oldRecentSubsToUpdate.isEmpty())
{
update oldRecentSubsToUpdate;
}
}
}

}

Ответить