Opportunity Apex Trigger的金额变化

时间:2015-03-20 20:26:07

标签: triggers salesforce apex

我尝试创建触发器,如果​​记录类型是收入风险,那么金额应保存为负值,这是我的代码,我有错误,我尝试了两种方式,第二个是评论..他们都没有工作

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

3 个答案:

答案 0 :(得分:0)

错误非常明确:

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
        }

您可能需要查看以前描述强类型编程语言的帖子:Strongly Typed

答案 1 :(得分:0)

由于我们尚未添加评论,我们将添加新答案:

您没有为您的机会更新/插入更新的金额。

执行此操作的正确方法是创建单独的商机列表(即列表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语句将函数包含在内,以查看代码的内容。

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

答案 2 :(得分:0)

工作代码:

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