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
Mariappan PerumalMariappan Perumal 

Triggers

Hi All

I have written a trigger on opportunity , it will trigger before insert and.

when the created opportunity's is associated to some campaign ,

then I tried to get campaign vendor(custom object ) which is associated to campaign, and tried to update a field in that custom object.

The trigger which i have written is not working fine and I have pasted that below .

can anyone help me out of this.

Trigger:
trigger vendoramount on Opportunity (before insert)
{
for (Opportunity t : trigger.new)
{
if (t.CampaignId!= null & t.LeadSource=='Web' )
 {
    List<Opportunity> opp=[Select CampaignId ,LeadSource From Opportunity Where CampaignId =:t.CampaignId limit 1 ];
    for(Opportunity o:opp)
    {
    List<CampaignVendor__c> cv=[Select Campaign__c,vendoramount__c From CampaignVendor__c where Campaign__c =:o.CampaignId ];
    for(CampaignVendor__c cc:cv)
    {
    cc.vendoramount__c= t.Amount ;
    }
    }
 }
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Hi,

 

You are not updating the CampaignVendor__c record ,try

 

List<CampaignVendor__c> ccList = new List<CampaignVendor__c>();

then use this list as follows

for(CampaignVendor__c cc:cv)
    {
    cc.vendoramount__c= t.Amount ;

ccList.add(cc);
    }

 

 

update ccList;

 

Also,I see that you are running the SOQL inside for loop which is not recommended,this would hit governor limits,if you are updating more than 100 records.

 

 

 

All Answers

Vinit_KumarVinit_Kumar

Hi,

 

You are not updating the CampaignVendor__c record ,try

 

List<CampaignVendor__c> ccList = new List<CampaignVendor__c>();

then use this list as follows

for(CampaignVendor__c cc:cv)
    {
    cc.vendoramount__c= t.Amount ;

ccList.add(cc);
    }

 

 

update ccList;

 

Also,I see that you are running the SOQL inside for loop which is not recommended,this would hit governor limits,if you are updating more than 100 records.

 

 

 

This was selected as the best answer
ibtesamibtesam
Mariappan PerumalMariappan Perumal

Thanks vinit. 

 

Its working now and i did the logic perfectly but forget to update the list. 

 

It looks great vinit and thanks once again.

 

One more thing , you have specified that we should written down the query in for loop . 

 

can you elaborate on that part . i didn't get that why we should not written like this.

Mariappan PerumalMariappan Perumal

Thanks sam. 

 

It looks good.