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');
}
}
}
... WHERE id = :Opp.RBCORelation__c
?