0

I'm trying to create trigger if record type is Revenue Risk then amount should be saved in negative value, Here's my code in which I'm having error, I tried it two ways, second is in comments.. none of them is working

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;
            }
        }
    }*/
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dave
  • 101
  • 6
  • 1
    What error are you getting? – Drazisil Mar 20 '15 at 20:31
  • I tried this one `code` public with sharing class amountValidator { //pull data of Opportunity in list public static void validateAmount (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'; } }' getting error that arithmetic expression can't be used – Dave Mar 20 '15 at 20:41
  • sorry for poor reply here but new for posting stuff here and don't know how to show it as a code. – Dave Mar 20 '15 at 20:45

3 Answers3

0

The error is very clear:

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'

}

Your second code snippet is also wrong (same for first one).

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
        }

You may want to take a look at a previous post describing strongly typed programming languages: Strongly Typed

Community
  • 1
  • 1
  • Still not working, those were generic mistakes which I corrected later on, but there's some problem in coding – Dave Mar 27 '15 at 12:37
0

Since we can't add comments just yet, we're going to add a new answer:

You're not updating/inserting the updated amount for your opportunity.

The correct way of doing this is to create a separate List of Opportunities (i.e. List oppsToUpdate) and add the updated opportunities to this list.

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

Remember to enclose your function with try-catch statements with system debugs to see what's going on with your code.

And this is the link to the input parameter modifications and why this is bad practice: Input Parameters

Community
  • 1
  • 1
0

Working Code:

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;
                    }
                }
            }
        }
    }
}
Dave
  • 101
  • 6