+ Start a Discussion
Philip MempinPhilip Mempin 

Try and Catch exception

Hi,

I am trying to create a trigger to update fields into opportunity object from Recurring Donation object only if the Recurring Donation record exist otherwise it will not take any action. I am new to Apex coding and I saw the "try" and "catch" exception. How can I incorporate these coding for SOQL like "Select ID from npe03_recurring_donation__c where name = 'as defined'".

Thanks for your help.
Philip
Best Answer chosen by Philip Mempin
RAM AnisettiRAM Anisetti
Hi Philip,

Could you refere below code and update ur code with ur objects and fields(use relationship field carefully)

 
trigger updateopptrigger on opportunity(before Insert,before Update) 
{

/*
  Parent : Position__c //here you can replace ur recurring_donation__c
  Child  : opportunity  // in your case same
  
*/

List<ID>pids=new List<ID>(); //pids holds parent ids by using relationship field(Position__c)between two objects,in your case this field is different so plz check in opportunity fields

for(opportunity op:trigger.new){
    
    if(op.Position__c!=null){
	//Position__c is relationship field
     pids.add(op.Position__c);
     
     }
}

//here mention all the fields you want to import to opportunity from ur object recurring_donation__c

List<Position__c>parentvalues=[select id,name,Job_Description__c from Position__c where id IN:pids];

if(trigger.isInsert && trigger.isBefore){

for(opportunity op2:trigger.new){

   for(Position__c p:parentvalues){
       
	   //here use ur critiria i mean in place of testRam
       if(op2.Position__c==p.id && p.name=='testRam'){
          //fetch the values from recurring_donation__c and insert to opportunity
		  //here i was update  opportunity Description using parent Job_Description__c value
          op2.Description=p.Job_Description__c; 
       }
   }  
}

}

if(trigger.isUpdate && trigger.isBefore){

for(opportunity op3:trigger.new){

   for(Position__c p1:parentvalues){
   
       if(op3.Position__c==p1.id && p1.name=='testRam'){
       
          op3.Description=p1.Job_Description__c; 
       }
   }  
}
}

}


 

All Answers

RAM AnisettiRAM Anisetti
Hi Philip,

can you provide me what relationship exist between opportunity and Recurring Donation objects.
Philip MempinPhilip Mempin
Hi Ram,
Thanks for your reply.
Recurring Donation  can have one or more Opportunity records. There are fields in Recurring Donation object I need to fetch into the Opportunity record before update/insert of Opportunity object. But I need to do fetch it only on certain criteria like if Recurring Donation name is a certain value and do not do any action if the value of the Recurring Donation field name does not exit.
Thanks,
Philip 
RAM AnisettiRAM Anisetti
Hi Philip,

Could you refere below code and update ur code with ur objects and fields(use relationship field carefully)

 
trigger updateopptrigger on opportunity(before Insert,before Update) 
{

/*
  Parent : Position__c //here you can replace ur recurring_donation__c
  Child  : opportunity  // in your case same
  
*/

List<ID>pids=new List<ID>(); //pids holds parent ids by using relationship field(Position__c)between two objects,in your case this field is different so plz check in opportunity fields

for(opportunity op:trigger.new){
    
    if(op.Position__c!=null){
	//Position__c is relationship field
     pids.add(op.Position__c);
     
     }
}

//here mention all the fields you want to import to opportunity from ur object recurring_donation__c

List<Position__c>parentvalues=[select id,name,Job_Description__c from Position__c where id IN:pids];

if(trigger.isInsert && trigger.isBefore){

for(opportunity op2:trigger.new){

   for(Position__c p:parentvalues){
       
	   //here use ur critiria i mean in place of testRam
       if(op2.Position__c==p.id && p.name=='testRam'){
          //fetch the values from recurring_donation__c and insert to opportunity
		  //here i was update  opportunity Description using parent Job_Description__c value
          op2.Description=p.Job_Description__c; 
       }
   }  
}

}

if(trigger.isUpdate && trigger.isBefore){

for(opportunity op3:trigger.new){

   for(Position__c p1:parentvalues){
   
       if(op3.Position__c==p1.id && p1.name=='testRam'){
       
          op3.Description=p1.Job_Description__c; 
       }
   }  
}
}

}


 
This was selected as the best answer
Philip MempinPhilip Mempin
Hi Ram,
I got it. Again many thanks for your assistance and I can proceed now in creating this trigger.

Philip