0

I have a custom object called Prospect_RTA_c, I get the data back as a single list of the Prospect_RTA_c object.

If this prospect doesn't exist I want to insert a new record for this which works.

However, it only works when all fields are filled in.

Is it possible when querying the data to have them set at a default value (So, they are not equal to null)?

LIST<Prospect_RTA__c> listPPI = new LIST<Prospect_RTA__c>();
  listPPI = [select Name,leadRecordID__c  FROM Prospect_RTA__c WHERE leadRecordID__c = :recordId];        

   if(listPPI.size() == 0){
     Lead ld = new Lead();
     ld = [select Cl_Date_Of_Birth__c, Client_Global_Postcode__c, Defendant_Name__c, Details__c, Client_Global_Email__c, Enquiry__c, FirstName, Form_Of_Contact__c, LastName, LeadLang__c, MobilePhone, Source_Details__c, Source__c, Title, work_type__c FROM Lead WHERE Id = : recordId];



     Prospect_RTA__c ppi = new Prospect_RTA__c();



     ppi.Cl_Date_Of_Birth__c = ld.Cl_Date_Of_Birth__c;
     ppi.Client_Global_Postcode__c = ld.Client_Global_Postcode__c;   
     ppi.Defendant_Name__c = ld.Defendant_Name__c;
     ppi.Details__c = ld.Details__c;   
     ppi.Enquiry__c = ld.Enquiry__c;//
     ppi.Client_Global_Forename__c = ld.FirstName;
     ppi.Form_Of_Contact__c = ld.Form_Of_Contact__c;
     ppi.Client_Global_Name__c = ld.LastName;
     ppi.LeadLang__c = ld.LeadLang__c;
     ppi.Client_Global_Tel_Mobile__c = ld.MobilePhone.replaceAll('[^0-9]', '');
     ppi.Source_Details__c = ld.Source_Details__c;
     ppi.Source__c = ld.Source__c;
     ppi.Client_Global_Title__c = ld.Title;
     ppi.Work_Type__c = ld.work_type__c;
     ppi.Name  = ld.FirstName + ' ' + ld.LastName;
     ppi.leadRecordID__c = recordId; 
     ppi.Client_Global_Email_Main__c = ld.Client_Global_Email__c;

   insert ppi;
  • What do you mean by "it only works when all fields are filled in"? You do have a couple of potential NullPointerException points here, but it's not clear what you're asking from the description. – David Reed Mar 26 at 11:47
0

it is not possible when querying the data to have them set at a default value

you have to have null checks to reduce code you can try ternary operator on fields (condition)?true:false

List<Lead> ld = [select Cl_Date_Of_Birth__c, Client_Global_Postcode__c, 
                 Defendant_Name__c, Details__c, Client_Global_Email__c, Enquiry__c, 
                 FirstName, Form_Of_Contact__c, LastName, LeadLang__c, MobilePhone, 
                 Source_Details__c, Source__c, Title, work_type__c FROM Lead WHERE Id 
                 = : recordId];
if(ld.size()>0){
 Prospect_RTA__c ppi = new Prospect_RTA__c();
 ppi.Cl_Date_Of_Birth__c = (ld.Cl_Date_Of_Birth__c!=null)?ld.Cl_Date_Of_Birth__c:'';
 ppi.Client_Global_Postcode__c = (ld.Client_Global_Postcode__c!=null)?ld.Client_Global_Postcode__c:'';   
  //rest of the fields
 insert ppi;
}

Your Answer

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