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
LoïcLoïc 

After case contact autocreation try to create a trigger to update the account name

Hello,

 

In the email to case, when I get an email not attached to a contact, I autocreate a contact but not linked to an account.

 

Then I manually attached the case to an account. I d'like to get the value from this filed : "Account Name" to update the contact filed "Account Name".

 

When I try to save my trigger I get the following error: Invalid initial expression type for field Contact.Id, expecting: Id

 

Here is my code (I'm just starting coding trigger so be kind ;-) ): 

trigger Case_account on Case (before update){
	if(Trigger.isUpdate)
    {
      if(Case.AccountId==null && Case.ContactId==null)
        { List <Contact> objCase= new List<Contact>();
      for(Case c:Trigger.New)
        {
    List <Case> ContactIDCase = [select ContactId from Case where CaseNumber=:c.CaseNumber LIMIT 1];
    List <Case> ContactAccountIdCase = [select AccountId from Case where CaseNumber=:c.CaseNumber LIMIT 1];
	contacts.update(new Contact (Id = ContactIDCase, AccountId = ContactAccountIdCase));
	update objCase;
        }
    }
    }
    }

Any help or suggestion are welcome!

 

LegendLegend

Hi,

You cannot use ID field while creating any record. The ID field is auto generated by Salesforce when a new record gets created. You can use that Id field after that record gets created.

 

-Thanks

LoïcLoïc

Thank you that's good to know.

 

Anyway, I d'like to update the existing contact adding the "account name" or "account id" (lookup field) the value of this field coming from the field account name in the attached case.

 

trigger Case_account on Case (before update){
	if(Trigger.isUpdate)
    {
      if(Case.AccountId==null && Case.ContactId==null)
        { List <Contact> objCase= new List<Contact>();
      for(Case c:Trigger.New)
        {
    List <Case> ContactIDCase = [select ContactId from Case where CaseNumber=:c.CaseNumber LIMIT 1];
    List <Case> ContactAccountIdCase = [select AccountId from Case where CaseNumber=:c.CaseNumber LIMIT 1];
	contacts.update(new Contact (Id = ContactIDCase, AccountId = ContactAccountIdCase));
	update objCase;
        }
    }
    }
    }

 

Any suggestion or help on how to proceed ?

Many thanks!

LoïcLoïc

Hello,

 

Here is my last code updated:

 

trigger Case_account on Case (before update){
if(Trigger.isUpdate)
{
if(Case.AccountId==null && Case.ContactId==null)
{
for(Case c:Trigger.New)
{
List <Case> ContactIDCase = [select ContactId from Case where CaseNumber=:c.CaseNumber LIMIT 1];
List <Case> ContactAccountIdCase = [select AccountId from Case where CaseNumber=:c.CaseNumber LIMIT 1];
for (List <Contact> objContact :[Select ID from Contact where (contact.Id = :ContactIDCase)])
{
Contact.AccountId = ContactAccountIdCase;
update objContact;}
}
}
}
}

 

 

The error code I'm getting now is:

Description Resource Path Location Type
Save error: Expression cannot be assigned Case_account.trigger /Test Trigger/src/triggers line 0 Force.com save problem

 

Any idea why I'm having this issue?

Thank you.

LoïcLoïc

I updated my code. I think I'm close but I still have an error:

 

trigger Case_account on Case (before update){
if(Trigger.isUpdate)
{
for(Case c:Trigger.New[0]){
if(Case.AccountId==null && Case.ContactId==null)
{
Case ContactIDCase = [select ContactId from Case where CaseNumber = :c.CaseNumber];
Case ContactAccountIdCase = [select AccountId from Case where CaseNumber = :c.CaseNumber];
for (Contact objContact :[Select ID from Contact where (contact.Id = :ContactIDCase)])
{
objContact.AccountId = ContactAccountIdCase;
update objContact;}
}
}
}
}

 

Save error: Illegal assignment from SOBJECT:Case to Id - line 11 Force.com save problem

 

Any ideas? Thank you for your help.

LegendLegend

Try

objContact.AccountId = ContactAccountIdCase.AccountID;

 

I guess it will solve your issue.

 

Regards,

- TK