function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
neshnesh 

Create a New Record on Custom Object when field on Standard Object changes

Hi

    Create a New Record on Custom Object when field on Standard Object (OPPORTUNITY)changes.

when opportunity object field changes (opp.record_type__c=='One timedonations' &&  opp.StageName=='pledged' && opp.Probability==100).its automatically create a New Record on Custom Object -name :One timedonations'.when i update opp object that also update on corresponding record on onetimedonation custom object. please correct my code.

 

 

trigger createonedonationrecord on Opportunity ( after insert,AFTER UPDATE)
{
  Opportunity opp=Trigger.new[0];
 
    if( opp.record_type__c=='One timedonations' &&  opp.StageName=='pledged' && opp.Probability==decimal.valueof(100) && OPP.npe03__Recurring_Donation__c==NULL)
    {         
    
              OneTimedonation__c  td1=new OneTimedonation__c();
              
              integer tds1=[select count() from OneTimedonation__c where Is_one_time_donation_created__c=false];
              if(tds1 ==0)
              {
              //td1=[select id,Organization__c,amount__c,Donation_Types__c from OneTimedonation__c where Is_one_time_donation_created__c=false];
              td1.Is_one_time_donation_created__c=True;
              td1.amount__c =opp.Amount;
              td1.Organization__c=opp.Accountid;
              td1.Campaign__c=opp.Campaignid;
              td1.Donation_Types__c=opp.Donation_type__c;
             // tdl.onetimedonationdone__c=true;
              insert td1;
              }
         if(trigger.isupdate)
            {
           
               // OneTimedonation__c  td1=new OneTimedonation__c();
              integer cntac1 =[select count() from OneTimedonation__c where Organization__c=:opp.Accountid];
              system.debug('count'+cntac1);
              if(cntac1 ==1)
              {
              td1=[select id,Organization__c from OneTimedonation__c where Organization__c=:opp.Accountid];
              td1.amount__c =opp.Amount;
              td1.Organization__c=opp.Accountid;
              td1.Campaign__c=opp.Campaignid;
              td1.Donation_Types__c=opp.Donation_type__c;
              update td1;
            
              }
           }
          
          
     }
 }

magicforce9magicforce9

Hi nesh,

 

Your trigger needs a lot of improvements. May be you can start off with the code below.

trigger createonedonationrecord on Opportunity (after insert, after update)
{
  
	if(trigger.isInsert){

		List<OneTimedonation__c> OneTimeDonationsToBeInserted = new List<OneTimedonation__c>();
		for(Opportuniy opp : trigger.new)
		{
    		if( opp.record_type__c=='One timedonations' &&  opp.StageName=='pledged' && opp.Probability==decimal.valueof(100) && OPP.npe03__Recurring_Donation__c==NULL)
    		{
    			OneTimedonation__c donation = new OneTimedonation__c(
    										Is_one_time_donation_created__c=True,
    										amount__c =opp.Amount,
    										Organization__c=opp.Accountid,
    										Campaign__c=opp.Campaignid,
    										Donation_Types__c=opp.Donation_type__c);
    			OneTimeDonationsToBeInserted.add(donation);
    		}
    	}
    	//Here we are creating new child records upon insert
		insert OneTimeDonationsToBeInserted;
	}
    if(trigger.isupdate){
    	Map<Id, Opportunity> OppIds = new Map<Id, Opportunity>();
		for(Opportunity opt : trigger.new)
		{
			OppIds.put(opt.id, Opt);

		}  

		//Here we are updating the child records.
		//I'm assuming Organization__c is the relationship field to Opportunity Object, 
		//if its not then please change Organization__c field in the below query & subsequest field assigning statements.
        List<OneTimedonation__c> OneTimeDonationsToBeUpdated = [select id from OneTimedonation__c where Organization__c IN :OppIds.keySet()];
        for(OneTimedonation__c donation : OneTimeDonationsToBeUpdated)
        {
        	donation.amount__c = OppIds.get(donation.Organization__c).Amount;

        	//I have commented the below statement because if this is the relationship field then this doesn't make sense.
        	//donation.Organization__c = OppIds.get(donation.Organization__c).Accountid;

        	donation.Campaign__c = OppIds.get(donation.Organization__c).Campaignid;
        	donation.Donation_Types__c = OppIds.get(donation.Organization__c).Donation_type__c;
            
        }
        update OneTimeDonationsToBeUpdated;
    }
}

 

neshnesh
THANKS FOR UR HELP