机会 Apex 触发器的数量变化

Amount change on Opportunity Apex Trigger

我正在尝试创建触发器,如果​​记录类型是 Revenue Risk 那么金额应该保存为负值,这是我的代码,我在其中出错,我尝试了两种方法,第二种是在评论中.. none 个正在工作

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
        }

您可能想看看之前的 post 描述强类型编程语言:Strongly Typed

由于我们现在还不能添加评论,我们将添加一个新的答案:

您未达到 updating/inserting 机会的更新金额。

正确的做法是创建一个单独的机会列表(即列表 oppsToUpdate)并将更新的机会添加到该列表。

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 语句中,以查看您的代码发生了什么。

这是对输入参数的 link 修改以及为什么这是不好的做法:Input Parameters

工作代码:

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;
                    }
                }
            }
        }
    }
}