0
public class BookingCltr {
    @AuraEnabled
    public static void CreateRecord(String conObj, String bookObj){
        contactObject con = (contactObject)JSON.deserialize(conObj, contactObject.class);
        bookingObject book = (bookingObject)JSON.deserialize(bookObj, bookingObject.class);

        Contact c = new Contact();
        c.FirstName =con.FirstName;
        c.LastName = con.LastName;
        c.Email = con.Email;

        Contact contacts=[select Id,Email,FirstName,LastName from Contact where email = :c.Email limit 1];

        if(contacts == null)

        {
            insert c;

            Booking__c b = new Booking__c();
            b.Contact__c = c.Id;
            b.Hotel__c = book.Hotel;
            b.Check_In__c = book.startDate;
            b.Check_Out__c = book.endDate;
            b.First_Name__c=c.FirstName;
            b.Last_Name__c=c.LastName;
            insert b;

        }
        else{
            Booking__c b = new Booking__c();
            b.Contact__c = contacts.Id;
            b.Hotel__c = book.Hotel;
            b.Check_In__c = book.startDate;
            b.Check_Out__c = book.endDate;
            b.First_Name__c=con.FirstName;
            b.Last_Name__c=con.LastName;
            b.Email__c=contacts.Email;
            insert b;
        } 
    }

    public class contactObject{
        @AuraEnabled public string FirstName {get; set;}
        @AuraEnabled public string LastName {get; set;}
        @AuraEnabled public String Email {get; set;}
    }

    public class bookingObject{
        @AuraEnabled public String Hotel {get; set;} 
        @AuraEnabled public Date startDate {get; set;} 
        @AuraEnabled public Date endDate {get; set;} 
    }
}
0

The line

Contact contacts=[select Id,Email,FirstName,LastName from Contact where email = :c.Email limit 1];

will throw a System.QueryException: List has no rows for assignment to SObject. So I think there is a contact record with that email Id.

Also just a suggestion since queries don't produce a null list please replace the above query and logic to something like this

List<Contact> con = [select Id,Email,FirstName,LastName from Contact where email = :c.Email];

if(con.IsEmpty()) {

}else {

}

I hope this helps.

4
  • - Save button will create a new contact if email address doesn't match or connect with existing contact in case of email found. Create record for booking on specified hotel, contact. – John Oct 11 '19 at 6:23
  • this is my requirement – John Oct 11 '19 at 6:23
  • after doing this ,I am getting errror in else condition that variable id and email not exist – John Oct 11 '19 at 6:44
  • yes it will throw error because the contacts is empty and you can't use id or email or any field since its empty – summer Oct 11 '19 at 8:41
0

I guess that this test never fails:

    if(contacts == null){

    }

Since you instantiate the contact:

    Contact c = new Contact();

So even if in your Json something like id/email is missing or even if your query fails, still you have a virtually non-empty contact. You can solve it declaring a list and check if its size is >0. Try it:

List<Contact> n = [select id from contact where email = 'somethingthatdoesnotexists'];

if(n != null){
        system.debug('list isnt null, but the size is 0',n.size());

}
0

The reason being query doesnt return null, it will return empty list. So the "if condition contact == null" would never work.

Also, please ensure that whenever you query, result of query should be stored in List not in SObject. The reason being if you store it in SObject and query doesnt return any row then you will end up facing below exception. System.QueryException: List has no rows for assignment to SObject

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.