You need to sign in to do that
Don't have an account?
Brabas
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....
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
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
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?
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.