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
Steve CairneySteve Cairney 

IF isNotEmpty insert?

Hi, I'm working on creating records using a visualforce page.

I would like the option of creating more than one record (of the same type) if neccessary by adding variables to the class and having declaring them in different columns on the page. It works well, in fact too well! Even if the field is NULL it will create the record.

Here's the class code
public class CreateIABookingController{
    public ItsApproved_Booking__c ItsApprovedBooking {get; set;} //Name the objects as a variables
    public ItsApproved_Product__c ItsApprovedProduct {get; set;}
        //test variables for columns
            public ItsApproved_Product__c ItsApprovedProduct2 {get; set;}
            public ItsApproved_Product__c ItsApprovedProduct3 {get; set;}
        //end of test variables
    
    public ItsApproved_Design__c ItsApprovedDesign {get; set;}
    public ItsApproved_Delivery__c ItsApprovedDelivery {get; set;}
    private string OpportunityId;
    public CreateIABookingController()
    {
        opportunityId = ApexPages.currentPage().getParameters().get('oppId');
        ItsApprovedBooking = new ItsApproved_Booking__c();
        ItsApprovedProduct = new ItsApproved_Product__c();
            //test variables for columns
                ItsApprovedProduct2 = new ItsApproved_Product__c();
                ItsApprovedProduct3 = new ItsApproved_Product__c();
            //end test variables for columns
        ItsApprovedDesign = new ItsApproved_Design__c();
        ItsApprovedDelivery = new ItsApproved_Delivery__c();
    }
    
    public PageReference Save()
    {
        ItsApprovedBooking.Opportunity__c = opportunityId;
        insert ItsApprovedBooking;
        ItsApprovedProduct.Booking_Reference__c = ItsApprovedBooking.Id; //insert the reference field for the parent and declare variable id
        ItsApprovedProduct.Opp__c = opportunityId; //relate the Opp to the Product
        insert ItsApprovedProduct;
            //test for columns
                ItsApprovedProduct2.Booking_Reference__c = ItsApprovedBooking.Id;
                ItsApprovedProduct2.Opp__c = opportunityId;
                insert ItsApprovedProduct2;
                    ItsApprovedProduct3.Booking_Reference__c = ItsApprovedBooking.Id;
                    ItsApprovedProduct3.Opp__c = opportunityId;
                    insert ItsApprovedProduct3;
            //end test for columns
            
        ItsApprovedDesign.Opportunity__c = opportunityId; //relate the Opp to the Design
        ItsApprovedDesign.Product_Reference__c  = ItsApprovedProduct.Id;
        ItsApprovedDelivery.Product__c = ItsApprovedProduct.Id;
        insert ItsApprovedDesign;
        ItsApprovedDelivery.ItsApproved_Design__c  = ItsApprovedDesign.Id;
        ItsApprovedDelivery.Opportunity__c = opportunityId; //relate the Opp to the Delivery
        insert ItsApprovedDelivery;
        
        return new PageReference ('/' + opportunityId);
    }
}

What I'd like to do is add something to the class that will ignore the insert of ItsApprovedProduct2, ItsApprovedProduct3 if they are NULL. I'm guessing that might be an IF isNotEmpty statement around the {get;set;} of the variables? I'm not too sure on the syntax though...
 
Best Answer chosen by Steve Cairney
pconpcon
You can simply say
 
if (ItsApprovedProduct2 != null) {
    // field assignment and insert code

    if (ItsApprovedProduct3 != null) {
        // field assignment and insert code
    }
}

All Answers

pconpcon
You can simply say
 
if (ItsApprovedProduct2 != null) {
    // field assignment and insert code

    if (ItsApprovedProduct3 != null) {
        // field assignment and insert code
    }
}
This was selected as the best answer
Steve CairneySteve Cairney
Thanks Pcon I added the following to the class
 
//test for columns with if statements for null fields
                if (ItsApprovedProduct2 != null) {
                ItsApprovedProduct2.Booking_Reference__c = ItsApprovedBooking.Id;
                ItsApprovedProduct2.Opp__c = opportunityId;
                insert ItsApprovedProduct2;
                
                    if (ItsApprovedProduct3 != null) {ItsApprovedProduct3.Booking_Reference__c = ItsApprovedBooking.Id;
                    ItsApprovedProduct3.Opp__c = opportunityId;
                    insert ItsApprovedProduct3;
                    }
                }
            //end for columns with if statements for null fields

However, when I test it, it doesn't seem to work. Perhaps the fields aren't NULL before? They're actually a PICKLIST
 
<apex:pageBlockSection columns="3" title="Product Details">
                <apex:inputField value="{!ItsApprovedProduct.Product_Code__c}" label="Product Code: "/>
                <apex:inputField value="{!ItsApprovedProduct2.Product_Code__c}" label="Product Code: "/>
                <apex:inputField value="{!ItsApprovedProduct3.Product_Code__c}" label="Product Code: "/>
            </apex:pageBlockSection>

They look like this on the VF page

User-added image

I tried using the code
 
if (ItsApprovedProduct3 != '-None') {ItsApprovedProduct3.Booking_Reference__c = ItsApprovedBooking.Id;
                    ItsApprovedProduct3.Opp__c = opportunityId;
                    insert ItsApprovedProduct3;

But that throws up an error

Comparison arguments must be compatible types: ItsApproved_Product__c, String at line 38 column 25    

So it looks like I'm going to have to add the 
ItsApprovedProduct3.Product_Code__c in there somwhere. I'll run some tests


 
Steve CairneySteve Cairney
Yep, I han't had my coffee yet. The class wasn't looking at the right field. This is the working now. Thanks for the tip!
 
if (ItsApprovedProduct2.Product_Code__c != null) {
                ItsApprovedProduct2.Booking_Reference__c = ItsApprovedBooking.Id;
                ItsApprovedProduct2.Opp__c = opportunityId;
                insert ItsApprovedProduct2;