+ Start a Discussion
p.gleixnerp.gleixner 

Trigger to get Account(LookupField) via String(TextField)

Hi!

Im trying to build a trigger that automatically adds the correct Account to a Case that was created via email-to-case. The Account name stands in the Description field of the case created by the incoming email.
At the moment I am at the first step. I am simply trying to copy the String of a TextField into the Account LookUp Field.
For Example: The Textfield "Description" says "ACME Corp." Then I want that as the Account(That already exists and can be picked int the lookup field) the Case belongs to.

Here is what I got:


trigger licenseToAssetTrig on Case (before insert, before update) {
    
    for(Case c:trigger.new){
      if(c.Subject == 'ACME Corp'){
        Account accMatch = [SELECT Name FROM Account WHERE Name =: c.Subject];
        c.Account = accMatch;
     }
    }
}

Remeber, its just the first step and I am only trying to change the Case Account to the Account of the Description when I open or update a new case manually

Thanks in advance
Peter

SFDCStarSFDCStar

Peter,

 

You sholud have Unique Field value of Account in Case

p.gleixnerp.gleixner

Hi.

I am not sure I understand what you mean with "Unique FIeld". I am pretty new to salesforce and especially apex.

 

In my Case Sheet I have the lookup "Account Name" If I edit a case I can choose here the Account Objects in my Database.

In my Description field is a String of the exact Account name, already existing in the Database. Is there no possibility to connect those fields?

 

Thanks for your answer

Peter
.

 

p.gleixnerp.gleixner

Hi!

 

ok I think I understand now what you ment. Thah`s what I have now and it works perfectl, but on a custom field. On the fiel Account in Case it won`t work. Is that just a salesforce problem?

Here is my code:

 

trigger test2 on Case (before insert, before update) {
    for(Case c:trigger.new){
        User accMatch = [SELECT Id ,Name FROM User WHERE Name =: c.Subject];
        //c.Account = accMatch.Id;  -> doesnt work
        c.Service_Complaint_User__c = accMatch.Id; //-> works
    }
}

 

Greetings

Peter

MoggyMoggy

I know its long ago but

exchange c.Account with c.AccountID, this will do the trick

 

trigger licenseToAssetTrig on Case (before insert, before update) {
    
    for(Case c:trigger.new){
      if(c.Subject == 'ACME Corp'){
        Account accMatch = [SELECT Id,Name FROM Account WHERE Name =: c.Subject];
        c.AccountID = accMatch.Id;
     }
    }
}