You need to sign in to do that
Don't have an account?
selva kumar 14
Trigger issue regarding pricebook
Hi Everyone,
I have a issue in my trigger functionality.
My Requirement is given below.
If a specific recordtype ("Bulk Record Type" ) opportunity is created,then it should select “k) Bulk PriceBook".
After Save,If i change Bulk to some other record type,then it should be changed to particular pricebook(Non Bulk Pricebook) .
As well as if other Recordtype to Bulk Record type it should change to "Bulk Pricebook" .
So i have created trigger regarding this requirement.
It is working for that,
1) Bulk Record Type Choosen --> Bulk Pricebook Assigned
2) Other Record Type to Bulk Change -->Bulk Pricebook Assigned
My only issue with this is,
If i Change Bulk Record type to Other Record Type Pricebook doesn't change.It remains in same Bulk Pricebook.
Is it cases related to where use insert,update in a if else statements?
Please any one help me in my coding to find out.
My trigger is given below.
trigger BulkPricebookOpp on Opportunity (before insert,before update)
{
if(trigger.isInsert || trigger.isUpdate) {
List<Pricebook2> prcbooklists = [select id,name from pricebook2];
Map<String,Id> prcbookMaps = new Map<String,Id>();
List<RecordType> recordtypId = [Select Id,Name From RecordType Where SobjectType = 'Opportunity'];
Map<Id,String> recMap = new Map<Id,String>();
Set<Opportunity> OppItrortunityUpdatesIds = new Set<Opportunity>();
if(!prcbooklists.isEmpty()){
for(Pricebook2 prcIter : prcbooklists){
prcbookMaps.put(prcIter.name,prcIter.id);
}
}
System.debug('==prcmap'+prcbookMaps);
for(RecordType rectyp : recordtypId){
recMap.put(rectyp.Id,rectyp.name);
}
for(Opportunity OppItrs : Trigger.new){
if(prcbookMaps.size() > 0) {
if (recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services'){
OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
}
}
else if(Trigger.isAfter)
{
if(Trigger.isUpdate)
{
List<Pricebook2> prcbooklist = [select id,name from pricebook2];
Map<String,Id> prcbookMap = new Map<String,Id>();
Map<Id,String> Accmap = new Map<Id,String>();
Set<Opportunity> OppItrortunityUpdateIds = new Set<Opportunity>();
Set<Id> accountIds = new Set<Id>();
if(!prcbooklist.isEmpty()){
for(Pricebook2 prcIter : prcbooklist){
prcbookMap.put(prcIter.name,prcIter.id);
}
}
System.debug('==prcmap'+prcbookMap);
for(Opportunity OppIdsItr : Trigger.new){
accountIds.add(OppIdsItr.accountid);
}
for(Account AccItr : [SELECT id,Region__c FROM Account WHERE Id IN:accountIds]){
Accmap.put(AccItr.id,AccItr.Region__c);
}
for(Opportunity OppItr : Trigger.new){
if(prcbookMap.size() > 0){
if (Accmap.get(OppItr.accountId) == 'Service 3'){
OppItr.Pricebook2Id = prcbookMap.get('Non Bulk Price Book - 3');
}
else if(Accmap.get(OppItr.accountId) == ' Service 2'){
OppItr.Pricebook2Id = prcbookMap.get('Non Bulk Price Book - 2');
}
}
}
}
}
}
}
}
Thanks in advance
I have a issue in my trigger functionality.
My Requirement is given below.
If a specific recordtype ("Bulk Record Type" ) opportunity is created,then it should select “k) Bulk PriceBook".
After Save,If i change Bulk to some other record type,then it should be changed to particular pricebook(Non Bulk Pricebook) .
As well as if other Recordtype to Bulk Record type it should change to "Bulk Pricebook" .
So i have created trigger regarding this requirement.
It is working for that,
1) Bulk Record Type Choosen --> Bulk Pricebook Assigned
2) Other Record Type to Bulk Change -->Bulk Pricebook Assigned
My only issue with this is,
If i Change Bulk Record type to Other Record Type Pricebook doesn't change.It remains in same Bulk Pricebook.
Is it cases related to where use insert,update in a if else statements?
Please any one help me in my coding to find out.
My trigger is given below.
trigger BulkPricebookOpp on Opportunity (before insert,before update)
{
if(trigger.isInsert || trigger.isUpdate) {
List<Pricebook2> prcbooklists = [select id,name from pricebook2];
Map<String,Id> prcbookMaps = new Map<String,Id>();
List<RecordType> recordtypId = [Select Id,Name From RecordType Where SobjectType = 'Opportunity'];
Map<Id,String> recMap = new Map<Id,String>();
Set<Opportunity> OppItrortunityUpdatesIds = new Set<Opportunity>();
if(!prcbooklists.isEmpty()){
for(Pricebook2 prcIter : prcbooklists){
prcbookMaps.put(prcIter.name,prcIter.id);
}
}
System.debug('==prcmap'+prcbookMaps);
for(RecordType rectyp : recordtypId){
recMap.put(rectyp.Id,rectyp.name);
}
for(Opportunity OppItrs : Trigger.new){
if(prcbookMaps.size() > 0) {
if (recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services'){
OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
}
}
else if(Trigger.isAfter)
{
if(Trigger.isUpdate)
{
List<Pricebook2> prcbooklist = [select id,name from pricebook2];
Map<String,Id> prcbookMap = new Map<String,Id>();
Map<Id,String> Accmap = new Map<Id,String>();
Set<Opportunity> OppItrortunityUpdateIds = new Set<Opportunity>();
Set<Id> accountIds = new Set<Id>();
if(!prcbooklist.isEmpty()){
for(Pricebook2 prcIter : prcbooklist){
prcbookMap.put(prcIter.name,prcIter.id);
}
}
System.debug('==prcmap'+prcbookMap);
for(Opportunity OppIdsItr : Trigger.new){
accountIds.add(OppIdsItr.accountid);
}
for(Account AccItr : [SELECT id,Region__c FROM Account WHERE Id IN:accountIds]){
Accmap.put(AccItr.id,AccItr.Region__c);
}
for(Opportunity OppItr : Trigger.new){
if(prcbookMap.size() > 0){
if (Accmap.get(OppItr.accountId) == 'Service 3'){
OppItr.Pricebook2Id = prcbookMap.get('Non Bulk Price Book - 3');
}
else if(Accmap.get(OppItr.accountId) == ' Service 2'){
OppItr.Pricebook2Id = prcbookMap.get('Non Bulk Price Book - 2');
}
}
}
}
}
}
}
}
Thanks in advance
Please use below mentioned code. Since you are performing updation on the same object so you should use before insert and before update instead.
trigger BulkPricebookOpp on Opportunity (before insert,before update)
{
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate))
{
List<Pricebook2> prcbooklists = [select id,name from pricebook2];
Map<String,Id> prcbookMaps = new Map<String,Id>();
List<RecordType> recordtypId = [Select Id,Name From RecordType Where SobjectType = 'Opportunity'];
Map<Id,String> recMap = new Map<Id,String>();
if(!prcbooklists.isEmpty())
{
for(Pricebook2 prcIter : prcbooklists)
{
prcbookMaps.put(prcIter.name,prcIter.id);
}
}
for(RecordType rectyp : recordtypId)
{
recMap.put(rectyp.Id,rectyp.name);
}
for(Opportunity OppItrs : Trigger.new)
{
if(prcbookMaps.size() > 0)
{
if(Trigger.isInsert)
{
if (recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services')
{
OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
}
}
else if(Trigger.isUpdate)
{
if(OppItrs.recordtypId!=Trigger.oldMap.get(OppItrs.Id).RecordTypeId)
{
if(recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services')
OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
else
OppItrs.Pricebook2Id = prcbookMaps.get('Non Bulk Pricebook');
}
}
}
}
}
}
All Answers
Please use below mentioned code. Since you are performing updation on the same object so you should use before insert and before update instead.
trigger BulkPricebookOpp on Opportunity (before insert,before update)
{
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate))
{
List<Pricebook2> prcbooklists = [select id,name from pricebook2];
Map<String,Id> prcbookMaps = new Map<String,Id>();
List<RecordType> recordtypId = [Select Id,Name From RecordType Where SobjectType = 'Opportunity'];
Map<Id,String> recMap = new Map<Id,String>();
if(!prcbooklists.isEmpty())
{
for(Pricebook2 prcIter : prcbooklists)
{
prcbookMaps.put(prcIter.name,prcIter.id);
}
}
for(RecordType rectyp : recordtypId)
{
recMap.put(rectyp.Id,rectyp.name);
}
for(Opportunity OppItrs : Trigger.new)
{
if(prcbookMaps.size() > 0)
{
if(Trigger.isInsert)
{
if (recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services')
{
OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
}
}
else if(Trigger.isUpdate)
{
if(OppItrs.recordtypId!=Trigger.oldMap.get(OppItrs.Id).RecordTypeId)
{
if(recMap.get(OppItrs.RecordTypeId) == 'k) Bulk Services')
OppItrs.Pricebook2Id = prcbookMaps.get('Bulk Price Book');
else
OppItrs.Pricebook2Id = prcbookMaps.get('Non Bulk Pricebook');
}
}
}
}
}
}
Thanks a lot. It is working fine.
Regards,
Selva
I faced one more issue in this.
In my sandbox,this trigger is working fine my above functionality issue.
When i deployed in production, am facing the same issue.
Is it happen? Since it is working in sandbox but not in production.
Do you know the reason? please help on this.
Thanks in advance.
Can you please make sure all the essential record types and price books exist in Production?
But it is working propely in sandbox.
My trigger is given below.
trigger BulkPricebookOpp on Opportunity (Before insert, Before update) {
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate))
{
List<Pricebook2> prcbooklist = [select id,name from pricebook2];
Map<String,Id> prcbookMap = new Map<String,Id>();
Map<Id,String> abbAccmap = new Map<Id,String>();
List<RecordType> recordtypId = [Select Id,Name From RecordType Where SobjectType = 'Opportunity'];
Map<Id,String> recMap = new Map<Id,String>();
Set<Opportunity> OppItrortunityUpdateIds = new Set<Opportunity>();
Set<Id> accountIds = new Set<Id>();
Set<Id> RectypIds = new Set<Id>();
if(!prcbooklist.isEmpty())
{
for(Pricebook2 prcIter : prcbooklist)
{
prcbookMap.put(prcIter.name,prcIter.id);
}
}
System.debug('==prcmap'+prcbookMap);
for(RecordType rectyp : recordtypId)
{
recMap.put(rectyp.Id,rectyp.name);
}
for(Opportunity OppIdsItr : Trigger.new)
{
accountIds.add(OppIdsItr.accountid);
}
for(Account AccItr : [SELECT id,ABB_Region__c FROM Account WHERE Id IN:accountIds])
{
abbAccmap.put(AccItr.id,AccItr.ABB_Region__c);
}
for(Opportunity OppItr : Trigger.new)
{
if(prcbookMap.size() > 0)
{
if (recMap.get(OppItr.RecordTypeId) == 'k) Bulk Services')
{
OppItr.Pricebook2Id = prcbookMap.get('Bulk Price Book');
}
else if(Trigger.isUpdate)
{
if(OppItr.RecordTypeId!=Trigger.oldMap.get(OppItr.Id).RecordTypeId)
{
if(recMap.get(OppItr.RecordTypeId) == 'k) Bulk Services')
OppItr.Pricebook2Id = prcbookMap.get('Bulk Price Book');
else if(abbAccmap.get(OppItr.accountId) == 'A - D2')
{
OppItr.Pricebook2Id = prcbookMap.get('A Price Book - D2');
}
else if(abbAccmap.get(OppItr.accountId) == 'W-D2'){
OppItr.Pricebook2Id=prcbookMap.get('W Price Book - D2');
}
else if(abbAccmap.get(OppItr.accountId) == 'W-D3'){
OppItr.Pricebook2Id=prcbookMap.get('W Price Book - D3');
}
}
System.debug('====assign'+OppItr.Pricebook2Id);
}
}
}
}
}
Can u look into it.
Now it is working as expected. Just i have created an other opportunity and followed as it is given result.
Thanks so much for your effort.