Skip to main content
Hi,

 

i have implemented a trigger and a handler class which work after update and it creates a new record. This trigger works only for some users not for all users. I checked the permission things, did not get any clue. I also changed the class to 'without sharing' but it also does not work.

 

I want it to work for all users irrespective permission, hiearchy etc.

 

i am unable to understand. what's going wrong. kindly suggest.

 

Trigger

 

---------------------------

 

trigger TransferSAASRecord on Opportunity (after insert, after update) {

 

    if(trigger.isafter && trigger.isupdate && TransferSAASRecordHandler.firstRun){

 

          TransferSAASRecordHandler.newInititationOpp(Trigger.new);

 

          TransferSAASRecordHandler.firstRun = false;

 

}

 

    if(trigger.isafter && trigger.isinsert){

 

        TransferSAASRecordHandler.sendNotification(trigger.new);

 

    }

 

}

 

Apex Class : 

 

public without sharing class TransferSAASRecordHandler {

 

    public static boolean firstRun = true;

 

    public static void newInititationOpp(List<opportunity>Opp){

 

        List<opportunity>newInitOpp = new List<opportunity>();

 

        set<id> ids = new set<id>();

 

        for(opportunity oldopp : opp){

 

            ids.add(oldopp.Id);

 

        }

 

        for(Opportunity o: [select id, name, stageName, ownerId, createdDate, FC_Opportunity_Status__c, RecordtypeId, recordtype.name, ForecastCategoryName,sekund_re_Leadquelle__c, terti_re_Leadquelle__c, closeDate, leadsource, AccountId, Account.name, currencyIsoCode, kommentar_zur_phase__c, LostReason__c, BDR_Recyceln__c, PrimaryContact__c from Opportunity where recordtype.name =: 'SAAS' AND Id IN: ids ]){

 

            if(o.stageName == 'Geschlossene und gewonnene' && o.RecordType.name == 'SAAS' && o.FC_Opportunity_Status__c == 'New Licence' ){

 

                Opportunity oppty = new opportunity();

 

                oppty.Name = O.Account.Name +' ' + 'Media';

 

                string S = oppty.Name;

 

                system.debug(S);

 

                //oppty.RecordType.name = 'Initiation';0122p000000t5g4AAA

 

                oppty.RecordTypeId = '0122p000000t5g4AAA';

 

                oppty.StageName = 'Pre Analysis';

 

                oppty.LostReason__c = o.LostReason__c;

 

                oppty.Kommentar_zur_Phase__c = o.Kommentar_zur_Phase__c;

 

                oppty.CloseDate = o.CloseDate.addmonths(2);

 

                //Oppty.BDR_Recyceln__c = o.BDR_Recyceln__c;

 

                //oppty.Account.name = o.Account.name;

 

                oppty.AccountId = o.AccountId;

 

                oppty.CurrencyIsoCode = o.CurrencyIsoCode;

 

                //oppty.Name = o.Name;

 

                oppty.ForecastCategoryName = o.ForecastCategoryName;

 

                oppty.LeadSource = o.LeadSource;

 

                oppty.sekund_re_Leadquelle__c = o.sekund_re_Leadquelle__c;

 

                oppty.terti_re_Leadquelle__c = o.terti_re_Leadquelle__c;

 

                oppty.OwnerId = '0052p000009pHKVAA2';//'0052p000009pHKVAA2';

 

                oppty.PrimaryContact__c = o.PrimaryContact__c;

 

                newInitOpp.add(oppty);

 

                system.debug(NewInitOpp);

 

                /*

 

                 *

 

  

 

  Close Date: "CloseDate of original Opp + 2 Months" 0060E00000o5MlhQAE

 

  Lead Source: Copy from original Opp

 

  Sec. Lead Source: Copy from original Opp

 

  Tertery Lead Source: Copy from original Opp

 

  Primary Contact: Copy from original Opp

 

  Currency: Copy from original Opp

 

 */

 

            }

 

        }

 

        try{

 

            insert newInitOpp;

 

            system.debug(newInitOpp);

 

        }

 

        catch(system.DmlException e){

 

            system.Debug(e);

 

        }

 

        

 

        set<Id> idss = new set<id>();

 

        string s;

 

        for(opportunity createdOpp : NewInitOpp){

 

            idss.add(createdOpp.id);

 

            s = createdopp.Id;

 

            string ManagerEmail = createdOpp.Owner.Manager.Email;

 

        }

 

        if(NewInitOpp.size()>0){

 

                 List<Messaging.SingleEmailMessage> mails = new

 

      List<Messaging.SingleEmailMessage>();

 

                 String body = 'A new opportunity was created. kindly click on this link https://XXXXXXXX.lightning.force.com/lightning/r/Opportunity/'+ s +'/view';

 

            for(Id oppId : Idss){

 

                string email = oppId;

 

                string[] toaddresses =  new string[]{'XXXXXXXXXXXX'};//[select email from user where username like '%nishant%'];

 

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

 

                mail.setToAddresses(toaddresses);

 

                mail.setSubject('Automated email: Opportunity created');

 

                mail.setPlainTextBody(body);

 

                mails.add(mail);

 

            }

 

            if(mails.size() > 0){

 

                Messaging.sendEmail(mails);

 

            }

 

            }

 

    }

 

    

 

    public static void sendNotification(List<opportunity>Opp){

 

        string s;

 

        set<id> ids = new set<id>();

 

        for(opportunity oldopp : opp){

 

            ids.add(oldopp.Id);

 

            s = oldopp.id;

 

        }

 

        system.debug(opp);

 

        List<feeditem>notify = new list<feeditem>();

 

        for(Opportunity o: Opp){

 

            system.debug(o);

 

            if(o.OwnerId=='0052p000009pHKVAA2' && O.RecordTypeId == '0122p000000t5g4AAA'){

 

            feeditem sendnotification = new feeditem();

 

            sendnotification.parentId = o.Id;

 

            sendnotification.body = 'Hello,the following Opportunity https://XXXXXXXXXX.lightning.force.com/lightning/r/Opportunity/'+ s + '/view is created. Please have a look and assign it to one of your Team Members, to start Sales Process. ';

 

            sendnotification.Title = o.name;

 

            sendnotification.createdbyId = o.OwnerId;

 

            //sendnotification.LinkUrl = 

 

            notify.add(sendnotification);

 

            system.debug(Notify);

 

          }

 

        }

 

        try{

 

            insert notify;

 

            system.debug(notify);

 

        }

 

        catch(system.DmlException e){

 

            system.Debug(e);

 

        }

 

        

 

}

 

      public void sendNotificationReminder(){

 

        List<feeditem>notify = new list<feeditem>();

 

        List<opportunity> opp = [select id, CreatedbyId, name, ownerId, RecordtypeId from Opportunity where recordtype.name =: 'Initiation']; 

 

            string s;

 

            //for(opportunity opps : Opp){

 

              //  s = opps.Id;

 

           // }

 

        for(Opportunity o: Opp){

 

            if(o.OwnerId=='0052p000009pHKVAA2' && O.RecordTypeId == '0122p000000t5g4AAA' && o.CreatedById != '0052p000009pHKVAA2'){

 

            feeditem sendnotification = new feeditem();

 

            s = o.Id;

 

            sendnotification.parentId = o.Id;

 

            sendnotification.body = 'Hello,the following Opportunity https://XXXXXXXX.lightning.force.com/lightning/r/Opportunity/'+ s + '/view is still assigned to you. Please have a look and assign it to one of your Team Members, to start Sales Process. ';

 

            sendnotification.Title = o.name;

 

            sendnotification.createdById = o.OwnerId;

 

            notify.add(sendnotification);

 

            system.debug(Notify);

 

          }

 

        }

 

        try{

 

            insert notify;

 

            system.debug(notify);

 

        }

 

        catch(system.DmlException e){

 

            system.Debug(e);

 

        }

 

}

 

}
2 risposte
  1. 25 feb 2021, 13:00
    Hi Guy,

     

    Thanks for your kind response. The error persist only for first method while creating an opp. rest is working fine. while creating an opp, it throws an error for some users.
0/9000