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
BrabasBrabas 

TRIGGER HELP - Creating a New Record in Custom Object

Greetings.

I have a custom object with a trigger after insert type, this object is called Policy__c once saved you must create a new custom object, Cetificate__c, the problem is that the trigger executes, but not created Certificate__c.

trigger applyCertificadosTrigger on Policy__c (after insert) {
   Policy__c[] policies = Trigger.new;
   createCert.applyCert(policies);

} 


public class createCert{


public static void applyCert(Policy__c[] policies ) {
 integer  cont=0;
 integer sip=policies.size(); 
 
   for (cont=0; cont<sip; cont++ ){
 
       Certificate__c cert= new Certificate__c(
                                        Comission__c=30.00,
                                        Description__c='123456'

                                             );
    insert cert;

}
 
}

}

 thanks.....

 

I tried the code Imutsav, not throw exceptions, I can' t  just insert the certificate, perform a test with the example http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_trigger.htm
for object Policy__c, this worked, just not inserted.  I wish to attach the certificate in the policy.

 

thanks....

 

Best Answer chosen by Admin (Salesforce Developers) 
imutsavimutsav

Try this

trigger applyCertificadosTrigger on Policy__c (after insert) {
createCert.applyCert(Trigger.new.size());

}


public class createCert{


public static void applyCert(Integer i) {
integer cont=0;
List<Certificate__c> certificates = new List<Certificates__c>();

for (cont=0; cont<i; cont++ ){

Certificate__c cert= new Certificate__c(
Comission__c=30.00,
Description__c='123456');
certificates.add(cert);


}
if(certificates!=NULL && certificates.size()>0) {
try {
insert certificates;
}catch(Exception e) {
System.debug('----There was a exception in inserting certificates ' + e);
}
}
}
}

 

You need to remove your insert statement from your loop.

Give Kudos and mark this answer as solution if this answer helped you.

 

Thanks

Utsav

All Answers

imutsavimutsav

Try this

trigger applyCertificadosTrigger on Policy__c (after insert) {
createCert.applyCert(Trigger.new.size());

}


public class createCert{


public static void applyCert(Integer i) {
integer cont=0;
List<Certificate__c> certificates = new List<Certificates__c>();

for (cont=0; cont<i; cont++ ){

Certificate__c cert= new Certificate__c(
Comission__c=30.00,
Description__c='123456');
certificates.add(cert);


}
if(certificates!=NULL && certificates.size()>0) {
try {
insert certificates;
}catch(Exception e) {
System.debug('----There was a exception in inserting certificates ' + e);
}
}
}
}

 

You need to remove your insert statement from your loop.

Give Kudos and mark this answer as solution if this answer helped you.

 

Thanks

Utsav

This was selected as the best answer
Rahul SharmaRahul Sharma
It seems your trigger is not active, as code seems Ok to me.

Note: And you have a DML statement inside a for loop which is a bad practice. use a list to collect the records and insert them outside of the for loop.

Don't you want to attach the certificates to the respective policies?
BrabasBrabas

It works.
By failing to establish a relationship of policies with the certificates, they floated, adding that the recent items bar only appeared the policies and certificates gave the impression of not saved, but finally solved, thanks.