You need to sign in to do that
Don't have an account?

System.QueryException: List has no rows for assignment to SObject
Trigger AttachTrig on Attachment (after delete, after insert, after update) { folder f=[select id,name,type from folder where name='xxx' and type='document' limit 1]; list<document> docc=[select name,body,description,folderid,bodylength from document where folderid=:f.id ]; list<contact> con=[select id from contact where recordtypeid=:[select id from recordtype where name='abc']]; if(trigger.isInsert){ document doc= new document(); attachment attach=[select name,description,body,parentid from attachment where id=:trigger.new and parentid in :con]; contact c=[select id,Cn__c from contact where id=:attach.parentid]; doc.name=attach.name; doc.body=attach.body; doc.folderid=f.id; doc.keywords=c.Cn__c; insert doc; } }
some times its works perfectly....some times above code cause an error like System.QueryException: List has no rows for assignment to SObject
please help me for better coding.....
Which line(s) do you see the error being thrown from?
vel123, Assigning a query to a Object is not a good practice. Always assign query to list and use 0th value from that list if the query retrieves any value.
Example:
This line cause an error...
This would help
Always do assignment to a list if you are not 100% sure that System would return you that record.
http://forums.sforce.com/t5/Apex-Code-Development/APEX-List-has-no-rows-for-assignement-to-SObject-problem/m-p/66796