Upsert not working..update and insert not working..need help
I have tried using the example given in salesforce visual force developer guide for upsert. It just inserts a new row. I tried in a different way. It checks for a row with the name that matches the name on the page.if there is one rw , then it updates otherwise it will insert a new row. It works for insert but update does not work. I am putting both code here
Code from Salesforce developer guide. I copied the code as it is for page and class. It is not working.
public class NewAndExistingController {
public Account account { get; private set; }
public NewAndExistingController() {
Id id = ApexPages.currentPage().getParameters().get('id');
account = (id == null) ? new Account() :
[SELECT Name, Phone, Industry FROM Account WHERE Id = :id];
}
public PageReference save() {
try {
upsert(account);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
// After successful Save, navigate to the default view page
PageReference redirectSuccess = new ApexPages.StandardController(Account).view();
return (redirectSuccess);
}
}
My code: In getaccnt() method name is becoming null,update not working. if I use code in NewAndExistingController1() and insert works. If I comment that code , name is getting value and update is working but insert (upsert) does not work. If I put that code in else part, insert is not working. THis is killing me. I am not able to figur eout what is wrong. I needto do similar task for my client. Aready deadline is over. Please help.
public class NewAndExistingController1
{
public Account acc{get;set;}
public string name {set; get;}
public string phone {get; set;}
//public string id {get; set;}
public NewAndExistingController1() {
system.debug('inside NewAndExistingController1' );
Id id = ApexPages.currentPage().getParameters().get('id');
system.debug('id' +id);
acc = (id == null) ? new account() : [select Id, Name, Phone, Industry from Account WHERE Id = :id];
}
public boolean getcount() {
integer ctr = [select count() from Account WHERE Name = :name];
system.debug('name= ' +name);
system.debug('ctr = ' +ctr);
return ctr == 1;
}
public PageReference save() {
try {
// string upd_flg = [select 'X' from Account WHERE Name = :name];
// this.acc = [select Id, Name, Phone from Account WHERE Name = :name];
if (getcount())
{
system.debug('check if getaccount');
this.acc = [select Id, Name, Phone from Account WHERE Name = :name];
system.debug('acc' +acc.id);
acc.phone = phone;
update(acc);
return null;
}
else
{
system.debug('in else part');
// Id id = ApexPages.currentPage().getParameters().get('id');
// system.debug('id' +id);
// acc = (id == null) ? new account() : [select Id, Name, Phone, Industry from Account WHERE Id = :id];
// account[] acc = [select Id, Name, Phone from Account WHERE Name = :name];
// Account newAcct = new Account(Name = :Name, phone = '54667788');
//acc.add(newAcct);
upsert acc;
return null;
}
}
catch (exception ex)
{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
ApexPages.addMessage(msg);
return null;
}
}
}
Page code:
<apex:page controller="NewAndExistingController1" tabstyle="Account">
<apex:form >
<apex:pageBlock mode="edit">
<apex:pageMessages />
<apex:pageBlockSection >
<apex:inputField value="{!acc.name}"/>
<apex:inputField value="{!acc.phone}"/>
<apex:inputField value="{!acc.industry}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>