You need to sign in to do that
Don't have an account?
raghu123
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
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
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:
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.
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
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
Hi Srini,
Thanx 4 ur reply. Getting below Error.
Method does not exist or incorrect signature: [Schema.DescribeSObjectResult].getRecordTypeInfosByName() at line 6 column 14
Thank You
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.
Hi Imran,
Thanx for ur reply....Getting Same error..:(( Here is the code
Appreciate your help
Thank You
Here you also would like to consider record type while inserting the recrord?
Hey Dhairya,
after inserting SalesOrder__c , if Record type is ReturnOrder. SalesOrderLine__c should create automatically with those fields....:))
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?
Hi Srinu.
Thanx for your reply. I entered wrong recordtype label name......its working
Thank You All.....
Happy ThanksGiving..:))