• Jason Rose 1
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
As of a couple of days ago we started getting this error and not sure where to go to get it fixed. Hope someone can help
 


Description:   A trigger that will generate the Property records when Lead is converted
Test Class:    TestTriggerGenerateNewProperty


trigger TriggerGenerateNewProperty on Lead (after update) {
    Map<String,Account> AccountMap= new map<String,Account>([Select name,Id, OwnerId from account]);    
    System.debug('@AccountMap'+AccountMap);   
    Property_Contacts__c propertyContact = new Property_Contacts__c();
    Property_Contacts__c propertyContactToUpdate = new Property_Contacts__c();
    List<Property_Contacts__c> propertyContactList = new List<Property_Contacts__c>();
    List<Property_Contacts__c> propertyContactListToUpdate = new List<Property_Contacts__c>();
    List<Property_Contacts__c> propertyContactListExistToUpdate = new List<Property_Contacts__c>();
    List<Property__c> PropertyList = new List<Property__c>();   
    Set<Id> leadId =new Set<Id>();
    Set<String> OppId = new Set<String>();
    Set<String> PropertyId = new Set<String>();
    Set<String> AccountId = new Set<String>();
    Map<Integer,Id> AccountExistmap = new Map<Integer,Id>();  
    List<String> LeadName = new List<String>();
    List<String> unitNumber = new List<String>();
    List<String> streetNumer = new List<String>();
    List<String> streetName = new List<String>();
    List<String> suburb = new List<String>(); 
    
    for(Lead lead: trigger.new){ 
    
        if(Lead.IsConverted){
            Integer i=0;
            Property__c property=new Property__c();
            if(lead.Property_Unit_Number__c!=null){
                property.Unit_Number__c = lead.Property_Unit_Number__c;
            }
            if(lead.Property_Street_Number__c!=null){
                property.Street_Number__c = lead.Property_Street_Number__c;
            }
            if(lead.Property_Street_Name__c!=null){
                property.Street_Name__c = lead.Property_Street_Name__c;
            }
            if(lead.Property_Suburb__c!=null){
                property.Suburb__c = lead.Property_Suburb__c;
            }
            if(lead.Property_Postcode__c!=null){
                property.Postcode__c = lead.Property_Postcode__c;   
            }   
            system.debug('@ AccountMap.get(lead.ConvertedAccountId).OwnerId'+ AccountMap.get(lead.ConvertedAccountId).OwnerId);     
            if(AccountMap.get(lead.ConvertedAccountId).OwnerId!=null){
                property.OwnerId = AccountMap.get(lead.ConvertedAccountId).OwnerId;
            }   
            
            if(lead.Property_Street_Type__c!=null){
                property.Street_Type__c = lead.Property_Street_Type__c;
            }   
            if(lead.Property_State__c!=null){
                property.State__c = lead.Property_State__c;
            }
            
            if(lead.Current_Agent__c!=null){
                property.Current_Agent__c = lead.Current_Agent__c;
            }
            if(lead.Rental_Amount__c!=null){
                property.Rental_Amount__c = lead.Rental_Amount__c;
            }
            
            if(lead.Property_Type__c!=null){
                property.Property_Type__c = lead.Property_Type__c;
            }   
            
            if(lead.Date_Last_Advertised__c!=null || lead.Date_Last_Advertised__c!=''){
                property.Date_Last_Advertised__c = lead.Date_Last_Advertised__c;
            }
            //REXP-35
            if(lead.Description != null || lead.Description != ''){
              property.Description__c = lead.Description;
            }
            if(lead.How_did_you_hear_about_Rental_Express__c != null || lead.How_did_you_hear_about_Rental_Express__c != ''){
              property.How_did_you_hear_about_Rental_Express__c = lead.How_did_you_hear_about_Rental_Express__c;
            }
            if(lead.I_would_like_to_rent_the_property__c != null || lead.I_would_like_to_rent_the_property__c != ''){
              property.I_would_like_to_rent_the_property__c = lead.I_would_like_to_rent_the_property__c;
            }
            if(lead.Property_Condition__c != null || lead.Property_Condition__c != ''){
              property.Property_Condition__c = lead.Property_Condition__c;
            }
            if(lead.Property_Furnishings__c != null || lead.Property_Furnishings__c != ''){
              property.Property_Furnishings__c = lead.Property_Furnishings__c;
            }
            if(lead.Pool__c != null || lead.Pool__c != ''){
              property.Pool__c = lead.Pool__c;
            }
            if(lead.Air_Conditioned__c != null || lead.Air_Conditioned__c != ''){
              property.Air_Conditioned__c = lead.Air_Conditioned__c;
            }
            if(lead.Car_Accomodation__c != null || lead.Car_Accomodation__c != ''){
              property.Car_Accomodation__c = lead.Car_Accomodation__c;
            }
            if(lead.Outdoor_Living_Areas__c != null || lead.Outdoor_Living_Areas__c != ''){
              property.Outdoor_Living_Areas__c = lead.Outdoor_Living_Areas__c;
            }
            if(lead.Living_Areas__c != null || lead.Living_Areas__c != ''){
              property.Living_Areas__c = lead.Living_Areas__c;
            }
            if(lead.Number_of_Bathrooms__c != null || lead.Number_of_Bathrooms__c != ''){
              property.Number_of_Bathrooms__c = lead.Number_of_Bathrooms__c;
            }
            if(lead.Number_of_Bedrooms__c != null || lead.Number_of_Bedrooms__c != ''){
              property.Number_of_Bedrooms__c = lead.Number_of_Bedrooms__c;
            }
            if(lead.Construction_Type__c != null || lead.Construction_Type__c != ''){
              property.Construction_Type__c = lead.Construction_Type__c;
            }
            if(lead.Age_of_Property__c != null || lead.Age_of_Property__c != ''){
              property.Age_of_Property__c = lead.Age_of_Property__c;
            }
            if(lead.Property_Status__c != null || lead.Property_Status__c != ''){
              property.Property_Status__c = lead.Property_Status__c;
            }
            AccountExistmap.put(i,lead.ConvertedAccountId); 
            
            OppId.add(lead.ConvertedOpportunityId);     
            leadId.add(lead.id);
            LeadName.add(lead.name);
            unitNumber.add(lead.Property_Unit_Number__c);   
            streetNumer.add(lead.Property_Street_Number__c);
            streetName.add(lead.Property_Street_Name__c);
            suburb.add(lead.Property_Suburb__c);
            if(lead.Property_Street_Number__c!=null && lead.Property_Street_Name__c!=null && lead.Property_Suburb__c!=null && lead.Property_Postcode__c!=null && lead.Property_State__c!=null){            
                PropertyList.add(property);
            }     
            
            propertyContact.Contact__c = lead.ConvertedAccountId;
            propertyContactList.add(propertyContact);
            
        }   
    }
    
    
    
    List<Property__c> propsExist = new List<Property__c>();
                    
    for(Property__c prop: [Select Id,Unit_Number__c, Street_Number__c, Street_Name__c, Suburb__c from Property__c where Unit_Number__c in:unitNumber and  // Check if Property exist
                        Street_Number__c in:streetNumer and Street_Name__c in:streetName and Suburb__c in:suburb]){
                        
        Integer loopCounter = 0;                    
        prop.Unit_Number__c=unitNumber[loopCounter];    
        prop.Street_Number__c=streetNumer[loopCounter];
        prop.Street_Name__c=streetName[loopCounter];
        prop.Suburb__c = suburb[loopCounter];   
        prop.Postcode__c = PropertyList[loopCounter].Postcode__c;
        prop.Street_Type__c = PropertyList[loopCounter].Street_Type__c;
        prop.State__c = PropertyList[loopCounter].State__c;
        prop.Current_Agent__c = PropertyList[loopCounter].Current_Agent__c;
        prop.Rental_Amount__c = PropertyList[loopCounter].Rental_Amount__c;
        prop.Property_Type__c = PropertyList[loopCounter].Property_Type__c;
        prop.Date_Last_Advertised__c = PropertyList[loopCounter].Date_Last_Advertised__c;
        PropertyId.add(prop.Id);            
        propsExist.add(prop);
    }
    System.debug('@propsExist'+propsExist);
    if(propsExist.size()>0){
        update propsExist;
        
        Property_Contacts__c propcontactNew = new Property_Contacts__c();
        for(Property_Contacts__c propcontact: [Select Id,Property__c, Contact__c from Property_Contacts__c where Property__c in: PropertyId limit 1]){ 
            Integer counter =0;           
            propertyContactListExistToUpdate.add(propcontact);
            counter++;
        }
        system.debug('@propertyContactListExistToUpdate'+propertyContactListExistToUpdate);
        if(propertyContactListExistToUpdate.size()>0){
            update propertyContactListExistToUpdate;
        }
    }
    else{
        if(PropertyList.size()>0){
            insert PropertyList;
            System.debug('@PropertyList'+PropertyList);
            
            for(Property__c p: PropertyList){
                Integer counter = 0;
                propertyContactToUpdate.Property__c = p.Id;         
                propertyContactToUpdate.Contact__c = propertyContactList[counter].Contact__c; 
                propertyContactListToUpdate.add(propertyContactToUpdate);
                    
                counter++;
            }
            
            if(propertyContactListToUpdate.size()>0){
                insert propertyContactListToUpdate;
            }
        }
        
    }
    
    List<Opportunity> OpportunityToUpdate = new List<Opportunity>();
    for(Opportunity op: [Select Id,property__c from Opportunity where Id in:OppId limit 1]){
        Integer counter = 0;
        if( PropertyList[counter].Id!=null){
            op.Property__c = PropertyList[counter].Id;
        }
        else{
            op.Property__c = propertyContactListExistToUpdate[counter].Property__c;
        }   
            
        OpportunityToUpdate.add(op);
        counter++;
    }
    System.debug('@OpportunityToUpdate'+OpportunityToUpdate);
    if(OpportunityToUpdate.size()>0){
        update OpportunityToUpdate;
    }
    
}