Dynamic SOQL Query
String qString = 'Select Id , Name , FirstName , LastName , Account.Name , Kips_Designation__c , Kips_Relationship__c , Birthdate , Kips_Date_of_Anniversary__c , Kips_Feast_Date__c , Kips_Annual_Gift__c From Contact ';
qString+= ' where Account.Name like \'%' + AccountName+ '%\'';
if(objGift.kips_Type__c !=null && objGift.kips_Type__c !='')
{
if(objGift.kips_Type__c == 'School')
{
//qString+= ' and Account.RecordType.Name like \'%' + objGift.kips_Type__c+ '%\'';
qString+= ' and Account.RecordType.Name = \'' + objGift.kips_Type__c+ '\'';
qString+= ' and Kips_CURRENT_SCHOOL_Active__c = \'Yes\'';
qString+=' and Kips_Key_Person__c = true';
}
if(objGift.kips_Type__c == 'Dealer')
{
qString+= ' and Account.RecordType.Name = \'' + objGift.kips_Type__c+ '\'';
}
}
if(objGift.kips_Festival_Occasion__c != null && objGift.kips_Festival_Occasion__c !='')
{
qString+= ' and Kips_Annual_Gift__c = '+'\''+objGift.kips_Festival_Occasion__c + '\'';
}
if(objGift.kips_Date_Type__c != null)
{
if(FestivalOccassion !='')
{
System.debug(FO.size());
System.debug(FO);
qString+= ' and ( ';
if(FO.size()==1)
{
if(FO[0].contains('Birthday'))
{
qString+= 'BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Feast'))
{
qString+= ' Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Anniversary'))
{
qString+= ' Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
}
if(FO.size()==2)
{
if(FO[0].contains('Birthday'))
{
qString+= 'BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Feast'))
{
qString+= ' Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Anniversary'))
{
qString+= ' Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
if(FO[1].contains('Birthday'))
{
qString+= ' or BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[1].contains('Feast'))
{
qString+= ' or Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[1].contains('Anniversary'))
{
qString+= ' or Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
}
if(FO.size()==3)
{
if(FO[0].contains('Birthday'))
{
qString+= 'BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Feast'))
{
qString+= ' Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Anniversary'))
{
qString+= ' Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
if(FO[1].contains('Birthday'))
{
qString+= ' or BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[1].contains('Feast'))
{
qString+= ' or Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[1].contains('Anniversary'))
{
qString+= ' or Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
if(FO[2].contains('Birthday'))
{
qString+= ' or BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[2].contains('Feast'))
{
qString+= ' or Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[2].contains('Anniversary'))
{
qString+= ' or Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
}
qString+= ')';
}
}
qString+= ' order by Account.Name';
system.debug('qString:' +qString);
qString+= ' where Account.Name like \'%' + AccountName+ '%\'';
if(objGift.kips_Type__c !=null && objGift.kips_Type__c !='')
{
if(objGift.kips_Type__c == 'School')
{
//qString+= ' and Account.RecordType.Name like \'%' + objGift.kips_Type__c+ '%\'';
qString+= ' and Account.RecordType.Name = \'' + objGift.kips_Type__c+ '\'';
qString+= ' and Kips_CURRENT_SCHOOL_Active__c = \'Yes\'';
qString+=' and Kips_Key_Person__c = true';
}
if(objGift.kips_Type__c == 'Dealer')
{
qString+= ' and Account.RecordType.Name = \'' + objGift.kips_Type__c+ '\'';
}
}
if(objGift.kips_Festival_Occasion__c != null && objGift.kips_Festival_Occasion__c !='')
{
qString+= ' and Kips_Annual_Gift__c = '+'\''+objGift.kips_Festival_Occasion__c + '\'';
}
if(objGift.kips_Date_Type__c != null)
{
if(FestivalOccassion !='')
{
System.debug(FO.size());
System.debug(FO);
qString+= ' and ( ';
if(FO.size()==1)
{
if(FO[0].contains('Birthday'))
{
qString+= 'BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Feast'))
{
qString+= ' Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Anniversary'))
{
qString+= ' Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
}
if(FO.size()==2)
{
if(FO[0].contains('Birthday'))
{
qString+= 'BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Feast'))
{
qString+= ' Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Anniversary'))
{
qString+= ' Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
if(FO[1].contains('Birthday'))
{
qString+= ' or BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[1].contains('Feast'))
{
qString+= ' or Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[1].contains('Anniversary'))
{
qString+= ' or Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
}
if(FO.size()==3)
{
if(FO[0].contains('Birthday'))
{
qString+= 'BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Feast'))
{
qString+= ' Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[0].contains('Anniversary'))
{
qString+= ' Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
if(FO[1].contains('Birthday'))
{
qString+= ' or BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[1].contains('Feast'))
{
qString+= ' or Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[1].contains('Anniversary'))
{
qString+= ' or Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
if(FO[2].contains('Birthday'))
{
qString+= ' or BirthDate = '+objGift.kips_Date_Type__c;
}
if(FO[2].contains('Feast'))
{
qString+= ' or Kips_Feast_Date__c = '+objGift.kips_Date_Type__c;
}
if(FO[2].contains('Anniversary'))
{
qString+= ' or Kips_Date_of_Anniversary__c = '+objGift.kips_Date_Type__c ;
}
}
qString+= ')';
}
}
qString+= ' order by Account.Name';
system.debug('qString:' +qString);
Comments
Post a Comment