Triggers 在特定帐户记录类型上触发
所以我写了一个触发器,它工作得非常好,完全符合我的要求,但问题是它对所有帐户都起作用,我希望它只对一种记录类型的帐户起作用。 有人能告诉我要在触发器中添加什么,这样我就可以使它只在一种记录类型上工作吗 以下是我的处理程序类:Triggers 在特定帐户记录类型上触发,triggers,salesforce,apex,recordtype,Triggers,Salesforce,Apex,Recordtype,所以我写了一个触发器,它工作得非常好,完全符合我的要求,但问题是它对所有帐户都起作用,我希望它只对一种记录类型的帐户起作用。 有人能告诉我要在触发器中添加什么,这样我就可以使它只在一种记录类型上工作吗 以下是我的处理程序类: public class AP03_OpportunityLineItem { public static void preventmultipleOpportunityLineItems(List<OpportunityLineItem> listOp
public class AP03_OpportunityLineItem {
public static void preventmultipleOpportunityLineItems(List<OpportunityLineItem> listOppLineItems){
Set<Id>opportunityIds = new Set<Id>();
// get all parent IDs
for(OpportunityLineItem oli : listOppLineItems)
{
//Condition to pick certain records
opportunityIds.add(oli.OpportunityId);
}
// query for related Opportunity Line Items
Map<Id, Opportunity> mapOpportunities = new Map<Id, Opportunity>([SELECT ID,
(SELECT ID
FROM OpportunityLineItems)
FROM Opportunity
WHERE ID IN :opportunityIds]);
// opp counter of new records
Map<Id, Integer> mapOppCounter = new Map<Id, Integer>();
for(OpportunityLineItem oli : listOppLineItems)
{
if(mapOppCounter.containsKey(oli.OpportunityId))
{
mapOppCounter.put(oli.OpportunityId, mapOppCounter.get(oli.OpportunityId)+1);
}
else
{
mapOppCounter.put(oli.OpportunityId, 1);
}
}
//loop to add error if condition violated
for(OpportunityLineItem olitems : listOppLineItems)
{
if(mapOpportunities.get(olitems.OpportunityId).OpportunityLineItems.size()+mapOppCounter.get(olitems.OpportunityId)>1 || olitems.Quantity > 1)
{
olitems.addError('Ce client peut seulement loué un seul véhicule.');
}
}
}
}
您需要循环浏览Opportunities产品并构建opportunity Id列表,然后查询列表中帐户与您想要匹配的记录类型的opportunity,并构建一组和指定记录类型匹配的ID,然后检查该集合是否包含正在处理的opportunity的accountId,以了解opportunity是否跳过或处理它
Set<Id> recordTypeOpp = new Set<ID>();
SET<Id> opportunityIds = new Set<Id>();
Id recordTypeIdYouWant = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId();
for(OpportunityLineItem item : listOppLineItems){
opportunityIds.add(item.OpportunityId);
}
for(Opportunity item : [SELECT Id FROM Opportunity WHERE opportunityIds IN :opportunityIds and Account.RecordTypeId = :recordTypeIdYouWant]){
recordTypeOpp.add(item.Id);
}
for(OpportunityLineItem olitems : listOppLineItems)
{
if(recordTypeOpp.contains(olitems.OpportunityId)){
//do processing
}
else {
continue;
}
}
Set recordTypeOpp=new Set();
SET opportunityIds=new SET();
Id recordTypeIdYouWant=Schema.SObjectType.Account.getRecordTypeInfosByName().get('recordtype Name').getRecordTypeId();
对于(OpportunityLineItem项目:listOppLineItems){
OpportunityId.add(item.OpportunityId);
}
for(Opportunity项:[从Opportunity中选择Id,其中OpportunityID位于:OpportunityID和Account.RecordTypeId=:RecordTypeyYouWant]){
记录类型OPP.add(项目Id);
}
对于(OpportunityLineItems:listOppLineItems)
{
if(recordTypeOpp.contains(olitems.OpportunityId)){
//处理
}
否则{
继续;
}
}
trigger OpportunityLineItemBeforeInsert on OpportunityLineItem (before insert) {
if(PAD.canTrigger('AP03_OpportunityLineItem')){
AP03_OpportunityLineItem.preventmultipleOpportunityLineItems(Trigger.new);
}
}
Set<Id> recordTypeOpp = new Set<ID>();
SET<Id> opportunityIds = new Set<Id>();
Id recordTypeIdYouWant = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId();
for(OpportunityLineItem item : listOppLineItems){
opportunityIds.add(item.OpportunityId);
}
for(Opportunity item : [SELECT Id FROM Opportunity WHERE opportunityIds IN :opportunityIds and Account.RecordTypeId = :recordTypeIdYouWant]){
recordTypeOpp.add(item.Id);
}
for(OpportunityLineItem olitems : listOppLineItems)
{
if(recordTypeOpp.contains(olitems.OpportunityId)){
//do processing
}
else {
continue;
}
}