0

I created an apex controller class and i am trying to upload ot to salesforce, i receive many errors, one of them is:"Comparison arguments must be compatible types: System.Address, String" and "Field is not writeable: Contact.Name (30:50)" same error with MailingAddress field. From what i know you can edit the name of a contact and its MailingAddress, i´ll leave my code down below:

@AuraEnabled(cacheable=true)
public static List<Object> searchContacts(String seachTerm) {

 List <Contact> result= ([SELECT id,Name,Phone,MailingAddress,Email,Title,Birthdate FROM contact where Name like: '%' + seachTerm +'%']);

  return result;

}
@AuraEnabled(cacheable=true)
public static void deleteContact(String conToDelete){

    Contact result=  ([SELECT id, Name,Phone,MailingAddress,Email,Title,Birthdate FROM contact where Name like: '%' + conToDelete +'%']);
try{
    delete result;
    
}catch(DmlException e){

    system.debug(e);

}
}
@AuraEnabled(cacheable=true)
public static void editContact(Contact Contact){

    Contact editContact=Contact;
    if(editContact.Name!=null || editContact.Name!=''){
    Contact result= ([SELECT  id,Name,Phone,MailingAddress,Email,Title,Birthdate FROM contact where Name like:'%' + editContact.Name +'%']);

    if(editContact.Name!=result.Name){result.Name=editContact.Name;}
    if((editContact.Phone!=null || editContact.Phone!='') && editContact.Phone!=result.Phone){result.Phone=editContact.Phone;}
    if((editContact.MailingAddress!=null || editContact.MailingAddress!='') && editContact.MailingAddress!=result.MailingAddress){result.MailingAddress=editContact.MailingAddress;}
    if((editContact.Email!=null || editContact.Email!='') && editContact.Email!=result.Email){result.Email=editContact.Email;}
    if((editContact.Title!=null || editContact.Title!='') && editContact.Title!=result.Title){result.Title=editContact.Title;}
    if((editContact.Birthdate!=null || editContact.Birthdate!='') && editContact.Birthdate!=result.Title){result.Birthdate=editContact.Birthdate;}

    try{
        update result;
        
    }catch(DmlException e){

        system.debug(e);

    }

}

}

enter image description here

Thank you!

1 Answer 1

1
  1. MailingAddress is a address datatype in salesforce and you have compared that with String. editContact.MailingAddress != '' Also you cannot set address like this. You need to set each and every attribute of address separately.

  2. Same for Birthdate, its a date field and you have compared that with String. editContact.Birthdate != ''

  3. Name in Contact is a compound field, which means it consists of FirstName and LastName. So you cannot set Name, directly, you can only set FirstName and LastName.

NOTE: Change your input variable from Contact Contact from Contact incomingContact

1
  • So checking out the MailingAddress and The Birthdate should be enough or could i use something else to evaluate if is empty? I tries isEmpy() but apparently works only for Strings as well. Thanks for your help!! Commented Jan 4, 2023 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .