You need to sign in to do that
Don't have an account?
Sales Force FRM
How to insert a value in to a custom object
Hi Folks,
I have crated 2 custom objects: AR__c and Opportunity_Invoice__c
Now i nvoked a trigger on a AR__c object so once i save any record it create a new record in Opportunity_Invoice__c object .
my code is follows:
trigger:
I have crated 2 custom objects: AR__c and Opportunity_Invoice__c
Now i nvoked a trigger on a AR__c object so once i save any record it create a new record in Opportunity_Invoice__c object .
my code is follows:
trigger:
trigger ARPTrigger on AR__c (before insert) {
for(AR__c arpc : Trigger.new){
String text=' Invoice';
OpportunityInvoice.createOppInvoice(arpc.Invoice_Scheduling_Date__c,arpc.Invoice_Line_Description__c,arpc.Name+text);
}
}
and my class is follows:
public class OpportunityInvoice{
public static void createOppInvoice(Date invoiceDate,String invoiceLineDesc,String Name){
Opportunity_Invoice__c record= new Opportunity_Invoice__c();
record.Date__c=invoiceDate;
record.Line_Description__c=invoiceLineDesc;
record.Name=Name;
Opportunity_Invoice__c[] oppInvoice = new Opportunity_Invoice__c[]{record};
// Attempt to insert it...
insert oppInvoice ;
}
}
compilation successful but when i crate a record in custom object the trigger should fire and call the class
and insert record in to Opportunity_Invoice__c object but no record is inserted in to Opportunity_Invoice__c object when i created a record
in AR__c object, can u suggest me , how can make this successful.
Regards,
Rajeshwar.
First, make sure your trigger is active. Next, add System.debug() statements, create a test method on the OpportunityInvoice class that inserts an AR__c record, and run the test. You'll then be able to look in the System Log (or from the Force.com IDE, in the Apex Code Test Runner output window) to see your debug output.
Once you've got your trigger working, you'll want to rewrite it so that it only makes one insert statement for the entire batch. This is much more efficient than calling insert every time you go through the for loop, and since you can only execute 20 DML statements per Apex transaction, it means your trigger won't break when more than 20 AR__c records are inserted in a batch operation. See the Force.com Cookbook and other online Apex Code resources for help writing effective batch triggers.
Jon
I tested this one using test methods and i didn't get any errors my new class is as follows:
The log is generated as follows:
*** Beginning Test 1: OpportunityInvoice.public static testMethod void testIcreateOppInvoice()
20080529035422.609:Class.OpportunityInvoice.testIcreateOppInvoice: line 17, column 9: 1
20080529035422.609:Class.OpportunityInvoice.testIcreateOppInvoice: line 19, column 9: date:2008-05-28 00:00:00
20080529035422.609:Class.OpportunityInvoice.testIcreateOppInvoice: line 21, column 9: Invoice descriptionFirst Invoice Line Desc
20080529035422.609:Class.OpportunityInvoice.testIcreateOppInvoice: line 26, column 9: Name:Best Invoice
20080529035422.609:Class.OpportunityInvoice.testIcreateOppInvoice: line 27, column 9: oppInvoice:(Opportunity_Invoice__c:{Date__c=Wed May 28 00:00:00 GMT 2008, Line_Description__c=First Invoice Line Desc, Name=Best Invoice})
20080529035422.609:Class.OpportunityInvoice.testIcreateOppInvoice: line 28, column 9: Insert: LIST:SOBJECT:Opportunity_Invoice__c
20080529035422.609:Class.OpportunityInvoice.testIcreateOppInvoice: line 28, column 9: DML Operation executed in 27 ms
20080529035422.609:Class.OpportunityInvoice.testIcreateOppInvoice: line 29, column 9: Inserted:
20080529035422.609:Class.OpportunityInvoice: line 12, column 35: returning from end of method public static testMethod void testIcreateOppInvoice() in 30 ms
Cumulative resource usage:
Resource usage for namespace: (default)
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 100
Number of DML rows: 1 out of 500
Number of script statements: 14 out of 200000
Maximum heap size: 0 out of 500000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Total email recipients queued to be sent : 0
*** Ending Test OpportunityInvoice.public static testMethod void testIcreateOppInvoice()
Regrads,
Rajeshwar.
When i create record in AR_c(clicking on save )(custom object which has it's own screen layout ) a trigger will be fired and the a record with specified values should be inserted in to opportunity_invoice__c(custom object which has it's own screen layout ) . but
no record is inserted , i have tested my code using testmethods and in log file there are no errors(Please refer previous reply for log file).
Regards,
Rajeshwar.
Now when enter values of AR__c and click save then a record with few parameters entered in to Opportunity_invoice_CC.
Regards,
Rajeshwar.
iam able to insert values in to the opprtunity_invoice __c from ARP__c but i can't see those records in opprtunity_invoice __c object view part , but when i wrote one soql query to check whether those records are inserted or not , as per SOQL there records in opprtunity_invoice __c object but in UI part iam unable to see the records in opprtunity_invoice __c object.
Regards,
Rajeshwar.
And then I wrote a Test for the Trigger:
and used a SOQL tool to verify that a record had been written to the database:
So, you must be very close to having your Trigger work.
I tested your trigger in my sales force developer edition it is also behaving in the same way as my trigger ! As you said it is working in sand box similarly it should work in developer edition so may any dependency ? my trigger is active but still unable to see, u said in the post that you have mentioned "then created an ARP record in the UI:" it means i need to create a view explicitly (once record inserted it will not appear in view part directly, to make it appear did i need to do any thing!).
Regards,
Rajeshwar
Regards,
Rajeshwar.