0

I got this message.

" You encountered some errors when trying to save this record There's a problem saving this record. You might not have permission to edit it, or it might have been deleted or archived. Contact your administrator for help ".

I put trigger on 'Closed Won' but got error message. Than change trigger on previous stage to test. I Got same error for that stage too and 'Closed Won' start working fine. I am stuck in this. I have 'System Administrator' profile. My trigger is working quit fine in my trailhead org. here, i am initiating POST callout upon trigger response. I am using Enterprise Edition.

Any input will be highly appreciated.

trigger won_opportunity on Opportunity (after update) {

    List<Opportunity> opp = new List<Opportunity>();
    for(Opportunity opp : trigger.new)
    {
        if(opp.StageName == 'Closed Won'){
            //Negotiation
            System.debug('id is: ' + opp.Id);
            String oppID = opp.Id;

            opp = [SELECT Name, Id,City__c, BIC_SWIFT_code__c, Building__c FROM Opportunity]; //Limit 1
            String json_opp = JSON.serialize(opp);
            Opportunity_Post.callout(json_opp);
        }
    }
}
  • Please include the relevant code in the body of your question. Welcome to SFSE! – David Reed Mar 30 at 14:00
  • @DavidReed code also added . check it. – Umer Farooq Mar 30 at 14:13
2

In my opinion, the compiler ought to complain about this code, although it does not. You're doing a couple of things here that change the behavior of your code in ways you might not expect.

List<Opportunity> opp = new List<Opportunity>();
for(Opportunity opp : trigger.new)

Here, you've shadowed a declaration of a List<Opportunity> with a declaration of an Opportunity. You should probably remove the list declaration since, as we'll see below, you don't need it.

{
    if(opp.StageName == 'Closed Won'){
        //Negotiation
        System.debug('id is: ' + opp.Id);
        String oppID = opp.Id;

You're not using this variable, so you can safely remove it.

        opp = [SELECT Name, Id,City__c, BIC_SWIFT_code__c, Building__c FROM Opportunity]; //Limit 1

This is the big problem, and I suspect it's what leads to the error you're pointing to once Opportunity_Post.callout() is called. Here, you're just querying every Opportunity in your database, and assigning the result to a single Opportunity variable. If you have multiple records present in your database, that'll get you a QueryException.

Since you're in after update context and not using any cross-relationship fields, all of this data will already be present on the records in Trigger.new. In other words, you don't need to query again, unless you're trying to mutate these records (in after update, the trigger sObjects are read-only). You can serialize either one or all of the trigger's sObjects and pass them to your method directly.

        String json_opp = JSON.serialize(opp);
        Opportunity_Post.callout(json_opp);

I assume that callout() is a future method or a synchronous method that itself calls a future methd. Be aware that the transaction limit for future method calls is 50. If your trigger is called with more than 50 records in a transaction, you will get a LimitException and the whole transaction will be rolled back. Think carefully about your integration architecture here, because it has very real consequences for the future of your org.

  • Thanks @David Reed. It helps me a lot and my problem is solved now. – Umer Farooq Mar 30 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.