You need to sign in to do that
Don't have an account?
How to write a Trigger Handler for this scenario
Hi I am Newbie to Development from Salesforce Admin .
I was able to write a trigger for a requirment to create Quote and Quoteline item from custom object named RPR , But i have this sutiation that i already have a trigger in same object which is causing my newly created trigger to get dectiavted .
As per research that i have done i got to know that it can be sloved by creating an apex trigger handler .
This is my newly created trigger .
trigger RPRTrigger on RPR__c (after insert)
{
List<Quote> quoteList = new List<Quote>();
List<QuoteLineItem> qliList = new List<QuoteLineItem>();
Pricebookentry pbe = [select Id,pricebook2Id,product2Id from PricebookEntry where product2id = '01t2s00000077OF'];
Id oppId = [select id from opportunity where id =: '006p000000Abxrb' LIMIT 1].Id;
if(trigger.isInsert && trigger.isAfter)
{
Set<Id> accId = new Set<Id>();
List<RPR__c> rprList = new List<RPR__c>();
for(RPR__c rpr : trigger.new)
{
rprList.add(rpr);
accId.add(rpr.Account__c);
}
List<Opportunity> oppList = [SELECT Id,Name FROM Opportunity
WHERE AccountId IN : accId];
System.debug('Account Set'+accId + ' Opportunity List '+ oppList);
for(RPR__c rprNew : rprList){
Quote q = new Quote();
q.Name = 'Teston New Quote'+rprNew.Name;
q.OpportunityId = oppList[0].Id;
q.Ship_To_City__c = rprNew.Shipment_From__c;
q.Pricebook2Id = pbe.pricebook2id;
quoteList.add(q);
}
if(!quoteList.isEmpty()){
insert quoteList;
}
for(Quote q : quoteList){
QuoteLineItem qli = new QuoteLineItem();
qli.QuoteId = q.Id;
qli.quantity = 2;
qli.pricebookentryId = pbe.id;
qli.unitprice = 10;
qli.product2Id = pbe.product2Id;
qli.Packing_Style__c = q.X1_Packing_Style__c;
qliList.add(qli);
}
if(!qliList.isEmpty()){
insert qliList;
}
}
}
This trigger is getting deactived if the below existing trigger fires .
trigger pullCommentsApproval on RPR__c (before update) {
Map<Id, RPR__c> oppMap = new Map<Id, RPR__c>([
Select (Select IsPending, ProcessInstanceId, TargetObjectId, StepStatus, OriginalActorId, ActorId,Actor.Name, RemindersSent, Comments, IsDeleted, CreatedDate, CreatedById, SystemModstamp
From ProcessSteps where StepStatus IN ('Approved','Rejected') ORDER BY CreatedDate DESC) From RPR__c WHERE Id IN : Trigger.new]);
for(RPR__c opp: Trigger.new) {
RPR__c opp1 = oppMap.get(opp.Id);
opp.Approver_Comments__c = '';
for (ProcessInstanceHistory processStep : opp1.ProcessSteps) {
opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only.
opp.Approver_Comments__c += 'Comments: ' + processStep.comments + ' . Status: ' + processStep.StepStatus + ' . Date: ' + processStep.CreatedDate +' . Commentor Name: ' + processStep.Actor.Name + '\\';
//opp.Approved_Date__c = DateTime.parse(system.now().format());
opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only, DO NOT DELETE, MAY EFFECT REPORT TYPE
}
opp.Approver_Comments3__c = 'aa';
}
}
what should i consider and how i can complete a trigger handler in order make my newly created trigger stable without getting deactivated .
Any suggestions will be a great help ! Thanks in advance :)
Please follow below code:-
pullCommentsApproval:-
TriggerHandler_Controller:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
All Answers
Please follow below code:-
pullCommentsApproval:-
TriggerHandler_Controller:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Hi Mukesh ,
Thank you so much !
I am trying to replicate this in my demo org before add to test envoriment , however there is error on line 1 .
Public class TriggerHandler_Controller(){ ---------- This line is having Error ----------- Below record id are demo org product and opportunity ID that i have added
public void afterInsert(List<RPR__c> objList){
List<Quote> quoteList = new List<Quote>();
List<QuoteLineItem> qliList = new List<QuoteLineItem>();
Pricebookentry pbe = [select Id,pricebook2Id,product2Id from PricebookEntry where product2id = '01t28000000cRQsAAM'];
Id oppId = [select id from opportunity where id =: '0060k00000CuKIw' LIMIT 1].Id;
Set<Id> accId = new Set<Id>();
List<RPR__c> rprList = new List<RPR__c>();
for(RPR__c rpr : objList)
{
rprList.add(rpr);
accId.add(rpr.Account__c);
}
List<Opportunity> oppList = [SELECT Id,Name FROM Opportunity
WHERE AccountId IN : accId];
System.debug('Account Set'+accId + ' Opportunity List '+ oppList);
for(RPR__c rprNew : rprList){
Quote q = new Quote();
q.Name = 'Teston New Quote'+rprNew.Name;
q.OpportunityId = oppList[0].Id;
q.Ship_To_City__c = rprNew.Shipment_From__c;
q.Pricebook2Id = pbe.pricebook2id;
quoteList.add(q);
}
if(!quoteList.isEmpty()){
insert quoteList;
}
for(Quote q : quoteList){
QuoteLineItem qli = new QuoteLineItem();
qli.QuoteId = q.Id;
qli.quantity = 2;
qli.pricebookentryId = pbe.id;
qli.unitprice = 10;
qli.product2Id = pbe.product2Id;
qli.Packing_Style__c = q.X1_Packing_Style__c;
qliList.add(qli);
}
if(!qliList.isEmpty()){
insert qliList;
}
}
public void beforeUpdate(List<RPR__c> objList){
Set<Id> RPR_Ids = new Set<Id>();
for(RPR__c obj : objList){
RPR_Ids.add(obj.Id);
}
Map<Id, RPR__c> oppMap = new Map<Id, RPR__c>([
Select (Select IsPending, ProcessInstanceId, TargetObjectId, StepStatus, OriginalActorId, ActorId,Actor.Name, RemindersSent, Comments, IsDeleted, CreatedDate, CreatedById, SystemModstamp
From ProcessSteps where StepStatus IN ('Approved','Rejected') ORDER BY CreatedDate DESC) From RPR__c WHERE Id IN : RPR_Ids]);
for(RPR__c opp: objList) {
RPR__c opp1 = oppMap.get(opp.Id);
opp.Approver_Comments__c = '';
for (ProcessInstanceHistory processStep : opp1.ProcessSteps) {
opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only.
opp.Approver_Comments__c += 'Comments: ' + processStep.comments + ' . Status: ' + processStep.StepStatus + ' . Date: ' + processStep.CreatedDate +' . Commentor Name: ' + processStep.Actor.Name + '\\';
//opp.Approved_Date__c = DateTime.parse(system.now().format());
opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only, DO NOT DELETE, MAY EFFECT REPORT TYPE
}
opp.Approver_Comments3__c = 'aa';
}
}
}
I was able to save the class now after removing the () in code .
Now i am on the Trigger part
If condition is having error for this
Use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
I was able to save both the code as expected but i got an Error while testing it .
Do i need to write a test class for this and the error i got is because of that ?
Hi Mukesh,
You helped a lot ! I am stuck with Test class for this In order to deploy this functionality it would be a great help to many new bie to salesforce development if you help us with test class for this can be deployed .
The test class should be written for Public class TriggerHandler_Controller ? correct to have code coverage ?
please help me with example for this in order to have code coverage for this