0

I am creating a trigger on my Opportunties object which on update alters some fields in the object, I also need this to update a related object called RBCO__c. The foreign key used to reference the related objects is RBCORelated__c.

When saving the trigger an error is displayed related to this code segment stating RBCORelation__c variable is not found. I've tried simply using Opp.RBCORelation__c but it says it expects a semicolon.

Opportunity Opp = Trigger.New[0];        
List<RBCO__c> listRBCO = [Select id, Total_Days__c, Days_Remaining__c from RBCO__c 
                                                   WHERE id = :RBCORelation__c];

EDIT

It seems there are more variables that do not exist even though they are visible: Please look at the code below:

trigger RBCOupdate on Opportunity (before update) {

       Opportunity Opp = Trigger.New[0];    
       Opportunity oldOpp = Trigger.oldMap.get(Opp.ID);
       private double difference = 0;
       private double remainder = 0;

       if(Opp.RBCO_Days__c != oldOpp.RBCO_Days__c ){
            if(Opp.RBCORelation == '') {
               List<RBCO__c> listRBCO = [Select id, Total_Days__c, Days_Remaining__c from RBCO__c 
                                               WHERE Days_Remaining__c > 0
                                               ORDER BY Date_Start__c DESC LIMIT 1];                                     
            } else {
               List<RBCO__c> listRBCO = [Select id, Total_Days__c, Days_Remaining__c from RBCO__c 
                                               WHERE id = :Opp.RBCORelation__c];
            }          

            for(RBCO__c rbco : listRBCO) {  

                 if(Opp.RBCO_Days__c < oldOpp.RBCO_Days__c && Opp.RBCO_Days__c > 0) {
                     difference = Opp.RBCO_Days__c - oldOpp.RBCO_Days__c;
                     remainder = rbco.Days_Remaining__c - difference;
                     rbco.Days_Remaining__c = remainder;
                 } else if(Opp.RBCO_Days__c > oldOpp.RBCO_Days__c && Opp.RBCO_Days__c > 0) {
                     difference = oldOpp.RBCO_Days__c - Opp.RBCO_Days__c;
                     remainder = rbco.Days_Remaining__c + difference;
                     rbco.Days_Remaining__c = remainder;
                     Opp.RBCORelation__c = rbco.Id;
                 } else if(oldOpp.RBCO_Days__c == 0 && Opp.RBCO_Days__c > 0) {
                     difference = Opp.RBCO_Days__c;
                     remainder = rbco.Days_Remaining__c - difference;
                     rbco.Days_Remaining__c = remainder;
                     Opp.RBCORelation__c = rbco.Id;
                 } else if(oldOpp.RBCO_Days__c > 0 && Opp.RBCO_Days__c == 0) {
                     difference = oldOpp.RBCO_Days__c;
                     remainder = rbco.Days_Remaining__c + difference;
                     rbco.Days_Remaining__c = remainder;
                     Opp.RBCORelation__c = null;
                 } else {
                    // do nothing
                 }               
            }

            if(remainder >= 0) {
                update listRBCO;     
            } else {
                trigger.new[0].RBCO_Days__c.addError('No sufficient funds: ' + remainder + ' shortfall');
            }

       }

}
2
  • 1
    Have you tried ... WHERE id = :Opp.RBCORelation__c?
    – BarCotter
    Commented Aug 12, 2014 at 16:16
  • @BarCotter I cannot believe I did not try this! Please convert into an answer. Commented Aug 12, 2014 at 16:18

2 Answers 2

3

This should work for you

Opportunity Opp = Trigger.New[0];        
List<RBCO__c> listRBCO = [Select id, Total_Days__c, Days_Remaining__c from RBCO__c 
                                                   WHERE id =: opp.RBCORelation__c];

I would however think about bulkifying your trigger as this only works for 1 object. If there is a mass update then it will only run on the first item and none of the rest.

Something like this would be more ideal

map<Id,RBCO__c> rbcoMap = new map<Id,RBCO__c>();
for(Opportunity o : trigger.new){
     rbcoIDs.put(o.RBCO__c, null);
}
rbcoMap.remove(null);
rbcoMap.putAll([Select Id, Name, other_field__c From RBCO__c Where Id In : rbcoMap.keyset()]);

Now you have a map that has all related RBCO__c objects. You can access it while in a loop over the Opportunity object and do your needed logic

for(Opportunity o: trigger.new){
    if(o.RBCO__C != null){
        RBCO__c myRBCO = rbcoMap.get(o.RBCO__c);
    }
}

For more info on bulkifying your triggers and some other best practices I would take a look here

http://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-salesforce/

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bestpract.htm

1

I believe :opp.RBCORelation__c should work (if you named the relation RBCORelation__c):

Opportunity Opp = Trigger.New[0];        
List<RBCO__c> listRBCO = [Select id, Total_Days__c, Days_Remaining__c from RBCO__c 
                                                   WHERE id = :Opp.RBCORelation__c];

But make sure you check how you named the relationship field in opportunity because it might be different. ie you could of named it RBCO_Relation__c and it would still be a valid relationship field name.

In that case it would be

Opportunity Opp = Trigger.New[0]; List<RBCO__c> listRBCO = [Select id, Total_Days__c, Days_Remaining__c from RBCO__c WHERE id =:Opp.RBCO_Relation__c];

Let me know how it goes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .