+ Start a Discussion
rajesh kumar 10rajesh kumar 10 

I have facing a problem on trigger that..

I have to to write a trigger on object Primary application allocation that is related to opportuniity and 
when atlease 1 record is created, set the opportunity.primary_application_allocation_set check mark = true
and
And when all the primary application records are deleted, set the opportunity.primary_application_allocation_set check mark = false ..

I was facing problem with this to write a trigger. so please can any one help on this..
Boris BachovskiBoris Bachovski
You're basically asking someone else to do your job. I'm totally ok if there are volunteers out there that want to help, though I'd suggest you to start learning the basics and to put some more effort into this, then reach out if you're stuck or you need help with something specific.

There is a lot of material out there regarding how to write a trigger and best practices, so it'll be worthwhile if you start digging deeper and learn something more.

Here are a few links that can get you started:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_trigger.htm
http://techman97.wordpress.com/2012/07/18/how-to-write-a-basic-apex-trigger/
https://developer.salesforce.com/page/Apex_Code_Best_Practices
CJWilderCJWilder
I threw this together in a text editor may not work but will give you something to go by along with the links Boris provide.
trigger PrimaryTrigger on Primary (after insert, after delete) {

	List <Id> lstOpps = new List<Id>();
	for(Primary otm : Trigger.New){
        	lstOpps.add(otm.OpportunityId);
        }               

        List<Opportunity> Opps = new List<Opportunity>([SELECT primary_application_allocation_set FROM Opportunity where Id in :lstOpps]); 


	if(Trigger.isInsert){
		for(Primary otm : Trigger.New){
			for (Opportunity atm : Opps){
                		If (otm.OpportunityId == atm.Id){
                 			otm.primary_application_allocation_set = True;
				}
                	}
	        }
		update Opps;
	}

	if(Trigger.isDelete){
		for(Primary otm : Trigger.New){
			for (Opportunity atm : Opps){
                		If (otm.OpportunityId == atm.Id){
                 			otm.primary_application_allocation_set = False;
				}
                	}
		}
		update Opps;
	}


}


rajesh kumar 10rajesh kumar 10
Hi CJWilder

when i am using the trigger that when i am inserting the records field is updating to true but when i delete atleast one record then it is updating to false.
But i want when all records are deleted which are are related to that opportunity then only it should be update to false..
But now i was facing when atleast one recordd is deleted then it is updating to false.
So can u please solve my problem.. Below is the Trigger

trigger UpdatePrimaryApllicationAllocationSet on Primary_Application_Allocation__c (after insert,after delete) {

    list<id> lst = new list<id>();
    if(trigger.isInsert){
    for(Primary_Application_Allocation__c paa : trigger.new) {
        lst.add(paa.opportunity__c);
    }}
    if(trigger.isDelete){
        for(Primary_Application_Allocation__c paa : trigger.old) {
        lst.add(paa.opportunity__c);
        }
    }
   
    list<Opportunity> opplst = new list<Opportunity>();
    opplst=[SELECT Primary_Application_Allocation_Set__c FROM Opportunity where Id in :lst];

    if(trigger.isInsert) {
        for(Primary_Application_Allocation__c paa : trigger.new) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.Id) {
                    o.Primary_Application_Allocation_Set__c = true;
                }
            }
        }
        update opplst;
    }
   
    if(trigger.isDelete) {
        for(Primary_Application_Allocation__c paa : trigger.old) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.id) {
                    o.Primary_Application_Allocation_Set__c = False;
                }
            }
        }
        update opplst;
    }
           
}
CJWilderCJWilder
you may could try something like below

if(trigger.isDelete) {
        for(Primary_Application_Allocation__c paa : trigger.old) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.id) {
			Integer counter = [ Select count() from Primary_Application_Allocation__c where ID = :o.id];
			if counter = 1 {
				o.Primary_Application_Allocation_Set__c = False;
			}
 		}
            }
        }
        update opplst;
}