+ Start a Discussion
Vipin K 10Vipin K 10 

Trigger to autopopulate Account Lookup

Hi All,

Hope someone can help me on this.
I have created an custom account lookup because with standard account lookup i am unable to add contact filter.
Now I have to autopopulate account name on the custom account field on quote from the opportunity object or possibliy from Account Object itself. It should autopopulate when i click the new button on quote. 
I tried some trigger but i hope there is some mistakes.Please see below.

trigger updateQuoteAccount on Quote (before insert, before update) {
      List<Quote> OpportunitiesToUpdate = new List<Quote>();
      for(Quote QA : [Select Id, Opportunity.Account.Id From Quote Where Id IN : trigger.newMap.keyset()]) {
            QA.Account_c = QA.Opportunity.Account.Id;
             QuoteToUpdate.add(QA);            
       }   
       if(!QuoteToUpdate.isEmpty())
                update QuoteToUpdate;
 
lalitaroralalitarora
Hi Vipin,

Point 1. (For knowledge) On the point of autopopulating the account custom field on the edit layout when you click new, this cannot be achived just by a trigger because trigger will run and fetch the values while you save the record (either before or efter trigger).

Point 2. (Solution) : Create one liner Visual force page with the standard controller as quote,extention and action tag calling the method from extention returning page reference.

Visual force page sample:
<apex:page standardcontroller="quote" extensions="Extension class" sidebar="false" action="{!autoPopulateFieldOnQuote}"> </apex:page>


Method would be like : 
 public PageReference autoPopulateFieldOnQuote() {
        
        Customsetting__c CustSetting = Customsetting__c.getOrgDefaults();        
        String keyPrefix = quote.sObjectType.getDescribe().getKeyPrefix();
        
        PageReference p = new pagereference('/' + keyPrefix + '/e?');
        p.getparameters().putall(ApexPages.currentPage().getparameters());
        p.getparameters().put('nooverride', '1');
        p.getparameters().remove('sfdc.override');
        p.getparameters().remove('save_new');
// custom setting will store the field ID , manually change the ID across environments(dev, test).
//here fetch the account value via SOQL
        p.getParameters().put(CustSetting.Fieldname, Account value returned from SOQL);
//create page reference URL and other custom code
        p.setRedirect(true);
        return p;
    } 

Hope that helps. Reply back in case you face any issues with implementation.

Cheers,
Lalit
suresh sanneboina 4suresh sanneboina 4
trigger updateQuoteAccount on Quote (before insert, before update) {
	List<Quote> OpportunitiesToUpdate = new List<Quote>();
	Set<Id> oppid=new Set<Id>();
	for(Quote quo:Trigger.new)
	{
		if(Trigger.isUpdate && quo.OpportunityId != Trigger.oldMap(quo.Id).OpportunityId);
		{
			oppid.add(quo.OpportunityId);
		}else{
			oppid.add(quo.OpportunityId);
		}
	}

	if(!oppid.isEmpty)
	{
		Map<Id,Opportunity> mapopportunity=new Map<Id,Opportunity>([Select Id,AccountId from Opportunity Where Id IN : oppid]);
		if(!mapopportunity.isEmpty())
		{
			for(Quote quo:Trigger.new)
			{
				if(mapopportunity.containsKey(quo.OpportunityId))
				{
					quo.Account_c = mapopportunity.containsKey(quo.OpportunityId).AccountId;
				}
			}
		}
	}
}