1

i've three objects: branch, student registration form and application. Application have two master detail relationship with "branch" and "student registration form". In "branch" there are 60 seats and in application there is status field have values "accept" and "reject". so if i "accept" a form then a value from seats is reduce by one(-1). so what will be the trigger coding.

The code i've wrote so far:

trigger SeatsUpdate on Branch__c (after insert) { 
    List<Admission_Application__c> adapp = [ SELECT Admission_Application__c.Branch__c 
                                             FROM Admission_Application__c 
                                             WHERE Admission_Application__c.Status__c = 'Accept' 
                                             AND Admission_Application__c.Branch__c.id IN: Trigger.New FOR Update]; 
    FOR(Admission_Application__c ad: adapp){ 
        ...
    } 
}

EDIT

Please look at this revised code:

trigger SeatsUpdate on Branch__c (after insert) { 
List<Admission_Application__c> adapp=[SELECT a.Branch__r.Name FROM Admission_Application__c a WHERE a.Status__c='Accept' AND a.Branch__r.Id IN: Trigger.New FOR Update]; 
for(Admission_Application__c ad: adapp){
   ad.Branch__r.Number_of_Seats__c=ad.Branch__r.Number_of_Seats__c-1; 
 } 
 Update adapp; 
 } 
6
  • 1
    Have you started writing code? if yes then please share your code
    – doga
    May 31, 2013 at 10:48
  • trigger SeatsUpdate on Branch__c (after insert) { List<Admission_Application__c> adapp=[SELECT Admission_Application__c.Branch__c FROM Admission_Application__c WHERE Admission_Application__c.Status__c='Accept' AND Admission_Application__c.Branch__c.id IN: Trigger.New FOR Update]; FOR(Admission_Application__c ad: adapp){ } } May 31, 2013 at 10:54
  • Actually i'm new in salesforce. i don't know how to use trigger.. May 31, 2013 at 10:55
  • 1
    Maybe try reading the simple example salesforce.com/us/developer/docs/apexcode/Content/…, try and adapt this for your situation, see how it works or what error you run into
    – Doug B
    May 31, 2013 at 11:00
  • please can you tell me simple code that reduce value by 1? May 31, 2013 at 11:02

1 Answer 1

1

I've added your revised code. The first thing I see is in your query where you have FOR Update at the end of it at IN: Trigger.New FOR Update. This isn't needed and will generate a SOQL error message. You only need IN: Trigger.New. To clarify, your query only needs to look like this:

List<Admission_Application__c> adapp=[SELECT a.Branch__r.Name FROM Admission_Application__c a WHERE a.Status__c='Accept' AND a.Branch__r.Id IN: Trigger.New]; 

Here's some additional help.

for(Admission_Application__c ad: adapp){
   adapp.Branch__r.Number_of_Seats__c=ad.Branch__r.Number_of_Seats__c - 1; 
} 
Update adapp;

The way you previously had it written, you weren't making any changes to adapp, but instead to ad which weren't getting committed.

Now, here's a question for you. At any point in your workflow or validation, are you testing to see if you're exceeding the maximum number of seats available? It would seem that would be part of your validation at the registration entry or else workflow, but could be part of your trigger logic. Regardless, it needs to be included someplace in your process.

You must log in to answer this question.

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