+ Start a Discussion
Nati KirenNati Kiren 

create record if does not exist

Hello,

I am newhere and I'm trying to develop a page that receive a record id of a custom object check if a record with this id exsist, if not creat one.
can anyone show how to do it.

Thanks
gautam_singhgautam_singh
Hi, 

A Small Quick Code to make you understand the bascis .. 

Use the <apex:inputText> for fields and Save button will call the action SaveResults. Let me know if you need any clearance over Visualforce Page as well. 

public class createARecord{
   
    //create a dummy variable which contains the account which you will create or update
    public List<Account> newAccount{get;set;}
   
    public String externalIdaccountName{get;set;}
    //constructor - this will be called on page load.
    public createARecord(){
       
        //checks if the Account Name which you have entered exsists or not, If Exsist then assign that account to update
        if(externalIdaccountName != null && externalIdaccountName != ''){
            newAccount= [SELECT Id, Name, Site FROM Account WHERE Name =: externalIdaccountName];
           
            if(newAccount.size() <0)
                newAccount = new List<Account>();
               
        }
   
       
    }

    //upsert the newly created account.
    public void SaveResults(){
        upsert NewAccount;
   
    }


}
Important :

If this is what you where looking for then please mark it as a solution for others benefits.

Thank You


Nati KirenNati Kiren
i did that but i receive an error
System.NullPointerException: Attempted to upsert a null list
Jim HladekJim Hladek
The issue for the NullPointerException is from line 14. The size of a list can never be less then 0.  Change the code to read if(newAccount.size() == 0). Also there is no new Account being created if there is no account found in the query. On line 16 you should add newAccount.add(new Account()); This will create a new account if one does not exist.
Pramod_SFDCPramod_SFDC
Hi ,

Just FYI

The execution of a trigger will sometimes generate an error message like the following:

execution of [EventName] caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.[TriggerName]: line [XX], column [XX]

This error is caused by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been initialized.

For example, if the get() method in the following code cannot find the value oldAccount.Name in the map accountMap, the object newAccount will be null, and trying to use it will generate this error message:

    trigger SampleTrigger on Account (before insert, before update) {
     integer i = 0;

      Map<String, Account> accountMap = new Map<String, Account>{};
      for (Account acct : System.Trigger.new)
        accountMap.put(acct.Name, acct);
      for (Account oldAccount : [SELECT Id, Name, Site FROM Account WHERE Name IN :accountMap.KeySet()]) {
        Account newAccount = accountMap.get(oldAccount.Name);
        i = newAccount.Site.length;
    }

Also in this example, if the field Site was left empty, trying to use it as above will generate the error message as well.

The solution is to make sure the object and/or the attribute to be used is not null. In this example, the code needs to be modified as follows:

        Account newAccount = accountMap.get(oldAccount.Name);
        if (newAccount != null)
          if (newAccount.Site != null)
            i = newAccount.Site.length();

or an exception handling routine can be used, such as

        Account newAccount = accountMap.get(oldAccount.Name);
        try {
          i = newAccount.Site.length();
        } catch (System.NullPointerException e) {
            e1 = e; // can be assigned to a variable to display a user-friendly error message
          }

For more information on handling exceptions, check the Apex Language Reference, under "Using Exception Methods", or "Using Exception Variables".

For more information on initializing variables, look under Chapter 2, "Language Constructs", in the topics "Variables", and "Case Sensitivity".



Regards
Pramod