• Strada
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 16
    Replies

Hi guys,

 

I'm wondering if anyone can help. I know this is probably something trivial that I'm missing but I have written a trigger that compares 2 amount field values in 2 different objects - Opportunity amount and Change Request amount. If these values are not equal then my trigger will force the Change Request amount to take the value of the Opportunity amount.

 

This all works fine, however, in my test class when I update the amount of the Change Request (so that it fires this update trigger), let's say change it to 150 from value 90, my assert statement keeps failing because the values are not equal. Here is a snippet of code from my test class:

 

---

Change_Request__c cr = new Change_Request__c(Summary__c = 'Test Crash Change Request', Amount__c = 90.00, Opportunity__c = myOpportunity.Id);

 

insert cr;      
        
System.assertEquals(myOpportunity.Amount, cr.Amount__c); //assert statement passes as two amounts are equal (90)
        
cr.Amount__c = 150.00;
update cr;
        
List<Change_Request__c> myCR = [Select Amount__c from Change_Request__c where id = : cr.Id];

 

System.assertEquals(myOpportunity.Amount, myCR[0].Amount__c); //failure as assert expects both values to be equal,

                                                                                                                            //but myCR[0].Amount__c = 150

---

 

So I can't figure out how to return the updated value from the trigger. My debug logs show this value of 150 getting updated to 90 in my trigger code, but no matter what I try in my test class I can't seem to achieve the same result.

 

Your help is great appreciated!

  • August 14, 2012
  • Like
  • 0

Hi guys,

I'm wondering if anyone can please help or at least point me in the direction of where I can get help for this issue.

 

For some reason I can't deploy some updated code from my sandbox to our live system. The last code deployed was on 16/07/2012, and this comprised of an updated trigger and test class.

 

Today I have tried to deploy a small change to both the trigger and test class and now I am getting a validation failure on a different object - validation of an assert in a different test class is occurring for some reason that I cannot explain. I have tried to run tests in my sandbox in order to recreate this assertion failure but I cannot reproduce the problem in my sandbox.

 

This is very strange and I am at a loss to figure out how this happened in the first place and more importantly how to resolve the issue. As it stands I cannot deploy any code to the live system now because of this.

If anyone can provide any help to me on this I would greatly appreciate it.

Hi all,

 

I'm wondering if anyone can help me out with my code. I have 2 objects: Opportunity and Change Request (custom object). Both are related via a lookup relationship where a lookup field to the Opportunity object exists on the Change Request custom object. 

 

I am trying to implement functionality whereby when the amount field value on the Opportunity record is changed, then the amount field on the Change Request object should be changed to this value also when the update trigger is fired. I have been able to validate that this has happened in my trigger by printing out values, however, in my test class when I try to pull the data from the database after the update to the opportunity value, the change request value hasn't changed and still retains the original opportunity value.

 

I know I'm probably making a very fundamental mistake but I've been trying to understand what I'm doing wrong but I can't seem to find any answers to my problem.

 

Below is my code, any help or tips to point me in the right direction in finding a solution to my problem would be greatly appreciated.

 

Thanks a million in advance!

 

----------
Trigger
----------

trigger OpportunityUpdateAmountCheck on Opportunity (after update) {
 
 
  list<Opportunity> opps = new list<Opportunity>();
  Decimal crAmount;
  Decimal oppAmount;
 
  for (Opportunity opp : Trigger.new) {
      List<Change_Request__c> changeReqs = [SELECT id, Amount__c, Opportunity__c FROM Change_Request__c WHERE Opportunity__c = :opp.Id];   
      
      
      List<Change_Request__c> crsToUpdate = new List<Change_Request__c>();
      
      for(Change_Request__c thisCR: changeReqs ){
          crAmount = thisCR.Amount__c;
          System.debug('Initial crAmount is: ' + crAmount);
          oppAmount = opp.Amount;
          if(crAmount != oppAmount){
              crAmount = oppAmount;
              crsToUpdate.add(thisCR);
          }
      }
    
      update changeReqs;
      System.debug('Updated crAmount is: ' + crAmount);
  }
}

-------------------
TEST CLASS:
-------------------
    
@isTest
public with sharing class TestOpportunityUpdateAmount {

static testMethod void myUnitTest() {

        //create Account
        Account myAccount = new Account (name='XYZ1 Organization');
        myAccount.BillingCountry = 'Ireland';
        myAccount.Type = 'Prospect';
        myAccount.Status__c = 'Prospect';
        insert myAccount;
                
        Contact mycontact = new Contact (lastName='XYZ Contact',Contact_Status__c = 'Pending',MailingCountry='Ireland');
        mycontact.account = myAccount;
        insert mycontact;
        
        List<Account> savedAccount = [SELECT Status__c, Type FROM Account WHERE name = 'XYZ1 Organization'];
        
        //create Campaign
        Campaign myCampaign = new Campaign(name='XYZ Campaign');
        myCampaign.Type = 'Advertisement';
        insert myCampaign;
        
        Opportunity[] myOpps = new Opportunity[]{
            new Opportunity(name='XYZ Opp', AccountId=savedAccount[0].id, StageName='Open', Campaign=myCampaign, CloseDate = date.today(),
                            amount=90.00,Communication_Type__c = 'Internal Comms'),
            new Opportunity(name='ABC Opp', AccountId=savedAccount[0].id, StageName='Closed-Won', Campaign=myCampaign, CloseDate = date.today(),
                            amount=90.00,Communication_Type__c = 'Internal Comms')
        };
        
        insert myOpps;
        
        Change_Request__c[] myCRs = new Change_Request__c[]{
            new Change_Request__c(Solution__C = myOpps[0].Solution__c, Type__c = myOpps[0].Solution_Category__c,
                Opportunity__c = myOpps[0].Id, Organisation__c = myOpps[0].AccountId, Amount__c = myOpps[0].Amount),
            new Change_Request__c(Solution__C = myOpps[1].Solution__c, Type__c = myOpps[1].Solution_Category__c,
                Opportunity__c = myOpps[1].Id, Organisation__c = myOpps[1].AccountId, Amount__c = myOpps[1].Amount)};
        insert myCRs;
        
        
        myOpps[0].Amount = 150.00;
        update myOpps;
        
        List<Change_Request__c> existingChangeRequest2 = [SELECT id, Amount__c FROM Change_Request__c WHERE Opportunity__c = :myOpps];
        System.assertEquals(myOpps[0].Amount, existingChangeRequest2[0].Amount__c);
        
    }
}

 

Hi guys,

 

I'm a newbee to Apex coding and I'm not long in my new role working on Salesforce admin and development. I'm just wondering if any of you kind people can help me with my following scenario.

 

I have 2 objects and I'm trying to figure out a way to perform a check (from Object A) on the status of a number of instances of the other object (Object B). Basically what I need to setup is a check that will change the status of Object A to complete, once the statuses of all instances of Object B are marked as complete.

 

My 2 objects are related through a lookup field that is on Object B, and it is a lookup field for Object A.

 

I hope my query makes sense. If any of you can provide me with some pointers on how to achieve what I'm looking for I would be very grateful.

 

Many thanks in advance for your help with this!

Hi guys,

 

I'm wondering if anyone can help. I know this is probably something trivial that I'm missing but I have written a trigger that compares 2 amount field values in 2 different objects - Opportunity amount and Change Request amount. If these values are not equal then my trigger will force the Change Request amount to take the value of the Opportunity amount.

 

This all works fine, however, in my test class when I update the amount of the Change Request (so that it fires this update trigger), let's say change it to 150 from value 90, my assert statement keeps failing because the values are not equal. Here is a snippet of code from my test class:

 

---

Change_Request__c cr = new Change_Request__c(Summary__c = 'Test Crash Change Request', Amount__c = 90.00, Opportunity__c = myOpportunity.Id);

 

insert cr;      
        
System.assertEquals(myOpportunity.Amount, cr.Amount__c); //assert statement passes as two amounts are equal (90)
        
cr.Amount__c = 150.00;
update cr;
        
List<Change_Request__c> myCR = [Select Amount__c from Change_Request__c where id = : cr.Id];

 

System.assertEquals(myOpportunity.Amount, myCR[0].Amount__c); //failure as assert expects both values to be equal,

                                                                                                                            //but myCR[0].Amount__c = 150

---

 

So I can't figure out how to return the updated value from the trigger. My debug logs show this value of 150 getting updated to 90 in my trigger code, but no matter what I try in my test class I can't seem to achieve the same result.

 

Your help is great appreciated!

  • August 14, 2012
  • Like
  • 0

Hi guys,

I'm wondering if anyone can please help or at least point me in the direction of where I can get help for this issue.

 

For some reason I can't deploy some updated code from my sandbox to our live system. The last code deployed was on 16/07/2012, and this comprised of an updated trigger and test class.

 

Today I have tried to deploy a small change to both the trigger and test class and now I am getting a validation failure on a different object - validation of an assert in a different test class is occurring for some reason that I cannot explain. I have tried to run tests in my sandbox in order to recreate this assertion failure but I cannot reproduce the problem in my sandbox.

 

This is very strange and I am at a loss to figure out how this happened in the first place and more importantly how to resolve the issue. As it stands I cannot deploy any code to the live system now because of this.

If anyone can provide any help to me on this I would greatly appreciate it.

Hi all,

 

I'm wondering if anyone can help me out with my code. I have 2 objects: Opportunity and Change Request (custom object). Both are related via a lookup relationship where a lookup field to the Opportunity object exists on the Change Request custom object. 

 

I am trying to implement functionality whereby when the amount field value on the Opportunity record is changed, then the amount field on the Change Request object should be changed to this value also when the update trigger is fired. I have been able to validate that this has happened in my trigger by printing out values, however, in my test class when I try to pull the data from the database after the update to the opportunity value, the change request value hasn't changed and still retains the original opportunity value.

 

I know I'm probably making a very fundamental mistake but I've been trying to understand what I'm doing wrong but I can't seem to find any answers to my problem.

 

Below is my code, any help or tips to point me in the right direction in finding a solution to my problem would be greatly appreciated.

 

Thanks a million in advance!

 

----------
Trigger
----------

trigger OpportunityUpdateAmountCheck on Opportunity (after update) {
 
 
  list<Opportunity> opps = new list<Opportunity>();
  Decimal crAmount;
  Decimal oppAmount;
 
  for (Opportunity opp : Trigger.new) {
      List<Change_Request__c> changeReqs = [SELECT id, Amount__c, Opportunity__c FROM Change_Request__c WHERE Opportunity__c = :opp.Id];   
      
      
      List<Change_Request__c> crsToUpdate = new List<Change_Request__c>();
      
      for(Change_Request__c thisCR: changeReqs ){
          crAmount = thisCR.Amount__c;
          System.debug('Initial crAmount is: ' + crAmount);
          oppAmount = opp.Amount;
          if(crAmount != oppAmount){
              crAmount = oppAmount;
              crsToUpdate.add(thisCR);
          }
      }
    
      update changeReqs;
      System.debug('Updated crAmount is: ' + crAmount);
  }
}

-------------------
TEST CLASS:
-------------------
    
@isTest
public with sharing class TestOpportunityUpdateAmount {

static testMethod void myUnitTest() {

        //create Account
        Account myAccount = new Account (name='XYZ1 Organization');
        myAccount.BillingCountry = 'Ireland';
        myAccount.Type = 'Prospect';
        myAccount.Status__c = 'Prospect';
        insert myAccount;
                
        Contact mycontact = new Contact (lastName='XYZ Contact',Contact_Status__c = 'Pending',MailingCountry='Ireland');
        mycontact.account = myAccount;
        insert mycontact;
        
        List<Account> savedAccount = [SELECT Status__c, Type FROM Account WHERE name = 'XYZ1 Organization'];
        
        //create Campaign
        Campaign myCampaign = new Campaign(name='XYZ Campaign');
        myCampaign.Type = 'Advertisement';
        insert myCampaign;
        
        Opportunity[] myOpps = new Opportunity[]{
            new Opportunity(name='XYZ Opp', AccountId=savedAccount[0].id, StageName='Open', Campaign=myCampaign, CloseDate = date.today(),
                            amount=90.00,Communication_Type__c = 'Internal Comms'),
            new Opportunity(name='ABC Opp', AccountId=savedAccount[0].id, StageName='Closed-Won', Campaign=myCampaign, CloseDate = date.today(),
                            amount=90.00,Communication_Type__c = 'Internal Comms')
        };
        
        insert myOpps;
        
        Change_Request__c[] myCRs = new Change_Request__c[]{
            new Change_Request__c(Solution__C = myOpps[0].Solution__c, Type__c = myOpps[0].Solution_Category__c,
                Opportunity__c = myOpps[0].Id, Organisation__c = myOpps[0].AccountId, Amount__c = myOpps[0].Amount),
            new Change_Request__c(Solution__C = myOpps[1].Solution__c, Type__c = myOpps[1].Solution_Category__c,
                Opportunity__c = myOpps[1].Id, Organisation__c = myOpps[1].AccountId, Amount__c = myOpps[1].Amount)};
        insert myCRs;
        
        
        myOpps[0].Amount = 150.00;
        update myOpps;
        
        List<Change_Request__c> existingChangeRequest2 = [SELECT id, Amount__c FROM Change_Request__c WHERE Opportunity__c = :myOpps];
        System.assertEquals(myOpps[0].Amount, existingChangeRequest2[0].Amount__c);
        
    }
}

 

Hi guys,

 

I'm a newbee to Apex coding and I'm not long in my new role working on Salesforce admin and development. I'm just wondering if any of you kind people can help me with my following scenario.

 

I have 2 objects and I'm trying to figure out a way to perform a check (from Object A) on the status of a number of instances of the other object (Object B). Basically what I need to setup is a check that will change the status of Object A to complete, once the statuses of all instances of Object B are marked as complete.

 

My 2 objects are related through a lookup field that is on Object B, and it is a lookup field for Object A.

 

I hope my query makes sense. If any of you can provide me with some pointers on how to achieve what I'm looking for I would be very grateful.

 

Many thanks in advance for your help with this!