Triggers Opportunity Apex触发器上的金额更改
我正在尝试创建触发器,如果记录类型是收入风险,那么金额应该保存为负值,这是我的代码,我有错误,我尝试了两种方法,第二种是在注释中。。他们都不工作Triggers Opportunity Apex触发器上的金额更改,triggers,salesforce,apex,Triggers,Salesforce,Apex,我正在尝试创建触发器,如果记录类型是收入风险,那么金额应该保存为负值,这是我的代码,我有错误,我尝试了两种方法,第二种是在注释中。。他们都不工作 public with sharing class amountValidator { //pull data of Opportunity in list public static void validateAmount (list<Opportunity> oppList){ oppList = [Sele
public with sharing class amountValidator {
//pull data of Opportunity in list
public static void validateAmount (list<Opportunity> oppList){
oppList = [Select amount FROM Opportunity WHERE RecordType.Name IN ('Revenue Risk')];
for(Opportunity opportunities : oppList){
if(oppList.amount >= '0'){
oppList.amount = oppList.amount * '-1';
}
}
/*Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
Schema.RecordTypeInfo rtByName = rtMapByName.get('Revenue Risk');
for(Opportunity each : oppList){
if(rtByName.size == 0){
}
else{
if(oppList.Amount >= 0){
oppList.Amount = oppList.Amount*-1;
}
}
}*/
错误很明显:
if(oppList.amount >= '0'){ // THIS LINE WILL THROW AN ERROR: 'Comparison arguments must be compatible types: Integer (or Double), String
oppList.amount = oppList.amount * '-1'; // THIS ONE TOO: 'Arithmetic expressions must use numeric arguments'
}
您的第二个代码段与第一个代码段同样错误
if(oppList.Amount >= 0){
oppList.Amount = oppList.Amount*-1;
// MUST BE each.Amount = each.Amount * - 1; Please try not to use reserved words as variable names
}
您可能想看一看之前描述强类型编程语言的帖子:既然我们还不能添加注释,我们将添加一个新的答案: 您没有更新/插入opportunity的更新金额 正确的做法是创建单独的Opportunity列表,即List oppsToUpdate,并将更新后的Opportunity添加到此列表中
public static void validateAmount (list<Opportunity> oppList){
oppList = [Select amount FROM Opportunity WHERE RecordType.Name IN ('Revenue Risk')]; // Why are you requerying the Opportunity if you already have it??
List<Opportunity> oppsToUpdate = new List<Opportunity>();
for(Opportunity opportunities : oppList){
if(opportunities.amount > 0){
opportunities.amount = opportunities.amount * -1;
oppsToUpdate.add(opportunities);
}
}
upsert opportunities;
}
记住用try-catch语句和系统调试来封装函数,以查看代码的运行情况
这是到输入参数修改的链接,以及为什么这是不好的做法:工作代码:
trigger Risk_NegativeQuantity on OpportunityLineItem (before insert) {
set<id> oppid = new set<id>();
for (OpportunityLineItem oli : trigger.new)
{
oppid.add(oli.opportunityid);
}
Id RevenueRisk= Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Revenue Risk').getRecordTypeId();
list<opportunity> opplist = [select id, recordtype.name,recordtypeid from opportunity where id in : oppid ];
for (OpportunityLineItem oli : trigger.new)
{
for (opportunity opp: opplist)
{
if (oli.opportunityid == opp.id)
{
if(opp.recordtype.name == 'Revenue Risk')
{
if(oli.Quantity > 0)
{
oli.Quantity = oli.Quantity * -1;
}
}
}
}
}
}
您遇到了什么错误?我尝试使用共享类amountValidator{//pull-data of Opportunity in-list public静态无效validateAmount Opportunity oppList{oppList=[从Opportunity WHERE-RecordType.Name in'Revenue Risk'中选择金额];forOpportunity Opportunity:oppList{ifoppList.amount>='0'{oppList.amount=oppList.amount*'-1';}“获取算术表达式不能使用的错误抱歉,这里的回复不好,但这里的内容是新的,不知道如何将其显示为代码。仍然不起作用,这些是我稍后更正的一般错误,但在编码方面存在一些问题