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
DhairyaDhairya 

Trigger which Create New account from Case

Hi,

 

     The scenario is :

 

     (1) Case contains few custom field, and if that field value is not present as an account name then create account using that name.

 

    They do not want to go with lookup idea. They want trigger.

 

The Code i tried to write is  :

 

 

trigger EndUserAccountTrigger on Case (after insert) {
 list<account> accname = [select name from account where type='End User']; 
 list<account> tempaccname = new list<account>();
 for(case c: trigger.new)
 {
   for(account acc: accname)
 {
 if(acc.name != c.system__c)
 {
 tempaccname.add(new account(name=c.System__c));
 }
   
 }
 }
 database.insert(tempaccname);
}

 

 

 

 

 

 

 

 

 

 

 

 

But it throw an error.

 

     Appreciate your response.

 

Thanks,

Dhairya

Best Answer chosen by Admin (Salesforce Developers) 
DhairyaDhairya

 

Solution :
  AccountCount = [select count() from Account where Name=:newRow.System__c];
         //if account name not exist then create a new account record
          if(AccountCount == 0 )
          {  
            Account a = new Account();
            a.Name=newRow.System__c;
            insert a;
          }
   
   

 

All Answers

sravusravu

Hi,

    If i am not wrong in understanding your scenario, try the folowing trigger

 

Account_Name__c is a custom field based on which we are going to create an account

 

trigger newAccountfromCase on Case (after insert) {
    Case c = [select Account_Name__c from Case where id = :Trigger.New];
    if(c.Account_Name__c!=NULL){
        Account acc = new Account();
        acc.Name=c.Account_Name__c;
        insert acc;
    }
}

 

Correct me if i am wrong.....................

 

DhairyaDhairya

Hi Sravu,

 

      Thanks for reply but at the same time i want to check whether that account name is already present or not? If not then create new one otherwise that fine.

sravusravu

Make the following changes and try if this helps you to meet your requirement.

 

 

trigger newAccountfromCase on Case (after insert) {
public Id aId;
public String aName;
List<Case> c = [select AccountId,Account_Name__c from Case where id = :Trigger.New];
for(Case ac : c){
aId=ac.AccountId;
aName=ac.Account_Name__c;
}
Account acc = [select Name from Account where Name=:aName];
if(aName!=acc.Name){
Account a = new Account();
a.Name=aName;
insert a;
}
}

 

Let me know if you face any difficulty..........

DhairyaDhairya

Hi, I modified the code with my custom field system__c : (1) Its not creating duplicate account and saved case succesfully (2) But following error throw when tried to save a case. Apex trigger EndUserAccountTrigger caused an unexpected exception, contact your administrator: EndUserAccountTrigger: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.EndUserAccountTrigger: line 12, column 15 .

 

 

trigger EndUserAccountTrigger on Case (after insert) { public Id aId; public String aName; List c = [select AccountId,System__c from Case where id = :Trigger.New]; for(Case ac : c){ aId=ac.AccountId; aName=ac.system__c; } Account acc = [select Name from Account where Name=:aName]; if(aName!=acc.Name){ Account a = new Account(); a.Name=aName; insert a; } }

sravusravu

The problem here is when the account name field is null it is giving an exception:

Add the following if condition in your code

 

trigger newAccountfromCase on Case (after insert) {
    public Id aId;
    public String aName;
    List<Case> c = [select AccountId,Account_Name__c from Case where id = :Trigger.New];
    for(Case ac : c){
        aId=ac.AccountId;
        aName=ac.Account_Name__c;
    }
    if(aName!=NULL){
        Account acc = [select Name from Account where Name=:aName];
        if(aName!=acc.Name){
            Account a = new Account();
            a.Name=aName;
            insert a;
        }
    }
}

 

Hope this helps you..............

DhairyaDhairya

Thanks for help. 

 

Solution figured it out.

DhairyaDhairya

 

Solution :
  AccountCount = [select count() from Account where Name=:newRow.System__c];
         //if account name not exist then create a new account record
          if(AccountCount == 0 )
          {  
            Account a = new Account();
            a.Name=newRow.System__c;
            insert a;
          }
   
   

 

This was selected as the best answer