You need to sign in to do that
Don't have an account?
Francois Roux
Trigger: update Account field from custom object (lookup)
I'm relatively new to Apex: I have a custom object with a lookup relationship to Accounts, and need to update an account field when a field from a custom object is populated with a value. I need a trigger. Any help?
Below is the test class for that trigger with 100% code coverage.
Thanks,
Akhil
All Answers
can u provide ur requirement with more details like which field of account u want to update and what is ur custom object nd field name.
and also u can refer this link for more help
http://salesforce.stackexchange.com/questions/45232/need-help-writing-a-custom-object-trigger
Thanks
shephali
You can refer the below example. In the Below example Parent__c object has a lookup relation to Account object and the "From Lead" field in the Account object is updated with 'Yes' value everytime it's related Parent__c object record is updated with a Child_count__C equal to 2.
Kindly mark it as an answer if that suffices your requirement.
Please find your required trigger below and make sure that you have properly replace all the fields with their API name.
Please do the changes as suggested and let me know if you have any issue.
Thanks,
Abhishek
We are using triggers here to update the related account.
Both the answers have thier own significance but at the end they both meet your requirement.
I have misseed one thing in my trigger code which i have corrected now.
Please find the updated trigger code below :
We have to update the values of other object only after the creation and insertion of our record successfully so i have changed the events of trigger from before to after.
Now it depends totally upon you whose method you like the most. :)
Thanks,
Abhishek
Below is the test class for that trigger with 100% code coverage.
Thanks,
Akhil
The test gets saved and passed, but the trigger gets Code Coverage 0% in the sandbox. Am I missing something?
Francois
Custom object Fundraise__c has a lookup relationship to Account object and to Fund_c object. The trigger updates FundABC__c, FundDEF__c and Fund__GHI checkbox fields in Account object with 'true' everytime Fundraise__c gets created or updated with field Fund__c equal to FundABC, FundDEF or FundGHI respectively.
Trigger:
trigger updateAcct1 on Fundraise__c (after update) {
Set<Id> acid = new Set<Id>();
for(Fundraise__c temp0:Trigger.New) {
acid.add(temp0.Account__c);
}
Map<Id,Account> act = new Map<Id,Account>([Select Id from Account where Id in :acid]);
Set<Account> acts = new Set<Account>();
List<Account> acts1 = new List<Account>();
for(Fundraise__c temp:Trigger.New) {
if(temp.Fund__c == 'FundABC') {
if(act.containsKey(temp.Account__c)) {
Account upd = act.get(temp.Account__c);
upd.FundABC__c = true ;
acts.add(upd);
}
}
if(temp.Fund_Copy__c == 'FundDEF') {
if(act.containsKey(temp.Account__c)) {
Account upd = act.get(temp.Account__c);
upd.FundDEF__c = true ;
acts.add(upd);
}
}
if(temp.Fund_Copy__c == 'FundGHI') {
if(act.containsKey(temp.Account__c)) {
Account upd = act.get(temp.Account__c);
upd.FundGHI__c = true ;
acts.add(upd);
}
}
}
acts1.addAll(acts);
update acts1;
}
Test:
@isTest
public class testUpdateAcct1 {
static testmethod void invokeTrigger() {
Account ac = new Account();
ac.Name = 'Test Account';
Database.SaveResult sr = Database.insert(ac, false);
if(sr.isSuccess()) {
Fundraise__c p = new Fundraise__c();
p.Name = 'Essai';
p.Account__c = sr.getId();
Database.SaveResult sr1 = Database.insert(p, false);
if(sr1.isSuccess()) {
Id pid = sr1.getId();
Fundraise__c p1 = [Select Id, Name from Fundraise__c where Id =:pid];
p1.Fund_Copy__c = 'FundABC';
update p1;
p1.Fund_Copy__c = 'FundDEF';
update p1;
p1.Fund_Copy__c = 'FundGHI';
update p1;
p1.Fund__c = '';
update p1;
}
}
}
}
Please try with below test class :
Let me know if you have any issue.
Thanks,
Abhishek,
I corrected my mistake: only one field, Fund__c, but test does not pass, so I did not even get to the 0 code covered stage. Any suggestion?
Francois