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
Daniele SerraDaniele Serra 

Why is Case.ContactEmail field is NULL?

Hi community, I'm working with case object. This is my code:
 
Case caso= new Case();
              
               Contact contatto=[SELECT Id, Name, Email FROM Contact where Account.id =: uli.B2WISU__Account__c limit 1];
               System.debug(contatto.Email);
               caso.ContactId=contatto.id;
               caso.Status = 'New';
               caso.Description = 'TestDescription';
               insert caso;
               
               System.debug(caso.ContactId);
               System.debug(caso.ContactEmail);
               System.debug(caso.SuppliedEmail);

After insert the case, i can visualize caso.ContactId, but ContactEmail and SuppliedEmail stay null. I can visualize contat.email different from null. Someone can explain to me why ContactEmail don't retrieve the email from the Contact?
Thank you so much.
Best Answer chosen by Daniele Serra
PawanKumarPawanKumar
Hi Daniele,
Please try below code. Please let me know if it helps you.


Case caso = new Case();

Contact contatto = [SELECT Id, Name, Email FROM Contact where Account.id = : uli.B2WISU__Account__c limit 1];
System.debug(contatto.Email);
caso.ContactId = contatto.id;
caso.Status = 'New';
caso.Description = 'TestDescription';
// as supplied email won't be updated automatically.
caso.SuppliedEmail= contatto.Email;
insert caso;

//After insert only Id is updated in the existing object that's why you are not getting below Value.
System.debug(caso.ContactId);
System.debug(caso.ContactEmail);
System.debug(caso.SuppliedEmail);

// to get other you will have to query again as below.
Case newCaseCreated = [Select Id, ContactId, ContactEmail, SuppliedEmail from Case where Id = : caso.Id];
System.debug(newCaseCreated.ContactId);
System.debug(newCaseCreated.ContactEmail);
System.debug(newCaseCreated.SuppliedEmail);

Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
Hi Daniele,
Please try below code. Please let me know if it helps you.


Case caso = new Case();

Contact contatto = [SELECT Id, Name, Email FROM Contact where Account.id = : uli.B2WISU__Account__c limit 1];
System.debug(contatto.Email);
caso.ContactId = contatto.id;
caso.Status = 'New';
caso.Description = 'TestDescription';
// as supplied email won't be updated automatically.
caso.SuppliedEmail= contatto.Email;
insert caso;

//After insert only Id is updated in the existing object that's why you are not getting below Value.
System.debug(caso.ContactId);
System.debug(caso.ContactEmail);
System.debug(caso.SuppliedEmail);

// to get other you will have to query again as below.
Case newCaseCreated = [Select Id, ContactId, ContactEmail, SuppliedEmail from Case where Id = : caso.Id];
System.debug(newCaseCreated.ContactId);
System.debug(newCaseCreated.ContactEmail);
System.debug(newCaseCreated.SuppliedEmail);

Regards,
Pawan Kumar
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
It's looks like you have never supplied any value in case.SuppliedEmail

Try to run the following code;
Case caso= new Case();
              
               Contact contatto=[SELECT Id, Name, Email FROM Contact where Account.id =: uli.B2WISU__Account__c limit 1];
               System.debug(contatto.Email);

               caso.ContactId=contatto.id;
               caso.Status = 'New';
               caso.Description = 'TestDescription';
               caso.ContactEmail = contatto.Email;
               caso.SuppliedEmail = contatto.Email; // whichever ones goes here.
               insert caso;
               
               System.debug(caso.ContactId);
               System.debug(caso.ContactEmail);
               System.debug(caso.SuppliedEmail);

Hope it helps!

 
PawanKumarPawanKumar
Hi Daniele,
Please let me know if it works for you.

Regards,
Pawan Kumar
Daniele SerraDaniele Serra
thanks!