+ Start a Discussion
Sales Force FRMSales 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:
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.
            
JonPJonP
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
Sales Force FRMSales Force FRM
Hi Jon,
             I tested this one using test methods and i didn't get any errors my new class is as follows:

public class OpportunityInvoice{


public static testMethod void testIcreateOppInvoice(){
try{
String invoiceLineDesc='First Invoice Line Desc';
String Name='Best Invoice';
Opportunity_Invoice__c record= new Opportunity_Invoice__c();
system.debug('1');
record.Date__c=Date.today();
system.debug('date:'+record.Date__c);
record.Line_Description__c=invoiceLineDesc;
system.debug('Invoice description'+record.Line_Description__c);
record.Name=Name;
Opportunity_Invoice__c[] oppInvoice = new Opportunity_Invoice__c[]{record};

// Attempt to insert it...
system.debug('Name:'+record.Name);
system.debug('oppInvoice:'+oppInvoice);
insert oppInvoice ;
system.debug('Inserted:');
}catch (DmlException e) {
for (Integer i = 0; i < e.getNumDml(); i++) {
// Process exception here
System.debug(e.getDmlMessage(i));
}
// system.error('Exception is:'+e);
// This catches the OtherException
}
}
}
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.
jrotensteinjrotenstein
I agree with JonP. You don't seem to be creating an AR__c record anywhere.
Sales Force FRMSales Force FRM
My issue is :

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.
jrotensteinjrotenstein
But where is your TestMethod to test the trigger?
jrotensteinjrotenstein
Also, please note that any records created within a TestMethod are rolled-back at the end of the test. That way, your system is not polluted with test data.
Sales Force FRMSales Force FRM
Is it possible to invoke a  trigger on custom  object and insert a value in to that if so , can you suggest me how to do it.
jrotensteinjrotenstein
To invoke your AR__c trigger, create a new AR__c object. This can be done via the UI, via Data Loader or via a TestMethod.
Sales Force FRMSales Force FRM
I have created AR__c using UI , similarly Opportunity_invoice_CC.
Now when enter values of AR__c and click save then a record with few parameters entered in to Opportunity_invoice_CC.

Regards,
Rajeshwar.
jrotensteinjrotenstein
So, do you still have a problem? If so, what are you doing, what is happening and what would you like to happen?
Sales Force FRMSales Force FRM
Hi ,
       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.
jrotensteinjrotenstein
Rajeshwar,

I have implemented your code on my sandbox and is works OK.

Most important: Make sure that your Trigger is "Active":



To make things simpler, I put all the code in the Trigger, rather than a separate class:
Code:
trigger ARP_Trigger on ARP__c (before insert) {

  for(ARP__c arpc : Trigger.new){
    Opportunity_Invoice__c record = new Opportunity_Invoice__c();
    record.Date__c = arpc.Invoice_Scheduling_Date__c;
    record.Line_Description__c = arpc.Invoice_Line_Description__c;
    record.Name = arpc.Name + ' Invoice';
    
    // Insert it...
    insert record ;
  }
}

 And then I wrote a Test for the Trigger:
Code:
public class ARP_Test {

 static TestMethod void testARP() {

  System.debug('Starting Test');
  ARP__c a = new ARP__c(Invoice_Line_Description__c = 'Inv Line',
 Invoice_Scheduling_Date__c = Date.newInstance(2008, 01, 01), Name = 'ARP Test'); insert a; System.debug('ARP: ' + a.Id); Opportunity_Invoice__c o = [select Id, Date__c, Line_Description__c
from Opportunity_Invoice__c where Name = 'ARP Test Invoice']; System.assertEquals('Inv Line', o.Line_Description__c); } }

 
I then created an ARP record in the UI:



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.
Sales Force FRMSales Force FRM
Hi ,
                         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
SuperfellSuperfell
After you've created your ARP record in the UI, where are you looking for the Opportunity_Invoice__c record ? as you have no relationship between the 2 records, it won't appear in any of the related lists for the ARP object, you'll need to go to the Opportunity_Invoice__c tab and view all the records there to find the record the trigger created.
Sales Force FRMSales Force FRM
Even ARP__c object i can't see records in view part of ARP__c , for this did i need to do any layout so it can be appear in view part.
Regards,
Rajeshwar.