You need to sign in to do that
Don't have an account?
Satyavathi 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.
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.
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: 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
While there are other ways to rewrite your code you can just add [0] to contact to get the first record.
try this
Thanks
Ramesh
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]. You should also try and not use object names like contact for the naming of your variables.
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;
}
}
}
I'll leave it up to you to rework the code per my suggestions.
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: I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi