我写的关于salsforce.com的第一个Trigger
trigger oppStageTrigger on Opportunities(before insert,before update)
{
if(Trigger.isBefore)
{
if(Trigger.isUpdate||Trigger.isinsert)
{
List<Product_Forecast_c> PFList=new List<Product_Forecast_c>();
//Defines a Product_Forecast_c list with no elements
//Determine the distinct opportunities entries
Set<ID> pbeIds=new Set<ID>();
for(OpportunityLineItem oli:Trigger.new)
pbeIds.add(oli.product_forcastid);
//Get the specific record of Opportunities
Map<Id,Opportunities> opportunityMap=new Map<Id,Opportunities>
(
//Return stage value
[select stagename from Opportunities where id in :pbeIds]
);
string oppName=[select name from opportunities where id in:pbeIds].name;
//Get the records of product@forcast by opportunities's name
for(Product_forecast_c PFC:[select spansion_part_c from product_forecast_c where opportunity_c=:oppName])
{
PFList.add(PFC.spansion_part_c);
}
//string[] partnum=[select spansion_part_c from product_forecast_c where opportunity_c=:oppName];
for(OpportunityLineItem oli:Trigger.new)
string stagevalue=opportunities.get(oli.OpportunitiesId).stageName;
for(integer i=0;i<PFList.size();i++)
{
if(stagevalue=='Reference Design Win'||stagevalue=='Design Win'||stagevalue=='Closed PO/Production')
{
integer PFID=[select product_forecast_cid from product_forecast_c where opportunity_c=:oppName].product_forecast_cid;
if(PFList.get(i)==null)
{
for(OpportunityLineItem oli:Trigger.new)
{
oli.addError('Spansion Part Number is required on all Product & Forecast items
when setting the Stage to this value. Please enter Spansion Part Number
by clicking Edit next to the item(s) in the Product & Forecast section.');
}
}
else if(PFID==null)
{
for(OpportunityLineItem oli:Trigger.new)
{
oli.addError('A Product & Forecast item (with Spansion Part Number) is required w
hen setting the Stage to this value. Please create a Product & Forecast
entry by clicking the "New" button in that section.');
}
}
else
{
switch(i)
{
case 0:
{
// oli.spansion_part_no_1_c is the first spansion_part_# which is needed to show
oli.spansion_part_no_1_c=PFList.get(0);
break;
}
case 1:
{
// oli.spansion_part_no_2_c is the first spansion_part_# which is needed to show
oli.spansion_part_no_2_c=PFList.get(1);
break;
}
}
}
}
else
{
switch(i)
{
case 0:
{
// oli.spansion_part_no_1_c is the first spansion_part_# which is needed to show
oli.spansion_part_no_1_c=PFList.get(0);
break;
}
case 1:
{
// oli.spansion_part_no_2_c is the first spansion_part_# which is needed to show
oli.spansion_part_no_2_c=PFList.get(1);
break;
}
}
}
}
}
}
}
这个trigger有些语法问题,经过修改后的代码如下(修改这个的过程真的很痛苦,因为我对它语法不是很熟悉):
trigger oppStageTrigger on Opportunity(before update) { if(Trigger.isBefore) { if(Trigger.isUpdate) { integer j=0; Map<Integer,String> m=new Map<Integer,String>(); Set<String> pbeIds=new Set<String>();
for(Opportunity oli:Trigger.old) pbeIds.add(oli.Name);
String oppName=[select name from Opportunity where Name in:pbeIds].name;integer PFID=[select count() from Product_Forecast__c where Opportunity__r.name=:oppName];
//Set<ID> pbeIds=new Set<ID>();//for(Opportunity oli:Trigger.old)
// pbeIds.add(oli.id);
//String oppName=[select name from Opportunity where id in:pbeIds].name;Opportunity名称可以重复,故改用id来获取唯一的记录
String oppName=Trigger.old[0].name;用这条语句可以很快地获取name,而不必去再次查询了(系统出错时才发现这便捷的方法,当初学习真失败!!
)。
if(PFID==0) { if(Trigger.new[0].StageName=='Reference Design Win'||Trigger.new[0].StageName=='Design Win'||Trigger.new[0].StageName=='Closed PO/Production') { Trigger.new[0].addError('A Product & Forecast item (with Spansion Part Number) is required w hen setting the Stage to this value. Please create a Product & Forecast entry by clicking the "New" button in that section.'); } } try { for(Product_Forecast__c PFC:[select Spansion_part__c from Product_Forecast__c where opportunity__r.name=:oppName]) { m.put(j,PFC.Spansion_part__c); j++; } } catch(QueryException e) { system.debug(e); } for(integer i=0;i<m.size();i++) { if(Trigger.new[0].StageName=='Reference Design Win'||Trigger.new[0].StageName=='Design Win'||Trigger.new[0].StageName=='Closed PO/Production') { if(m.get(i)==null) { Trigger.new[0].addError('Spansion Part Number is required on all Product & Forecast items when setting the Stage to this value. Please enter Spansion Part Number by clicking Edit next to the item(s) in the Product & Forecast section.'); } else { if(i==0) { for(Opportunity oli:Trigger.new) { //spansion_part_no_1_c is the first spansion_part_# which is needed to show oli.spansion_part_no_1__c=m.get(0); break; } } else if(i==1) { for(Opportunity oli:Trigger.new) { //spansion_part_no_2_c is the first spansion_part_# which is needed to show oli.spansion_part_no_2__c=m.get(1); break; } } } } else { if(i==0) { for(Opportunity oli:Trigger.new) { //spansion_part_no_1_c is the first spansion_part_# which is needed to show oli.spansion_part_no_1__c=m.get(0); break; } } else if(i==1) { for(Opportunity oli:Trigger.new) { //spansion_part_no_2_c is the first spansion_part_# which is needed to show oli.spansion_part_no_2__c=m.get(1); break; } } } } } } }