+ Start a Discussion
shoba shobashoba shoba 

Button feature in Account object

Hi 
For an opportunity there is a feature called "Save and  Add" button,while clicking this button it will save the opportunity and redirect to opportunitylineitem to add products. Is there is any feature in account object that while saving account  it should save and redirect to its contact.
Thanks in advance.

Mahesh DMahesh D
Hi Shoba,

I don't think we have any OTB (Out of The Box) functionality but you can able to achieve it with writing a simple trigger on Account Object.

Please check below:
 
trigger AccountTrigger on Account (after insert){
    List<Contact> conList = new List<Contact>();
    
    for(Account acc: Trigger.new){
        Contact con = new Contact();
        con.LastName = acc.Name;
        con.AccountId = acc.Id;
        con.Email = 'test@test.com'; // Set the Email Id from Account if you are capturing it from the user.
        conList.add(con);
    }
    
    insert conList;
}

Please do let me know if you need more information.

Regards,
Mahesh
shoba shobashoba shoba
Hi mahesh

thank you for your reply. My scenario is to create a custom button "save and add" while clicking this button i want to save the account and should redirect to the contact page. Actually my doubt is can we do this through javascript.
shoba shobashoba shoba
To  solve this issue i wrote a Vf page and apex class and i override the view button in account.But it througwing error like                                        "Formula Expression is required on the action attributes"". can anyone help me out to solve this issue.
 
VF page:
<apex:page standardController="Account" extensions="AccountRedirect" action="{!redirect}" >
 <apex:outputfield value="{!Account.OwnerId}"/>
 <apex:detail />
</apex:page>

public with sharing class AccountRedirect {
public String accId;     
public String ownerId;     
private final Account Acc;
    public AccountRedirect(ApexPages.StandardController controller) {
accId = Controller.getId();               
Acc = (Account)Controller.getRecord();              
ownerId = Acc.OwnerId;
    }
public PageReference redirect() {

    PageReference pageDetail = new PageReference ('e?retURL=' + accId + '&accid=' + accId);
        pageDetail.setRedirect(true);
        Contact[] ocr = [select id,AccountId from contact where AccountId = :accId];
        if (ocr.size() == 0 && UserInfo.getUserId() == ownerId) return pageDetail;
        return pageDetail;
    }

}

 
Mahesh DMahesh D
Hi Shoba,

Please find the modified code:
 
public with sharing class AccountRedirect {
public String accId;     
public String ownerId;     
private final Account Acc;
    public AccountRedirect(ApexPages.StandardController controller) {
accId = Controller.getId();               
Acc = (Account)Controller.getRecord();              
ownerId = Acc.OwnerId;
    }
public PageReference redirect() {

    PageReference pageDetail = new PageReference ('/003/e?retURL=' + accId + '&accid=' + accId);
        pageDetail.setRedirect(true);
        Contact[] ocr = [select id,AccountId from contact where AccountId = :accId];
        if (ocr.size() == 0 && UserInfo.getUserId() == ownerId) return pageDetail;
        return pageDetail;
    }

}
Please do let me know if it helps you.

Regards,
Mahesh
shoba shobashoba shoba
Hi mahesh

Thank you for your reply. when we create a new account it working good. but if i view the exist accont its direct goes to contact i cant able to see account details. whether am in right way or not. Can you please give me sugession.
harsha__charsha__c
I dont think you need to override new button for this. Try something like this way :

- Create a List button on Account "Save & Add Contact"
- On click of this button, redirect to '001/e' and here set the saveURL and returnURL as '003/e' (URL comes to /001/e?retURL=003/e&saveURL=003/e&cancelURL=001/o)
- This overrides the callbak url of SAVE and takes you to the NEW CONTACT page.

One tricky part here is to pass AccountId in the saveURL/retURL....  If you can select the Account manually then this should work...

- Harsha