+ Start a Discussion
DamsosDamsos 

Variable WhatId does not exist

Hello everyone !

I'm settling in this community as I've recently been given the task of implementing Salesforce for my company.

As I'm learning the ropes of administrating an Org I'm also getting interested in learning APEX.

So I've attempted to write simple triggers, which has worked out quite well until now.

Now, I'm attempting to trigger the creation of a Task whenever a Opportunity is created.

I want to link that task to that opportunity, to do just that I wanna use (api name) WhatID from the task fields.

Although I'm running into an error on that perticuliar line;;
 
trigger CadoSupere on Opportunity (after insert) {
    for (Opportunity opp : trigger.new) {
        Task t          = new Task();
        opp.WhatId      = opp.Id;
        opp.Description = 'Je vous offre un supere cado';
        insert t;
    }
}

Line 4: Variable does not exist

I've checked the field and it seems Field Lever Security is set to visible on that WhatId field.

Is there something obvious I'm missing ?

Thanks a bunch, have a nice day and I hope to help others when I get good :)
Best Answer chosen by Damsos
CharuDuttCharuDutt
Hii Damien Ronteix 
I've Made Some Changes In Your Code Try Now
trigger CadoSupere on Opportunity (after insert) {
    for (Opportunity opp : trigger.new) {
        Task t          = new Task();
        t.WhatId      = opp.Id;
        t.Description = 'Je vous offre un supere cado';
        insert t;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Damien Ronteix 
I've Made Some Changes In Your Code Try Now
trigger CadoSupere on Opportunity (after insert) {
    for (Opportunity opp : trigger.new) {
        Task t          = new Task();
        t.WhatId      = opp.Id;
        t.Description = 'Je vous offre un supere cado';
        insert t;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
PriyaPriya (Salesforce Developers) 

Hi Damien,

In the line 4, you are using the variable of opportunity, you have to use task variable instead.

Regadrs,

Priya Ranjan

Suraj Tripathi 47Suraj Tripathi 47
Hi Damien Ronteix,
Greetings
 
trigger CadoSupere on Opportunity (after insert) {
    for (Opportunity opp : trigger.new) {
        Task t          = new Task();
        t.WhatId      = opp.Id;
        t.Description = 'Je vous offre un supere cado';
        insert t;
    }


Please mark as the best answer if this help you! 

Thanks and regards,
Suraj Tripathi.
DamsosDamsos
Thank you so much everyone :-)