+ Start a Discussion
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Why is my code failing to save? Expression cannot be assigned at line -1 column -1

Hey there, I have this trigger which is designed to rollup a number field into either one field or another based on certain criteria...However it must be phrased wrong or something as it is not saving. Please help. Thank you in advance



trigger FinanceCommissionEntry on Finance_Commission__c (after delete, after insert, after update) {

  //Limit the size of list by using Sets which do not contain duplicate elements
  set<id> OffComIds = new set<id>();

  //When adding new payments or updating existing payments
  if(trigger.isInsert || trigger.isUpdate){
    for(Finance_Commission__c FinCom : trigger.new){
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }

  //When deleting Finance
  if(trigger.isDelete){
    for(Finance_Commission__c FinCom : trigger.old){
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }

  //Map will contain one Opportunity Id to one sum value
  map<id,double>OffComMap = new map <id,double>();

  //Produce a sum of Payments__c and add them to the map
  //use group by to have a single Opportunity Id with a single sum value
  for(AggregateResult q : [select Office_Commission__c,sum(Finance_Commission_Amount__c),Finance_Commission_Institution__c
    from Finance_Commission__c where Office_Commission__c IN :OffComIds group by Office_Commission__c,Finance_Commission_Institution__c]){
      OffComMap.put((Id)q.get('Office_Commission__c'),(Double)q.get('expr0'));
  }

  List<Office_Commission__c> OffComToUpdate = new List<Office_Commission__c>();

  //Run the for loop on Opportunity using the non-duplicate set of Opportunities Ids
  //Get the sum value from the map and create a list of Opportunities to update


for(Office_Commission__c o : [Select Id, St_George_Upfront_Commission__c, DMS_Upfront_Commission__c from Office_Commission__c where Id IN :OffComIds]){
    Double PaymentSum = OffComMap.get(o.Id);



if (Finance_commission__c.Finance_Commission_Institution__c = 'St George') {


    o.St_George_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);
   
    }else if (Finance_Commission__c.Finance_Commission_Institution__c = 'DMS') {


    o.DMS_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);


  }

  update OffComToUpdate;
}
}
Best Answer chosen by Developer.mikie.Apex.Student
Sri549Sri549
Hello Mikie,

Try with this code

trigger FinanceCommissionEntry on Finance_Commission__c (after delete, after insert, after update) {
  set<id> OffComIds = new set<id>();
  if(trigger.isInsert || trigger.isUpdate){
    for(Finance_Commission__c FinCom : trigger.new)
    {
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }

  if(trigger.isDelete){
    for(Finance_Commission__c FinCom : trigger.old)
    {
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }
  map<id,double>OffComMap = new map <id,double>();

  for(AggregateResult q : [select Office_Commission__c,sum(Finance_Commission_Amount__c),Finance_Commission_Institution__c
    from Finance_Commission__c where Office_Commission__c IN :OffComIds group by    Office_Commission__c,Finance_Commission_Institution__c]){
      OffComMap.put((Id)q.get('Office_Commission__c'),(Double)q.get('expr0'));
  }

  List<Office_Commission__c> OffComToUpdate = new List<Office_Commission__c>();

for(Office_Commission__c o : [Select Id, St_George_Upfront_Commission__c, DMS_Upfront_Commission__c from Office_Commission__c where Id IN :OffComIds]){
    Double PaymentSum = OffComMap.get(o.Id);

if (Finance_commission__c.Finance_Commission_Institution__c == 'St George') {

    o.St_George_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);
  
    }else if (Finance_Commission__c.Finance_Commission_Institution__c == 'DMS') {


    o.DMS_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);


  }

  update OffComToUpdate;
}
}

if you face with same problem just ping me again i will check it other wise if you dont face any problem and if you reach ur requirment

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer

All Answers

Sri549Sri549
Hello Mikie,

Try with this code

trigger FinanceCommissionEntry on Finance_Commission__c (after delete, after insert, after update) {
  set<id> OffComIds = new set<id>();
  if(trigger.isInsert || trigger.isUpdate){
    for(Finance_Commission__c FinCom : trigger.new)
    {
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }

  if(trigger.isDelete){
    for(Finance_Commission__c FinCom : trigger.old)
    {
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }
  map<id,double>OffComMap = new map <id,double>();

  for(AggregateResult q : [select Office_Commission__c,sum(Finance_Commission_Amount__c),Finance_Commission_Institution__c
    from Finance_Commission__c where Office_Commission__c IN :OffComIds group by    Office_Commission__c,Finance_Commission_Institution__c]){
      OffComMap.put((Id)q.get('Office_Commission__c'),(Double)q.get('expr0'));
  }

  List<Office_Commission__c> OffComToUpdate = new List<Office_Commission__c>();

for(Office_Commission__c o : [Select Id, St_George_Upfront_Commission__c, DMS_Upfront_Commission__c from Office_Commission__c where Id IN :OffComIds]){
    Double PaymentSum = OffComMap.get(o.Id);

if (Finance_commission__c.Finance_Commission_Institution__c == 'St George') {

    o.St_George_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);
  
    }else if (Finance_Commission__c.Finance_Commission_Institution__c == 'DMS') {


    o.DMS_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);


  }

  update OffComToUpdate;
}
}

if you face with same problem just ping me again i will check it other wise if you dont face any problem and if you reach ur requirment

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer
This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Hey Sri,

Thank you for your reply. I tried your code but it returned this error:

rror: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 28 column 5

Does this have to do with the fact that the fields are picklist values?

Thank you so much for your assistance.

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Hey Sri,

I solved the problem using your revised code. i just had to introduce the If statements as variables. Probably not he right way to describe it...But, here is the code regardless. i will select your answer as the solution though as I could not have done it without you.

Thank you for your help Sri!

trigger FinanceCommissionEntry on Finance_Commission__c (after delete, after insert, after update) {
  set<id> OffComIds = new set<id>();
  if(trigger.isInsert || trigger.isUpdate){
    for(Finance_Commission__c FinCom : trigger.new)
    {
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }

  if(trigger.isDelete){
    for(Finance_Commission__c FinCom : trigger.old)
    {
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }
  map<id,double>OffComMap = new map <id,double>();

  for(AggregateResult q : [select Office_Commission__c,sum(Finance_Commission_Amount__c),Finance_Commission_Institution__c
    from Finance_Commission__c where Office_Commission__c IN :OffComIds group by    Office_Commission__c,Finance_Commission_Institution__c]){
      OffComMap.put((Id)q.get('Office_Commission__c'),(Double)q.get('expr0'));
  }

  List<Office_Commission__c> OffComToUpdate = new List<Office_Commission__c>();

for(Office_Commission__c o : [Select Id, St_George_Upfront_Commission__c, DMS_Upfront_Commission__c from Office_Commission__c where Id IN :OffComIds]){
    Double PaymentSum = OffComMap.get(o.Id);
   
    for (Finance_commission__c FinCom : Trigger.new) {

if (FinCom.Finance_Commission_Institution__c == 'St George') {

    o.St_George_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);
 
    }else if (FinCom.Finance_Commission_Institution__c == 'DMS') {


    o.DMS_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);


  }

  update OffComToUpdate;
}
}
}
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Hey Sri,

Sorry to trouble you again. I got the trigger working to a point...It will sort St george Finance institution (finance commission records) into the St george rollup number field.... but DMS and the new AFG field both rollup just the DMS finance_commission records...

E.G.
St_george_Upfront_commission__c = 3434.02
DMS_Upfront_commission__c = 7676.02
AFG_Upfront_commission__c = 7676.02

The bottom two results are the same. 

There also seems to be an issue with deleting..I assume this is as a result of there being different fields that the trigger rolls up the same field. i get this error:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger FinanceCommissionEntry caused an unexpected exception, contact your administrator: FinanceCommissionEntry: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.FinanceCommissionEntry: line 28, column 1".


This is my revised code: Thank you for your help

trigger FinanceCommissionEntry on Finance_Commission__c (after delete, after insert, after update) {
  set<id> OffComIds = new set<id>();
  if(trigger.isInsert || trigger.isUpdate){
    for(Finance_Commission__c FinCom : trigger.new)
    {
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }

  if(trigger.isDelete){
    for(Finance_Commission__c FinCom : trigger.old)
    {
      OffComIds.add(FinCom.Office_Commission__c);
    }
  }
  map<id,double>OffComMap = new map <id,double>();

  for(AggregateResult q : [select Office_Commission__c,sum(Finance_Commission_Amount__c),Finance_Commission_Institution__c,Finance_Commission_Type__c
    from Finance_Commission__c where Office_Commission__c IN :OffComIds group by Office_Commission__c,Finance_Commission_Institution__c,Finance_Commission_Type__c]){
      OffComMap.put((Id)q.get('Office_Commission__c'),(Double)q.get('expr0'));
  }

  List<Office_Commission__c> OffComToUpdate = new List<Office_Commission__c>();

for(Office_Commission__c o : [Select Id, St_George_Upfront_Commission__c, DMS_Upfront_Commission__c, AFG_Upfront_Commission__c from Office_Commission__c where Id IN :OffComIds]){
    Double PaymentSum = OffComMap.get(o.Id);
  
    for (Finance_commission__c FinCom : Trigger.new) {

if (FinCom.Finance_Commission_Institution__c == 'St George' && FinCom.Finance_Commission_Type__c == 'Upfront') {

    o.St_George_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);

    }else if (FinCom.Finance_Commission_Institution__c == 'DMS' && FinCom.Finance_Commission_Type__c == 'Upfront') {


    o.DMS_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);
   
    }else if (FinCom.Finance_Commission_Institution__c == 'AFG' && FinCom.Finance_Commission_Type__c == 'Upfront') {


    o.AFG_Upfront_Commission__c = PaymentSum;
    OffComToUpdate.add(o);


  }

  update OffComToUpdate;
}
}
}