You need to sign in to do that
Don't have an account?
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 ;
}
}
}
}
}
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
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.
https://success.salesforce.com/questionDetail?qId=a1X30000000fBjTEAU
I replied in customer community.
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.
Thanks sam.
It looks good.