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
Abdul RazzaqAbdul Razzaq 

Email handler

My email handler works fine for if condition.. but not for else... 
following is my code..:-

 

global class emailhandler2 implements Messaging.InboundEmailHandler {


  global Messaging.InboundEmailResult handleInboundEmail(messaging.inboundemail email, messaging.inboundenvelope envelope)
  {
  
  WeatherCheckIn__c[] newWeathercheckIn = new WeatherCheckIn__c[0];
  
    String subject = email.subject;
   
   String[] emailsub = email.subject.split('#', 0);
   String country = emailsub[0];
   String city = emailsub[1];
   string[] emailaddressr = envelope.fromaddress.split('@',0);
   string lastnamer = emailaddressr[0];
 system.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'+lastnamer);
     Xmlparsercls obj = new Xmlparsercls(city, country);
     list<string> ls = obj.Parsexmlfile();
     
     list<contact> dulast =[select id, lastname from contact where lastname= :lastnamer];
     
     for(contact x : dulast){
     
        if(x.lastname != lastnamer) {
     
     system.debug('//////////////////////////////////////////x.lastname!=lastnamer//////////'+x.lastname);
      
   
   Contact c = new Contact();
   
      c.LastName = lastnamer;
      
      insert c;
       WeatherCheckIn__c wcic = new WeatherCheckIn__c();
        wcic.City__c = city;
        wcic.Country__c = country;
        wcic.Contact__c = c.id;
      newWeathercheckIn.add(new WeatherCheckIn__c(City__c = city,
      Country__c = country));
      insert wcic;
       WeatherCheckOut__c wechou= new WeatherCheckOut__c();
   
   wechou.Location__c = ls[0].substring(24,75);
   wechou.Temperature__c = ls[0].substring(88,100);
   wechou.DewPoint__c = ls[0].substring(110,120);
   wechou.Contact__c = c.id;
   insert wechou;
   
   }
    
     
     else{
  
   system.debug('############################################# x.lastname==lastnamer ####'+x.lastname);
   
  
 WeatherCheckIn__c wcic = new WeatherCheckIn__c();
        wcic.City__c = city;
        wcic.Country__c = country;
        wcic.Contact__c = x.id;
      newWeathercheckIn.add(new WeatherCheckIn__c(City__c = city,
      Country__c = country));
      insert wcic;
       WeatherCheckOut__c wechou= new WeatherCheckOut__c();
   
   wechou.Location__c = ls[0].substring(24,75);
   wechou.Temperature__c = ls[0].substring(88,100);
   wechou.DewPoint__c = ls[0].substring(110,120);
   wechou.Contact__c = x.id;
   insert wechou;
   
   update x;
   }
        }
     system.debug('----------------------------------------subject--------------------'+subject);
       system.debug('--------------------------------------------------country--'+country);
       system.debug('--------------------------------------------city---------'+city);
  
      
       system.debug('............................................................................................................'+ls[0].substring(24,75)+ls[0].substring(88,100)+ls[0].substring(110,120));
      
    Messaging.InboundEmailresult result = new Messaging.InboundEmailResult();
    result.message = 'Your mail has been recieved'+ls;
  
    return result;
  }
}

Sumitkumar_ShingaviSumitkumar_Shingavi
I believe there is problem in your if statement. I cleaned your code. Kindly find below:
 
global class emailhandler2 implements Messaging.InboundEmailHandler {

	global Messaging.InboundEmailResult handleInboundEmail(messaging.inboundemail email, messaging.inboundenvelope envelope) {
  
		WeatherCheckIn__c[] newWeathercheckIn = new WeatherCheckIn__c[0];

		String subject = email.subject;

		List<String> emailsub = email.subject.split('#', 0);
		String country = emailsub[0];
		String city = emailsub[1];
		List<String> emailaddressr = envelope.fromaddress.split('@',0);
		String lastnamer = emailaddressr[0];
		System.debug('^^lastname^^'+lastnamer);
		
		Xmlparsercls obj = new Xmlparsercls(city, country);
		List<string> ls = obj.Parsexmlfile();
		List<contact> dulast =[select id, lastname from contact where lastname= :lastnamer];
     
		for(contact x : dulast) {
     
			if(!x.lastname.equals(lastnamer)) {
				System.debug('//////////////////////////////////////////x.lastname!=lastnamer//////////'+x.lastname);
        
				Contact c = new Contact();
				c.LastName = lastnamer;
				insert c;
				
				WeatherCheckIn__c wcic = new WeatherCheckIn__c();
				wcic.City__c = city;
				wcic.Country__c = country;
				wcic.Contact__c = c.id;
				newWeathercheckIn.add(new WeatherCheckIn__c(City__c = city,
				Country__c = country));
				insert wcic;
				WeatherCheckOut__c wechou= new WeatherCheckOut__c();

				wechou.Location__c = ls[0].substring(24,75);
				wechou.Temperature__c = ls[0].substring(88,100);
				wechou.DewPoint__c = ls[0].substring(110,120);
				wechou.Contact__c = c.id;
				insert wechou;

			} else {

				System.debug('############################################# x.lastname==lastnamer ####'+x.lastname);

				WeatherCheckIn__c wcic = new WeatherCheckIn__c();
				wcic.City__c = city;
				wcic.Country__c = country;
				wcic.Contact__c = x.id;
				newWeathercheckIn.add(new WeatherCheckIn__c(City__c = city,
				Country__c = country));
				insert wcic;
				
				WeatherCheckOut__c wechou= new WeatherCheckOut__c();
				wechou.Location__c = ls[0].substring(24,75);
				wechou.Temperature__c = ls[0].substring(88,100);
				wechou.DewPoint__c = ls[0].substring(110,120);
				wechou.Contact__c = x.id;
				insert wechou;

				update x;
			}
		}
		system.debug('----------------------------------------subject--------------------'+subject);
		system.debug('--------------------------------------------------country--'+country);
		system.debug('--------------------------------------------city---------'+city);
		Messaging.InboundEmailresult result = new Messaging.InboundEmailResult();
		result.message = 'Your mail has been recieved'+ls;
		return result;
	}
}
Hope this helps! If solves the problem then mark it as solution.
Abdul RazzaqAbdul Razzaq

What I was trying to do was... if x.lastname is equal to lastnamer it should update contact.

else if x.lastname is not equal to lastnamer it should insert contact record.

And what is happening is record is not getting inserted only contact present is updated.

hence, from my code if condition is not working.. only else condition works..

made few changes to your(Sumitkumar_Shingavi) code result remains same.

Thanks for the approach.

Razzzaq.