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
gwestongweston 

pre populate an opp field from account

I am trying to pre-populate an opportunity field when creating it from an related account. I think the trigger should look something like this but I keep getting stumped by errors.

 

trigger populateBillingInfo on Opportunity (before insert) {


for(Opportunity o: Trigger.new){
	if(o.CCNumber__c == null || o.CCNumber__c.equals('')){
		
	
o.CCNumber__c = [Select CCNumber__c from Account  Where Id = :o.AccountId];
		

	}
	}
}

 

 

Ankit AroraAnkit Arora

And what is the error ??

 

Is it : "Illegal assignment from LIST<Account> to String"

 

If yes then change your code to look something like this :

 

trigger populateBillingInfo on Opportunity (before insert) {


for(Opportunity o: Trigger.new){
	if(o.CCNumber__c == null || o.CCNumber__c.equals('')){
		
	
o.CCNumber__c = [Select CCNumber__c from Account  Where Id = :o.AccountId].CCNumber__c ;
		

	}
	}
}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

gwestongweston

That was the error and that worked for part of the problem.

 

Is there a way to do a "before load" trigger. I want the field to populate with a credit card number from the account record that the opportunity is initiated from. 

 

Currently the trigger saves the number when i save the opportunity.

 

Thank you

Platy ITPlaty IT

GWeston,

 

The triggers are only fired when the record is saved.  There are a few options I can think of for accomplishing what you're doing-

  • Change your logic a bit so that if the CCN field is null, it gets filled in from the Account in that trigger, and throw an error from the trigger if there is no CCN on the Account.  Users will need to understand that this logic is there (you could add it to the Help text for the CCN number), but this is the easiest and most secure solution.
  • Create a custom list button on Oppotunity to create these Opportunity records, and use this button on the Opportunity related list of your Account page layout instead of the standard New button.  The CCN can be passed as a parameter in the URL using that new button to pre-populate that field on the Opportunity, see this article for details.  For your particular requirement, this makes me a little nervous though because you'll be transmitting a credit card number as clear text (not encrypted) in the URL.
  • Create a VisualForce page to override the new Opportunity page.  The page could be created to populate that CCN when the page loads if the Opportunity is created from an Account record, or anytime the Account is selected in the lookup dialog.  The downside of this solution is that you'll lose the ability to change your new Opportunity page using the standard page layout administration.
Ankit AroraAnkit Arora

I don't think you can show prefill values using triggers. You need to create a custom VFP for "New Opportunity" and override the "New" of opportunity with that page.

 

When you click on "New" from related list of opportunity on account it provides your the Account Id in URL. You can use that accountid to get the bill info using "ApexPages.currentPage().getParameters().get('accid')" .

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Ankit AroraAnkit Arora

Sorry!! This is a cross post. But we are on same page :)

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

gwestongweston

I don't think the url will work because the Credit Card fields on the opportunity are part of an app so I will not have access.