You need to sign in to do that
Don't have an account?

Error in Simple Trigger
Hi,
I'm very much new to apex and salesforce...has been only few days..Have little knowledge in programming..
kind of stuck writting my first trigger.Please can anyone help..
I'm writting a trigger that updates every accounts Account.Master_Score__c ..it searches all its contacts score_card__c field ..gets the max value present and updates..I have made the code very complex...now getting error in last line of code..pls help me completing
error: initial term of field expression must be concrete Sobject : list<id>
trigger trigger_new on Contact (after insert, after update)
{
List<Id> contlist = new List<Id>();
List<Id> contlist1 = new List<Id>();
List<Id> contlist4 = new List<Id>();
Map<Id, Contact>newmap= new Map<Id, Contact>();
for (Contact c : trigger.new)
{
while( c.score_card__c >0 )
{
contlist.add(c.id);
for(contact ca : contlist)
{
if(ca.Account.Id=Account.id)
{
contlist1.add(ca.id);
List<contact> contlist2=[select id,score_card__c from contact where id in : contlist1];
for(contact c2: contlist2)
{
for(Integer i=0;i<=contlist2.size();i++)
{
List<AggregateResult> contlist3=[select MAX(score_card__c) from contact where id in : contlist2];
contlist4.add(c2.id);
c2.Account.Master_Score__c= contlist4.score_card__c;
}
}
}
}
}
}
}
cool .....
here to go ..... :)
Thanks,
bForce
All Answers
Could you please post your requirement in details,
ALso post all related fields API name , so we can help you to build apex script.
Anyways below line causing error for you
for(contact ca : contlist)
It is expecting contlist should be list of contacts , in your scene it is list of Ids
Thanks,
bForce
Thanks..:)
Account object has field Master_Score__c
all its contacts has score_card__c field...both are of number data type..
every contact gets a score say score_card__c of contact 1=10, contact2=20, contact3=30...(which is entered manually)
account must take max of that value..in this case it shud take 30 as its master_score value
i shud write a trigger for this that whenever there is update in contact or new contact is created check the fields and suitable update accounts master score
you need to define trigger on contact (after insert , after update)
Query all contacts related to that Account,
Choose max score value
Copy this into account
update account
are you expecting trigger code ??
thanks,
bForce
As i said im very new to apex..Learning and coding simultaneously..
i found mistakes i was doing..
refined code and have written below...can u point out mistake i have done here ..
trigger trigger_new on Contact (after insert, after update)
{
List<contact> contlist = new List<contact>();
List<Id> contlist1 = new List<Id>();
List<Id> contlist4 = new List<Id>();
// Map<Id, Contact>newmap= new Map<Id, Contact>();
for (Contact c : trigger.new)
{
while( c.score_card__c >0 )
{
if(c.Account.Id=Account.id)
{
contlist.add(c);
//List<contact> contlist2=[select id,score_card__c from contact where id in : contlist1];
for(contact c2: contlist)
{
for(Integer i=0;i<=contlist.size();i++)
{
List<AggregateResult> contlist2=[select MAX(score_card__c) from contlist];
// contlist1.add(c2);
c2.Account.Master_Score__c= contlist2.score_card__c;
}
}
}
}
}
}
Yes, doing the same thing..
while loop to check if contacts has score_card >0
then link contact wit its accnt .... if(c.Account.Id=Account.id)
add those contacts to a list
for loop those contacts
mostly this line is not needed ..for(Integer i=0;i<=contlist.size();i++)
then List<AggregateResult> contlist2=[select MAX(score_card__c) from contlist];...............which is throwing error with contlist
final line ...c2.Account.Master_Score__c= contlist2.score_card__c; ....
Dont know what to write instead of ..
List<AggregateResult> contlist2=[select MAX(score_card__c) from contlist];
cool .....
here to go ..... :)
Thanks,
bForce
This is very helpful...
Nice code :)...need to learn n improve..
Thank you very much b-force... :)
you are welcome
**
Learn and contribute . Thanks, bForce
Hi,
i tried the same trigger it is working fine, how to write test class for this.
Thanks,
Bujji