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
lapaullapaul 

Create a new record for a custom object

Hello,

I just created a new custom object and wrote a small Apex code attempting to create/insert a new record. However I got the error message that says "List has no rows for assignment to SObject". What did I do wrong? Please help.

Any sample codes related to this issue will be appreciated.

 

thanks,

Paul

 

Imran MohammedImran Mohammed

I think you have your code of this format.

MyCustomObject mc = <<Query>>;

Then you may be accessing mc.<<FieldName>>;

The problem here is if there are no rows assigned, then it will throw the error you are getting.

 

USe this format,

List<MyCustomObject> mc = <<Query>>;

Now check,

if(mc.size() > 0)

{

 //your code

}

 

Let me know if you are still facing the problem and post your code so that it will be helpful to identify the problem..

 

lapaullapaul

Hi Imran,

You are right. I was using this format MyCustomObject mc = <<Query>>;

Now I changed to the code you suggested but I got different error message that says "

8:36:55.123|EXCEPTION_THROWN|[473,13]|System.NullPointerException: Attempt to de-reference a null object"

Basically I just want to update the record if it is found otherwise create a new record. Thanks

 

here's my code:

 public void saveRec(){
    
    User u = [Select Department, Name, Username From User Where id = :UserInfo.getUserId()];
    String dept = u.Department;
    String uname = u.Name;
    String uloginname = u.Username;

    List<CrescentOnlineTests__c> Results = [Select UserName__c From CrescentOnlineTests__c Where UserName__c  = :uloginname];
   
    System.debug('IdentityTheftController/saveRec/Results: ' + Results);
   
    if (Results.size() > 0){
     IDTheft.UserName__c = uloginname;
        IDTheft.DatePassed__c = certDate;
        update IDTheft;
      }
    else {
     insert IDTheft;
      IDTheft.UserName__c = uloginname;
        IDTheft.DatePassed__c = certDate;
     update IDTheft;
     
       }
   }

 

Imran MohammedImran Mohammed

 

Please make below changes to the code

public void saveRec(){
    String dept ;
    String uname;
    String uloginname;


    User[] uList = [Select Department, Name, Username From User Where id = :UserInfo.getUserId()];

   if(uList.size() > 0)

   {
     dept = uList[0].Department;
     uname = uList[0].Name;
     uloginname = uList[0].Username;

  

    List<CrescentOnlineTests__c> Results = [Select UserName__c From CrescentOnlineTests__c Where UserName__c  = :uloginname];
   
    System.debug('IdentityTheftController/saveRec/Results: ' + Results);
   
    if (Results.size() > 0){

    //One question here, are you instantiating IDTheft or where are you getting IDTheft from. 
     IDTheft.UserName__c = uloginname;
        IDTheft.DatePassed__c = certDate;
        update IDTheft;
      }
    else {

    // Without setting any details to IDTheft you are inserting.Where are you creating this IDTheft

    //For ex it should be like <<CustomObject>> IDTheft = new <<CustomObject>>();
     insert IDTheft;// Remove this IDTheft from here because below you are setting some value to it.
      IDTheft.UserName__c = uloginname;
        IDTheft.DatePassed__c = certDate;
     update IDTheft;//remove this statement and add insert IDTheft; here
     
       }

 

    }//end of if(uList.size() > ....
   }

 

Let  me know if you have any queries

lapaullapaul

The Insert command works now. You are right. I need to instantiate this statement like the following. I did not do it right in the beginning. 

CrescentOnlineTests__c

IDTheft = new CrescentOnlineTests__c();

 

However now my update command is still not working. I got the error message below. Please advice.

 

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

 

Class.IdentityTheftController.saveRec: line 468, column 9

 

 

 here's my code. Now the insert works but update is not working. I got the above error message. The error caused by the line bolded below (update IDTheft);

 

  public void saveRec(){
    
    User u = [Select Department, Name, Username From User Where id = :UserInfo.getUserId()];
    String dept = u.Department;
    String uname = u.Name;
    String uloginname = u.Username;
    System.debug('IdentityTheftController/saveRec/dept/uname/uloginname: ' + dept + ' ' + uname + ' ' + uloginname);
   
    List<CrescentOnlineTests__c> Results = [Select UserName__c From CrescentOnlineTests__c Where UserName__c  = :uloginname];
   
    System.debug('IdentityTheftController/saveRec/Results/certDate: ' + Results + ' ' + certDate);
   
    if (Results.size() > 0){
        IDTheft.UserName__c = uloginname;
        IDTheft.DatePassed__c = certDate;
        update IDTheft;
      }
    else {
     insert IDTheft;
      IDTheft.UserName__c = uloginname;
        IDTheft.DatePassed__c = certDate;
     update IDTheft;
     
       }
   }

 

  

 

 

Imran MohammedImran Mohammed

Can you post the code where you are defining IDTheft?

 

Before updating any record, the record should be retrieved through SOQL.

Did you make a query which is assigned to IDTheft?

 

I think your code should be like this

    if (Results.size() > 0){

       IDTheft = Results[0];
        IDTheft.UserName__c = uloginname;
        IDTheft.DatePassed__c = certDate;
        update IDTheft;
      }

 

 

lapaullapaul

Thanks so much. The update works now with this statement IDTheft = Results[0];

Everything seems to work as expected now.

I have a minor issue problably you can help me with this too. How come I use the following date format but display incorrect date.  It displays like 40/30/2010. Basically the month is incorrect.  Any ideas? Thanks.

Datetime.now().format('mm/dd/yyyy')

Imran MohammedImran Mohammed

Use Datetime.now().format('MM/dd/yyyy').

This will work.

If you see this works make the thread as Accepted solution.