3

I'm trying to insert a custom object record which contains various custom fields and also a lookup field. Is it possible to insert the record without specifying the lookup field value? I've tried inserting an empty value or null on the lookup field but it throws an System.NullPointerException: exception error.

carts.Code__c --> The Lookup field

if(carts == null) carts = new Cart__c();
      carts.rStatus__c = 'Saved';
      carts.Price__c = 125;

     if (code != null || code.length() != 0){
     carts.Code__c = [SELECT Name, code__c FROM Code__c WHERE code__c =:code].Id;
       }   else{
          carts.Code__c = '';
          }

      insert carts;

   pageRef = new PageReference('/apex/out'); 
    pageRef.setRedirect(true);            

    }catch(DmlException e) {
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Error' + e);
        ApexPages.addMessage(myMsg);

    } catch(Exception e) {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please be patience, Your request will be......' + e);
        ApexPages.addMessage(myMsg);

    } 
    return pageRef;
  • Is Code__c a required lookup field? – Daniel Ballinger Mar 22 '15 at 18:12
  • It's not a required lookup field @DanielBallinger – fb69 Mar 22 '15 at 18:14
  • What is the exception that you are getting? – Daniel Ballinger Mar 22 '15 at 18:15
  • It's System.NullPointerException: Attempt to de-reference a null object @DanielBallinger – fb69 Mar 22 '15 at 18:23
  • Which line in your code is given that exception? If it is coming from a DML operation like insert carts; then it may be a trigger expecting the Code__c field to be populated. – Daniel Ballinger Mar 22 '15 at 19:13
1

Currently the error message is System.QueryException: List has no rows for assignment to SObject.

This will most likely be coming from the line: (please indicate in your responses which line is throwing the exception)

carts.Code__c = [SELECT Name, code__c FROM Code__c WHERE code__c =:code].Id;

There are a couple of problems with this line:

  1. You are using a short hand syntax that assumes exactly one Code__c record will always be returned by this query. If no records match then you will get the QueryException for no rows.
  2. You are querying the Name and Code__c fields and then using the Id field, which isn't part of the SOQL query.

Instead, try something like:

List<Code__c> codes = [Select Id from Code__c where code__c = :code limit 1];
if(codes.size() > 0) {
    carts.Code__c = codes[0].Id;
} else {
    carts.Code__c = null;
}
0

Check if the field is required in the profile then if this is not required you could skip the field when you are creating the record. Be sure that field is not an a master detail because by default it is required.

  • noted. Lookup field is not required. it's Lookup Field. Not master details. Thanks. – fb69 Mar 22 '15 at 19:09
0

I suspect the SOQL query inside your code is not correct. Your are querying a wrong object.

carts.Code__c = [SELECT Name, code__c FROM Code__c WHERE code__c =:code].Id;

I believe it should be something like this according to the Carts object in your instance.

carts.code__C = [Select name from Code__c where name=:code].id
  • Sadly no. My problem is related to inserting an empty value in my lookup field. The solution you gave me is unrelated to my question. Thanks anw. – fb69 Mar 22 '15 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.