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
raghu123raghu123 

Trigger not inserting record

Hi Guys,

                 I Created a trigger on  SalesOrder__c . that should create new record of SalesOrderLine__c when RecordType is ReturnOrder.

 

trigger insertsalesorderline on SalesOrder__c (after insert) {

  for(SalesOrder__c so:Trigger.new)
  {
   if(so.RecordType.Name =='Return_Order')  
    {
    SalesOrderLine__c sol = new SalesOrderLine__c(SalesOrder__c=so.id,Product__c='500PTU',
                                  OrderQuantity__c=3);
                      insert sol;
                      }
                      else
                      {
                      }
                      }
   }

      I am unable to insert salesorder line record

      Appreciate Your Help,

     Thank You

Best Answer chosen by Admin (Salesforce Developers) 
hisrinuhisrinu

id id1 = Schema.SObjectType.SalesOrder__c.getRecordTypeInfosByName().get('Return Order').getRecordTypeId();

 

I have executed this and I am able to retrieve the recordtype id correctly, I think you might have given wrong recordtype.

 

Validate whether the object name and Recordtype name(label) is correct or not?

All Answers

bob_buzzardbob_buzzard

I wouldn't expect the object graph will not be filled when you are processing the objects in the trigger, so dereferences like RecordType.Name will return null.

 

You'll need to lookup the record type name based on the id in your salesorder.  Something like:

 

 

trigger insertsalesorderline on SalesOrder__c (after insert) {

  Id rtId=[select id from RecordType where sobjecttype='SalesOrder__c'
           and name = 'Return_Order'].id;
  for(SalesOrder__c so:Trigger.new)
  {
   if(so.RecordTypeId ==rtId)  
    {
    SalesOrderLine__c sol = new SalesOrderLine__c(SalesOrder__c=so.id,Product__c='500PTU',
                                  OrderQuantity__c=3);
                      insert sol;
                      }
                      else
                      {
                      }
                      }
   }

 

 

EnthEnth

And for governor limit reasons you'll want to remove that insert sol from from for-loop and add the record to a List, then insert the list in a single operation after you've looped through the trigger records.

raghu123raghu123

Thanx for your reply Bob,

 

      i tried to insert  SalesOrder__c  record. getting error

     System.QueryException: List has no rows for assignment to SObject: Trigger.insertsalesorderline: line 3, column 11

   

     Thank You

hisrinuhisrinu

id id1 = Schema.SObjectType.SalesOrder__c.getRecordTypeInfosByName().get('Return Order').getRecordTypeId();

 

Instead of querying you can make use of describe calls... try this one, verify the recordtype name

raghu123raghu123

Hi Srini,

               Thanx 4 ur reply. Getting  below Error.

 

Method does not exist or incorrect signature: [Schema.DescribeSObjectResult].getRecordTypeInfo​sByName() at line 6 column 14

Thank You

Imran MohammedImran Mohammed

You should update the code to the below line.

SalesOrder__c.SObjectType.getDescribe().getRecordTypeInfosByName().get('Return Order').getRecordTypeId();

 

Let me know if any issues faced.

raghu123raghu123

Hi Imran,

                  Thanx for ur reply....Getting Same error..:((     Here is the code

 

 

trigger insertsalesorderline on SalesOrder__c (after insert) {
  List<SalesOrderLine__c> sol = new List<SalesOrderLine__c>();
 
  Id rtId = SalesOrder__c.SObjectType.getDescribe().getRecordT​ypeInfosByName().get('Return_Order').getRecordTypeId();
  
  for(SalesOrder__c so:Trigger.new)
  {
   if(so.RecordTypeId == rtId)  
    {
    sol.add(new SalesOrderLine__c(SalesOrder__c=so.id,Product__c='​5000 PTU',
                                  OrderQuantity__c=3));
                
                      }
                      else
                      {
                      }
                      }
       Database.insert(sol);
   }

   Appreciate your help

   Thank You

DhairyaDhairya

Here you also would like to consider record type while inserting the recrord?

raghu123raghu123

Hey  Dhairya,

                            after inserting SalesOrder__c , if Record type is ReturnOrder. SalesOrderLine__c should create automatically with those fields....:))

hisrinuhisrinu

id id1 = Schema.SObjectType.SalesOrder__c.getRecordTypeInfosByName().get('Return Order').getRecordTypeId();

 

I have executed this and I am able to retrieve the recordtype id correctly, I think you might have given wrong recordtype.

 

Validate whether the object name and Recordtype name(label) is correct or not?

This was selected as the best answer
raghu123raghu123

Hi Srinu.

                 Thanx for your reply.  I entered wrong recordtype label name......its working

 

Thank You All.....

Happy ThanksGiving..:))