0

I am new to apex. I have two objects blood_bank__c and patient__c. I want to change quantity of blood in blood bank after patient is given some blood. This trigger is not working, and its not allowing me to add new patient.

trigger :

trigger bloodcalTrigger on patient__c ( after update, after insert) {

patient__c tok = [ Select blood_quantity_used__c, Blood_group_del__c From patient__c];
blood_bank__c cons = [Select quantity__c, name From blood_bank__c];


          if(tok.Blood_group_del__c == cons.name )  {
   cons.quantity__c = cons.quantity__c - tok.blood_quantity_used__c ;

   }else {}

}
2
  • What is the relationship between these two objects? What type of field is Blood_group_del__c? Can you please update the question with these details.
    – Nick Cook
    Sep 13, 2016 at 9:45
  • I would create an object that is detail to blood bank and patient called transfusions. Then use roll up summaries and formulas. Having a single field on patient does not seem sufficient as it is only good for a single transfusion and provides no detail. Suggest you look at object model before trigger. You may also want another object named donations related to blood bank
    – Eric
    Sep 13, 2016 at 12:30

1 Answer 1

0

You are not having any DML statement that your blood_bank__c record is updated.

Try

cons.quantity__c = cons.quantity__c - tok.blood_quantity_used__c ; update cons;

However, you assume that you only have one blood bank in the system. You should try to also update

blood_bank__c cons = [Select quantity__c, name From blood_bank__c];

to something like

blood_bank__c cons = [Select quantity__c, name From blood_bank__c WHERE name = :tok.Blood_group_del__c];

If you need more help, we need additional information about your data structure.

2
  • I tried this, but it doesnt work. Moreover .... After my triggger is active, I cant add new patient to database. it shows an error saying- "Error: Invalid Data. Review all error messages below to correct your data. Apex trigger bloodcalTrigger caused an unexpected exception, contact your administrator: bloodcalTrigger: execution of AfterInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject: Trigger.bloodcalTrigger: line 6, column 1" Sep 14, 2016 at 7:52
  • This is exactly what I meant by "However, you assume that you only have one blood bank in the system." Your query returns more than one result, and you are trying to assign multiple results to one sObject. We need more information about your data structure - how many blood banks are in the system? Is it possible that multiple blood banks have the same name?
    – fat32
    Sep 14, 2016 at 9:07

You must log in to answer this question.

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