1
trigger updatepipelineamountField on Opportunity (after update) {

Map<Id,Decimal>  budgetIDTotalMap = new MAp<Id,Decimal>();  
set<id> setbudgetId = new set<Id>();

for (Opportunity opp : Trigger.new)
{
    if( opp.budget__c != null){
        setbudgetId.add(opp.budget__c);
    }
}

for(AggregateResult objArg : [SELECT budget__c, SUM(Amount) total FROM Opportunity WHERE budget__c IN: setbudgetId AND (stageName = 'Prospecting' OR stageName = 'Value Proposition')  GROUP BY budget__c])
{
    budgetIDTotalMap.put((Id)objArg.get('budget__c'), (Decimal)objArg.get('total'));
}
List<Budget__c> blist = [select id,Pipeline_Amount__c from budget__c where id in :setbudgetId];
for(Budget__c c: blist)
{
    c.Pipeline_Amount__c = budgetIDTotalMap.containskey(c.id) ? budgetIDTotalMap.get(c.id) : 0;
}
update blist; 
}

and I wrote a test class for this but I am getting error:

@isTest
private class TestupdatepipelineamountField {

@isTest static void TestupdatepipelineamountField() {
    // Test data setup
    // Create an account with an opportunity, and then try to delete it
    Account acct = new Account(Name='Test Account');
    insert acct;
    acct=[SELECT Name FROM Account WHERE Id=:acct.Id];
    System.assertEquals(acct.Name,'TestOpp');

    Budget__c bud = new Budget__c(Name ='Jan', Budget_Amount__c = 10000, Won_Amount__c = 5000, 
                                  Commit_Amount__c = 2000, Best_Case_Amount__c = 1000, 
                                  Pipeline_Amount__c = 1000)
    insert bud;

    bud=[SELECT Name FROM Budget__c WHERE Id=:bud.Id];
    System.assertEquals(bud.Name,'Jan');

    Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                   StageName='Prospecting',budget__c = bud.Name
                                   CloseDate=System.today().addMonths(1),
                                   AccountId=acct.Id);
    insert opp;
    opp=[SELECT Name FROM Opportunity WHERE Id=:opp.Id];
    System.assertEquals(opp.Name,'TestOpp');


}
}

Error: unexpected token: 'insert' at line 14 column 8
can any one help me to figure out the error and let me know where I am wrong. @Ratan

  • budget__c = bud.Name CloseDate=System.today().addMonths(1), you forgot one , here it should be like budget__c = bud.Name, CloseDate=System.today().addMonths(1), – Ratan Paul Jan 29 '16 at 10:47
  • I put (,) now I am getting error : unexpected token: 'insert' at line 15 column 8 – Russel baker 1 Jan 29 '16 at 10:50
  • This topic has already been discussed and answered. Apex assert vs assertEquals – user28237 Jan 29 '16 at 11:25
  • Still getting the same error after change the Account name. @Ratan – Russel baker 1 Jan 29 '16 at 11:42
  • @Russelbaker1 What does your tigger do? I just want to confirm I understand what your trying to do – developer__c Jan 29 '16 at 13:47
0

SO many errors.. SO I am writing complete code..

@isTest 
private class TestupdatepipelineamountField { 

    @isTest static void TestupdatepipelineamountField() { 
        // Test data setup 
        // Create an account with an opportunity, and then try to delete it 
        Account acct = new Account(Name='Test1'); 
        insert acct; 
        acct=[SELECT Name FROM Account WHERE Id=:acct.Id]; 

        Budget__c bud = new Budget__c(Name ='Jan', Budget_Amount__c = 10000, Won_Amount__c = 5000, 
        Commit_Amount__c = 2000, Best_Case_Amount__c = 1000, Pipeline_Amount__c = 1000); 
        insert bud; 

        bud=[SELECT Id, Name FROM Budget__c WHERE Id=:bud.Id]; 

        Opportunity opp = new Opportunity(Name= acct.Name + ' Opportunity', StageName='Prospecting', budget__c = bud.Id, 
                                            CloseDate=System.today().addMonths(1), AccountId=acct.Id); 
        insert opp; 

        opp=[SELECT Name FROM Opportunity WHERE Id=:opp.Id]; 
        System.assertEquals(opp.Name,'Test1 Opportunity'); 

        opp.StageName = 'Value Proposition';

        update opp;

        system.assertEquals([SELECT Budget_Amount__c FROM Budget__c WHERE Id =: bud.Id ].Budget_Amount__c, 10000);

    } 
}
| improve this answer | |
  • Test fail with Error: Assertion fail: Test account, actual: testopp – Russel baker 1 Jan 29 '16 at 10:59
  • @Russelbaker1 what is expected result ? – Ratan Paul Jan 29 '16 at 11:43
  • Test is fail. I saw in developer console so it is showing error: String Exception :Invalid id: Jan Line 20 column 1 – Russel baker 1 Jan 29 '16 at 11:48
  • 1
    There is no point in your first 3 asserts. You're inserting objects and asserting their names (which you just inserted)... are you just testing DML there? The last assert is good :) – mkorman Jan 29 '16 at 14:24
0

In the test class , you are not closing a line with ';'

Budget__c bud = new Budget__c(Name ='Jan', Budget_Amount__c = 10000, Won_Amount__c = 5000, 
                                  Commit_Amount__c = 2000, Best_Case_Amount__c = 1000, 
                                  Pipeline_Amount__c = 1000);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.