The code itself doesn't give any errors, but anytime I run it Trailhead gives me this message:
"Challenge not yet complete... here's what's wrong:
Executing the trigger did not work as expected. "
Here are the instructions:
For this challenge, you need to create a trigger that, before insert or update, checks for a checkbox. If the checkbox field is true, it sets the Shipping Postal Code (whose API name is ShippingPostalCode) to be the same as the Billing Postal Code (BillingPostalCode).
The Apex trigger must be called AccountAddressTrigger.
The Account object will need a new custom checkbox that should have the Field Label 'Match Billing Address' and Field Name of Match_Billing_Address. The resulting API Name should be Match_Billing_Address__c.
With AccountAddressTrigger active, if an Account has a Billing Postal Code and Match_Billing_Address__c is true, the record should have the Shipping Postal Code set to match on insert or update.
My code:
trigger AccountAddressTrigger on Account (before insert,before update) {
for(Account a : [SELECT Id FROM Account WHERE Match_Billing_Address__c = TRUE AND BillingPostalCode != NULL])
{
a.ShippingPostalCode = a.BillingPostalCode;
update a;
}//end for
}
Your Trigger is like this way.
trigger AccountAddressTrigger on Account (before insert,before update) {
//Iterate all accounts that is updated or inserted.
for(Account acc :Trigger.New){
//if match is true set values.
if(acc.Match_Billing_Address__c){
acc.ShippingPostalCode = acc.BillingPostalCode;
}
}
}
trigger AccountAddressTrigger on Account (before insert,before update) {
for(Account a : Trigger.new){
If (a.Match_Billing_Address__c == true && a.BillingPostalCode!=Null) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
First need to create check box field with name Match billing address in Account tab then open developer console and write the code and save it .Finally check that whether its working or not again in ur salesforce instance
here is the code :
trigger accountAddressTrigger on Account (before insert, before update) {
for(Account acct : trigger.new){
if(acct.Match_Billing_Address__c == true)
acct.ShippingPostalCode = acct.BillingPostalCode;
}
}
Trigger Name_of_Trigger on Object (Event 1,Event 2) {
for(Each Object's Event1/Event2) {
if (Check box is selected) {
Assign Billing address to Shipping address (i.e using '=' operator);
}
}
}
Related
Good day!
I would like to create a Script on Google Sheets that will take the name (as an ID) from the "Your Name" column and tie it to an email address so that once the "x" on the "Completed" column is added, it will automatically send an alert to the email address of the person who made the request.
Screenshot of the Information collected
(the data is filled in Spanish, my apologies)
This should get you started
function onMyEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Your sheet name" && e.range.rowStart > 1 && e.range.columnStart == 7 && e.value.toLowerCase() == "x") {
GmailApp.sendEmail(e.user.email,"Subject","Message");
}
}
I think it should be an installable onEdit since sending emails requires permission
Here is my code but not working-
trigger trgOnAccount on Account (before insert) {
list<string>nameList=new list<string>();
list<account>names=new list<account>();
for(account ac:trigger.new){
nameList.add(ac.name);
}
map<string,account>accMap=new map<string,account>([select name from account where name IN:nameList]);
for(account ac:trigger.new){
if(accMap.containsKey(ac.name)){
names.add(accMap.Values());
}
}
delete names;
}
I am trying to check if Account field changed from a specific value.
If old Account's flag was "Open" and new Account flag is not open
Then perform some check.
private void doCheck(map<Id, Account> oldAccounts, map<Id, Account> newAccounts){
List<Account>newList= new List<Account>();
List<Account>oldList= new List<Account>();
oldList= oldAccounts.values();
newList= newAccounts.values();
//check if old account flag changed from "open"
}
private void doCheck(map<Id, Account> oldAccounts, map<Id, Account> newAccounts){
for(Id i : oldAccounts.keyset()){
Account old = oldAccounts.get(i).Flag__c;
if(old.Flag__c == 'Open' && old.Flag__c != newAccounts.get(i).Flag__c){
// do your magic here
}
}
}
If you're going to run some queries for that - you shouldn't make queries in a loop. Some helper Set<Id> where you'd put the Ids and later run the queries for all Account Id's in that set might be best idea... hard to say what exactly do you need.
the keyset() and get(someKey) methods from Map collection are described in Apex help: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_map.htm#apex_System_Map_methods
I noticed the following bug:
When I add an address to a customer using the admin backend, or if I change an address and i save the customer, the Magento Costumer AccountController sends a standard email to the updated customer. The email template used is the template for the customer event "confirmed". This always happens when I update the customer.
Had someone the same problem or a solution for this? I can't understand why magento sends an email for this event...
Class: Mage_Adminhtml_CustomerController extends Mage_Adminhtml_Controller_Action
Methode: saveAction()
Solution: It's a core bug from older versions. The condition for sending a mail after saving a customer uses isset($sendPassToEmail). But if you notice, the sendPassToEmail variable is always set and has the values true or false. Because of the isset() the condition is always true and an email will be send every time a customer is saved.
...
$sendPassToEmail = false;
// force new customer active
if ($isNewCustomer) {
$customer->setPassword($data['account']['password']);
$customer->setForceConfirmed(true);
if ($customer->getPassword() == 'auto') {
$sendPassToEmail = true;
$customer->setPassword($customer->generatePassword());
}
}
Mage::dispatchEvent('adminhtml_customer_prepare_save', array(
'customer' => $customer,
'request' => $this->getRequest()
));
$customer->save();
// send welcome email
if ($customer->getWebsiteId() && (!empty($data['account']['sendemail']) || isset($sendPassToEmail))) {
$storeId = $customer->getSendemailStoreId();
if ($isNewCustomer) {
$customer->sendNewAccountEmail('registered', '', $storeId);
}
// confirm not confirmed customer
else if ((!$customer->getConfirmation())) {
$customer->sendNewAccountEmail('confirmed', '', $storeId);
}
}
I'm trying to see if it's possible for a field in Opportunity to be updated (a checkbox to be checked true) when someone completes a related task. Is there a way to do this?
I don't think this can be done with cross-object workflow, since the WhatId field of a Task is a "polymorphic key". If I'm right, you'll have to use a Trigger on Task.
As Jeremy said you'll need a trigger, code will look something like (I've not checked field names etc. so treat this as almost-real pseudo code)!
trigger TaskAfterInsertUpdate on Task (after update, after insert)
{
list<opportunity> liOpportunities = new list<opportunity>();
list<id> liIDs = new list<id>();
for(Task sTask : trigger.new)
{
if(sTask.Status == 'Complete' && ('' + sTask.WhatId).startsWith('006'))
{
liIDs.add(sTask.WhatId);
}
}
for(Opportunity sOppty : [select Id, CheckBoxField__c from Opportunity where Id in : liIDS])
{
sOppty.CheckBoxField__c = true;
liOpportunities.add(sOppty);
}
update liOpportunities;
}
Hope this is of some help!