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
Shree KShree K 

Need help on writing sample trigger

Hi All,
Trying to write few triggers while learning but facing few issues to make them work as expected, below is the trigger code and the issues/errors Please feel free to provide your inputs as comments.

Trigger 1:

 /* Challenge: whenever a contact is either inserted or updated with an account,
 the Primary_Account__c (Text field on contact) has to be updated with account name(text formate),
 I am unable to convert AccountId to Account Name (as text). */ 

Trigger2:

 /* Challege is to query only Accounts which have Opportunities and 
 whenever an account is either inserted or updated with its Primary_Account__c(checkbox field)=True then its related 
 Opportunity which has a Primary_Account__c(Text field) has to be updated as 'Yes'.
    
 I am Trying to query and Iterate over only accounts which have Opps in this trigger,
 Please feel free to better up the query if it is not properly written,
 but in a way that it retrieves only accounts which have Opps. */
//Trigger 1

Trigger ContactTrigger on Contact (before insert,before update){

list <Contact> Cons = New List<Contact>();

list <Contact> ConsWithAccts = [select Id,Name,AccountId from Contact where Id IN:trigger.New AND AccountId!=Null];
for (contact c:ConsWithAccts){
    if (ConsWithAccts.size()>0){
  
      //I am stuck here.
}
}
}

// Trigger 2:

trigger UpdateRelatedOpp on Account(after insert,after update) {
     
    List<Opportunity> oppList = new List<Opportunity>();
    for (Account a : [SELECT Id,Name,(select Id,AccountId from Opportunities) FROM Account WHERE Primary_Account__c = True AND Id IN :Trigger.New ]) {
        
        //I'm Stuck here.
    }
    
    if (oppList.size() > 0) {
        update oppList;
    }
 }
Best Answer chosen by Shree K
Maharajan CMaharajan C
Hi Shree,

Please find the below updated triggers :

Trigger 1:

Trigger ContactTrigger on Contact (before insert,before update){
    set<id> accIds = new set<id>();
    for (contact c:Trigger.New){
        if(c.AccountId != null)
        {
            accIds.add(c.AccountId);
        }
    }
    Map<Id,Account> AccountMap = new Map<Id,Account>([Select Id,Name from Account where Id IN: accIds]);
    if(!AccountMap.isEmpty())
    {
        for (contact c:Trigger.New){
            c.Primary_Account__c= AccountMap.get(c.AccountId).Name;
        }
    }
}

Trigger 2:

trigger UpdateRelatedOpp on Account(after insert,after update) {
    List<Opportunity> oppList = new List<Opportunity>();
    for (Account a : [SELECT Id,Name,(select Id,AccountId,Primary_Account__c from Opportunities) FROM Account WHERE Primary_Account__c = True AND Id IN :Trigger.New ]) {
        for(Opportunity opp:a.Opportunities){
            Opportunity o = new Opportunity(id = opp.Id, Primary_Account__c ='Yes');
            oppList.add(o);
        }
    }
    
    if (oppList.size() > 0) {
        update oppList;
    }
 }

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Shree,

Please find the below updated triggers :

Trigger 1:

Trigger ContactTrigger on Contact (before insert,before update){
    set<id> accIds = new set<id>();
    for (contact c:Trigger.New){
        if(c.AccountId != null)
        {
            accIds.add(c.AccountId);
        }
    }
    Map<Id,Account> AccountMap = new Map<Id,Account>([Select Id,Name from Account where Id IN: accIds]);
    if(!AccountMap.isEmpty())
    {
        for (contact c:Trigger.New){
            c.Primary_Account__c= AccountMap.get(c.AccountId).Name;
        }
    }
}

Trigger 2:

trigger UpdateRelatedOpp on Account(after insert,after update) {
    List<Opportunity> oppList = new List<Opportunity>();
    for (Account a : [SELECT Id,Name,(select Id,AccountId,Primary_Account__c from Opportunities) FROM Account WHERE Primary_Account__c = True AND Id IN :Trigger.New ]) {
        for(Opportunity opp:a.Opportunities){
            Opportunity o = new Opportunity(id = opp.Id, Primary_Account__c ='Yes');
            oppList.add(o);
        }
    }
    
    if (oppList.size() > 0) {
        update oppList;
    }
 }

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
Shree KShree K
Hi Maharajan,
Happy New Year. Thanks for the quick turnaround, Trigger 2 is working but i am facing error for Trigger 2
"Error: Compile Error: Variable does not exist: c at line 16 ",
I tried couple of ways but no luck,Could you pleass modify it when you have sometime?  and once this is done my next step is to expand the trigger by using all the possible trigger context variables, hope you wouldn't mind  helping me, if i am stuck at anypoint.

I appreciate your time.
Maharajan CMaharajan C
Error in Trigger 2 or Triiger 1 ?
Shree KShree K
sorry (Trigger 1), Trigger ContactTrigger on Contact 
error for the line //c.Primary_Account__c= AccountMap.get(c.AccountId).Name;
"Error: Compile Error: Variable does not exist: c at line 16 "
Maharajan CMaharajan C
Hi Shree,

Happy New Year!!!

Am not seeing any issues in my developer org.It's working fine.

Can you pls delete the trigger and resave it or try with some other org.

If you have added any ne new lines in the trigger then post your updated code  here
Shree KShree K
Yes, It worked Maharajan, Thanks a lot.