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
Satyavathi PolepallySatyavathi Polepally 

how to fix these bugs

Hi,  

My requirement is to check if the contact is already exists or not. And, if the contact already exists then I have to check if the contact is subscribed or not. To check the Subscription I have a custom object called Subscription__c. Now I need to query a custom field and check the value of that custom field  on Subscription__c from contact object.
I have written the following code but, its not working. Can somebody help me in fixing this...

KList<Contact> contact = [SELECT Id, Email FROM Contact WHERE Email=:Email];
         if(contact.size() > 0){
         List<Subscription__c> subscription = [SELECT Id,Subscription_Status__c FROM Subscription__c WHERE Contact__c = :contact.Id AND Subscription_Status__c = true Limit 1];
             if(subscription.size() > 0 && subscription.Subscription_Status__c == true){
             }


Note: Contact and Subscription__c are having master detail relationship.
 
Best Answer chosen by Satyavathi Polepally
Ajay K DubediAjay K Dubedi
Hi Satyavathi,

There are some mistakes in your code, you can't use contact.Id directly in sql-Query.So,try the below code it works in my org:
List<Contact> contactList=new List<Contact>();
        Set<Id> conIdSet=new Set<Id>();
        contactList = [SELECT Id, Email FROM Contact WHERE Email=:Email Limit 10000];
         if(contactList.size() > 0){
         for(Contact con:contactList){
                conIdSet.add(con.Id);
         }
         }
         if(conIdSet.size()>0){
         List<Subscription__c> subscriptionList=new List<Subscription__c>();
         subscriptionList = [SELECT Id,Subscription_Status__c FROM Subscription__c WHERE Contact__c = :conIdSet AND Subscription_Status__c = true Limit 1];
            if(subscriptionList.size()>0){
             if(subscriptionList[0].size() > 0 && subscriptionList[0].Subscription_Status__c == true){
             ***************
        }
        }
        }
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

 

All Answers

Tad Aalgaard 3Tad Aalgaard 3
You cannot get contact.Id  from a List object.

While there are other ways to rewrite your code you can just add [0] to contact to get the first record.
List<Contact> contact = [SELECT Id, Email FROM Contact WHERE Email=:Email];
         if(contact.size() > 0){
         List<Subscription__c> subscription = [SELECT Id,Subscription_Status__c FROM Subscription__c WHERE Contact__c = :contact[0].Id AND Subscription_Status__c = true Limit 1];
             if(subscription.size() > 0 && subscription.Subscription_Status__c == true){
             }



 
Ramesh DRamesh D
@Satyavathi,
try this
if(subscription.size() > 0 && subscription[0].Subscription_Status__c){
             }

Thanks
Ramesh
Tad Aalgaard 3Tad Aalgaard 3
Without the messed up html that I tried to insert earlier.

To add, when I say you cannot get Id from a list, I mean that you have to either iterate through the list to get the id from the returned record(s) or you have to go after a specific row result using contact[0]. 
List<Contact> contact = [SELECT Id, Email FROM Contact WHERE Email=:Email];
if(contact.size() > 0){
    List<Subscription__c> subscription = [SELECT Id,Subscription_Status__c FROM 
    Subscription__c WHERE Contact__c = :contact[0].Id AND 
    Subscription_Status__c = true Limit 1];
if(subscription.size() > 0 && subscription.Subscription_Status__c == true){
}
You should also try and not use object names like contact for the naming of your variables.
 
Satyavathi PolepallySatyavathi Polepally
Thank you for your help. @Ramesh Sorry there is no option that I can select two answers as best answers. With this I am able to remove the errors but unable to run the code properly. In my requirement I should check if the contact already exists and subscribed then I should not be able to save the record. 
If the contact does not exists OR its not subscribed then I should be able to save the record. I am posting my code here.
When I test the code using my lightning component I am able to save the contact record if its not existing already. But my problem is I am unable to save the contact record if its not subscribed.
Please help me in solving this..
public class CreateContactrecordController {
@AuraEnabled
     public static List<String> getLandValuesIntoList()
     {
      List<String> LandValuesList= new List<String>();
        Schema.DescribeFieldResult fieldResult = Contact.Land__c.getDescribe();
         List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
         for( Schema.PicklistEntry pickListVal : ple){
            LandValuesList.add(pickListVal.getLabel());
        }    
         return LandValuesList;
     }
@AuraEnabled
     public static List<String> getStoreValuesIntoList()
     {
      List<String> StoreValuesList= new List<String>();
        Schema.DescribeFieldResult fieldResult = Contact.Preferred_Store__c.getDescribe();
         List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
         for( Schema.PicklistEntry pickListVal : ple){
            StoreValuesList.add(pickListVal.getLabel());
        }    
         return StoreValuesList;
     }
@AuraEnabled   
    public static Boolean save(String FirstName, String LastName, String Email, String Store, String Land)
    {
        List<Contact> contact = [SELECT Id, Email FROM Contact WHERE Email=:Email];
         if(contact.size() > 0){
         List<Subscription__c> subscription = [SELECT Id,Subscription_Status__c FROM Subscription__c WHERE Contact__c = :contact[0].Id AND Subscription_Status__c = true Limit 1];
             if(subscription.size() > 0 && subscription[0].Subscription_Status__c == true){
                 return false;
             }
            return false;
        }else{
            Contact con=new Contact();
            con.FirstName = FirstName;
            con.LastName = LastName;
            con.Email = Email;
            con.Preferred_Store__c = Store;
            con.Land__c = Land;
            con.HasOptedOutOfEmail = false;
            insert con;
            Subscription__c sub = new Subscription__c();
            sub.Subscription_Status__c = true;
            sub.Subscription_Date__c = date.today();
            sub.Contact__c = con.Id;
            insert sub;
            return true;
        }
    }
 }
Tad Aalgaard 3Tad Aalgaard 3
Basically the problem is the second "return false".  Instead of the second return false you need to add the following.
Subscription__c sub = new Subscription__c();
            sub.Subscription_Status__c = true;
            sub.Subscription_Date__c = date.today();
            sub.Contact__c = contact[0].Id;
            insert sub;
            return true;
But instead of just adding the code above you should move both of the insert sub sections of code to outside of if(contact.size()>0) and have it always try and create a sub.  When it creates the sub outside of that if it will either use a sub.Contact__c value from the existing contact, or it may come from the one you insert.

I'll leave it up to you to rework the code per my suggestions.
 
Ajay K DubediAjay K Dubedi
Hi Satyavathi,

There are some mistakes in your code, you can't use contact.Id directly in sql-Query.So,try the below code it works in my org:
List<Contact> contactList=new List<Contact>();
        Set<Id> conIdSet=new Set<Id>();
        contactList = [SELECT Id, Email FROM Contact WHERE Email=:Email Limit 10000];
         if(contactList.size() > 0){
         for(Contact con:contactList){
                conIdSet.add(con.Id);
         }
         }
         if(conIdSet.size()>0){
         List<Subscription__c> subscriptionList=new List<Subscription__c>();
         subscriptionList = [SELECT Id,Subscription_Status__c FROM Subscription__c WHERE Contact__c = :conIdSet AND Subscription_Status__c = true Limit 1];
            if(subscriptionList.size()>0){
             if(subscriptionList[0].size() > 0 && subscriptionList[0].Subscription_Status__c == true){
             ***************
        }
        }
        }
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

 
This was selected as the best answer