function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shahab KhanShahab Khan 

Want to add more than one record at time

Hi,

i want to add 100 records to opportunit and 100 records to opportunityrole
Here is my code

for (Integer i = 1; i <= 100; i++)
{
    opportunity = null;
    role = null;
    opportunity = new Opportunity();
    role = new OpportunityContactRole();
    
    opportunity.name = account.name + ' Donation (' + i + ' of 5) ' + current_date;
    opportunity.Donation_Type__c = contact.Donation_Type__c;
    opportunity.Payment_Type__c = payment_type;
    opportunity.amount = Integer.valueof(contact.Donataion_Amount__c);
    opportunity.CloseDate = re_today_date;
    opportunity.stageName = 'Pledged';
    oppList.add(opportunity);
    insert opportunity;
    
    // Then create opportunity role
    role.opportunityId = opportunity.id;
    if(is_anonymous == false)
    {
        role.contactId = contact.id;
    }
    else
    {
        role.contactId = prev_contact_id;
    }
    role.Role = '10';
    insert role;
}


With this code i got the following exception.

User-added image

Please let me know the best way how to fix this.

Thanks in advance
Best Answer chosen by Shahab Khan
Denis VakulishinDenis Vakulishin
Sorry, 
I'm wondering how I couldn't see that.
You cannot compare ID and String. 
Try to replace this line 
if(opp.id != null && opp.id !='')

with this 
if(opp.id != null)


All Answers

Denis VakulishinDenis Vakulishin
Here's code.
Don't forget about bulkification. https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
List<OpportunityContactRole> ocrs = new List<OpportunityContactRole>();
List<Opportunity> oppies = new List<Opportunity>();
for (Integer i = 1; i <= 100; i++)
{
    opportunity = null;
    role = null;
    opportunity = new Opportunity();
    
    
    opportunity.name = account.name + ' Donation (' + i + ' of 5) ' + current_date;
    opportunity.Donation_Type__c = contact.Donation_Type__c;
    opportunity.Payment_Type__c = payment_type;
    opportunity.amount = Integer.valueof(contact.Donataion_Amount__c);
    opportunity.CloseDate = re_today_date;
    opportunity.stageName = 'Pledged';
    oppList.add(opportunity);
    oppies.add(opportunity);
}

insert oppies;
for(Opportunity opp : oppies)
{
    // Then create opportunity role
    role = new OpportunityContactRole();
    role.opportunityId = opp.id;
    if(is_anonymous == false)
    {
        role.contactId = contact.id;
    }
    else
    {
        role.contactId = prev_contact_id;
    }
    role.Role = '10';
    ocrs.add(role);
}
insert ocrs;


Shahab KhanShahab Khan
Thank you very much Denis for your quick reply.
I have used the same code as below
List<OpportunityContactRole> ocrs = new List<OpportunityContactRole>();
List<Opportunity> oppies = new List<Opportunity>();
for (Integer i = 1; i <= 100; i++)
{
    opportunity = null;
    role = null;
    opportunity = new Opportunity();
    
    
    opportunity.name = account.name + ' Donation (' + i + ' of 5) ' + current_date;
    opportunity.Donation_Type__c = contact.Donation_Type__c;
    opportunity.Payment_Type__c = payment_type;
    opportunity.amount = Integer.valueof(contact.Donataion_Amount__c);
    opportunity.CloseDate = re_today_date;
    opportunity.stageName = 'Pledged';
    oppies.add(opportunity);
}

insert oppies;
for(Opportunity opp : oppies)
{
    // Then create opportunity role
    role = new OpportunityContactRole();
    role.opportunityId = opp.id;
    if(is_anonymous == false)
    {
        role.contactId = contact.id;
    }
    else
    {
        role.contactId = prev_contact_id;
    }
    role.Role = '10';
    ocrs.add(role);
}
insert ocrs;


Now getting the following exception

User-added image


Please help me.

Very thanks in advance.

Shahab KhanShahab Khan
I think the error ia about following line number 24

role.opportunityId = opp.id;


Shahab KhanShahab Khan
Cany any body help me on this issue?
Do we need to add triggers?

Denis can you please help me more i am new to SalesForce
Thanks in advance.
Denis VakulishinDenis Vakulishin
One question:
Are you sure that this line is correct?
role.Role = '10';

Role is picklist value.It should be look like 'Decision Maker' or 'Business User'
Denis VakulishinDenis Vakulishin
Also,
What type have prev_contact_id variable ?
Shahab KhanShahab Khan
Thanks for your quick reply Denis
Yes the line is correct
role.Role = '10';

and prev_contact_id variable has contact id having type String
Denis VakulishinDenis Vakulishin
Could you give me the code where you're assigning prev_contact_id value ?
Shahab KhanShahab Khan
Here is my complete class code
I am splitting it in two or three replies

public class DonorAdmin
{
    Account account;
    Contact contact, contact_to_use;
    Opportunity opportunity;
    OpportunityContactRole role;
    npe03__Recurring_Donation__c recDonation;
    BluePay_DNA__Rebilling_Cycle__c rebilling_cycle;
    BluePay_DNA__Payment__c bluePayment;
    public String payment_type {get;set;}
    public String current_date {get;set;}
    public Datetime next_date {get;set;}
    public Boolean is_anonymous = true;
    public String prev_account_id = '001M000000ZtSf8IAF';
    public String prev_contact_id = '003M000000TiQ3A';
    //public String prev_account_id = '001F0000019uZqiIAE';
    //public String prev_contact_id = '003F000001UTJl3';
    
    public DonorAdmin()
    {
        account = new Account();
        opportunity = new Opportunity();
        role = new OpportunityContactRole();
        recDonation = new npe03__Recurring_Donation__c();
        bluePayment = new BluePay_DNA__Payment__c();
        rebilling_cycle = new BluePay_DNA__Rebilling_Cycle__c();
        payment_type = 'Credit Card';
        Datetime cDT = System.now();
        next_date = System.now().addHours(1);
        current_date = cDT.format('MM/d/yyyy');
    }
   
    public Contact getContact()
    {
        if(contact == null)
        {
            contact = new Contact();
            contact.MailingState = 'VA';
            contact.MailingCountry = 'USA';
            contact.Branch__c = 'Main Center';
        }
        return contact;
    }
    
    public List<SelectOption> getBranchSelectList(){ return getPickValues(Contact, 'Branch__c', NULL); }
   
    public List<selectOption> getPickValues(Sobject object_name, String field_name, String first_val)
    {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
        if (first_val != null) { //if there is a first value being provided
            options.add(new selectOption(first_val, first_val)); //add the first option
        }
        Schema.sObjectType sobject_type = object_name.getSObjectType(); //grab the sobject that was passed
        Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
        Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
        List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
        for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
            
            options.add(new selectOption(a.getValue(), a.getLabel())); //add the value and label to our final list
        }
        return options; //return the List
    }
    
    public List<selectOption> getCampaignSelectList()
    {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
        //options.add(new selectOption('Select', 'Select'));
        for(Campaign rt:[select id,Name from Campaign Order By Name ASC])
        {
            options.add(new selectOption(rt.id,rt.Name));
        }
        return options; //return the List
    }
    
    public List<SelectOption> getFrequencySelectList()
    {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
        for(Integer i = 1; i <= 100; i++)
        {
            options.add(new selectOption(String.valueOf(i), String.valueOf(i)));
        }
        return options; //return the List
    }
    
    public String StrSaveResult { get; set; }
   
    public PageReference save()
    {
        if((contact.FirstName != null && contact.FirstName != '') && (contact.LastName != null && contact.LastName != '') && (contact.Email != null && contact.Email != '') && (contact.Phone != null && contact.Phone != '') )
        {
            is_anonymous = false;
        }
        if(is_anonymous == false)
        {
            list<Contact> listCon = [select Id, AccountId from Contact where Phone=:contact.phone or Email=:contact.Email limit 1];
            if (listCon.size() > 0) {
                contact_to_use = listCon.get(0);
                prev_contact_id = contact_to_use.id;
                prev_account_id = contact_to_use.AccountId;
                is_anonymous = true;
                
                //StrSaveResult = 'prev_contact_id = ' + prev_contact_id + ' prev_account_id = ' + prev_account_id;
                //return null;
            }
        }
        try
        {
            Account prev_account_name = [select Name from Account where id=:prev_account_id limit 1];
            
            if((contact.FirstName != null && contact.FirstName != '') && (contact.LastName != null && contact.LastName != '') )
            {
                is_anonymous = false;
            }
            StrSaveResult = '';
            if(payment_type != null && payment_type != 'Cash' && payment_type != 'Physical Check')
            {
                if(contact.FirstName == null || contact.FirstName == '')
                {
                    StrSaveResult += 'First name is required.\n<br/>';
                }
                if(contact.LastName == null || contact.LastName == '')
                {
                    StrSaveResult += 'Last name is required.\n<br/>';
                }
                if(contact.MailingStreet == null || contact.MailingStreet == '')
                {
                    StrSaveResult += 'Street is required.\n<br/>';
                }
                if(contact.MailingCity == null || contact.MailingCity == '')
                {
                    StrSaveResult += 'City is required.\n<br/>';
                }
                if(contact.MailingState == null || contact.MailingState == '')
                {
                    StrSaveResult += 'State/Province is required.\n<br/>';
                }
                if(contact.MailingPostalCode == null || contact.MailingPostalCode == '')
                {
                    StrSaveResult += 'Zip/Postal Code is required.\n<br/>';
                }
                if(contact.MailingCountry == null || contact.MailingCountry == '')
                {
                    StrSaveResult += 'Country is required.\n<br/>';
                }
                if(contact.Email == null || contact.Email == '')
                {
                    StrSaveResult += 'Email is required.\n<br/>';
                }
                if(contact.Phone == null || contact.Phone == '')
                {
                    StrSaveResult += 'Phone is required.\n<br/>';
                }
            }
            
            Integer donation_amount = 0;
            
            if(contact.Donataion_Amount__c != null && contact.Donataion_Amount__c != '')
            {
                donation_amount = Integer.valueof(contact.Donataion_Amount__c);
            }
            else
            {
                donation_amount = Integer.valueof(contact.DonataionAmountOther__c);
            }
            
            if(donation_amount > 0 && (contact.Donation_Frequeny__c == null || contact.Donation_Frequeny__c == ''))
            {
                StrSaveResult += 'Please select gift frequency.\n<br/>';
            }
            
            if(contact.Donation_Frequeny__c != null && contact.Donation_Frequeny__c != '')
            {
                if(!(donation_amount > 0) )
                {
                    StrSaveResult += 'Please select gift amount.\n<br/>';
                }
            }
            
            if(payment_type != null && payment_type == 'Credit Card')
            {
                if(contact.Card_Number__c == null || contact.Card_Number__c == '')
                {
                    StrSaveResult += 'Please enter your card number.\n<br/>';
                }
                if(contact.CVV2__c == null || contact.CVV2__c == '')
                {
                    StrSaveResult += 'Please enter your CVV2.\n<br/>';
                }
            }
            else if(payment_type != null && payment_type == 'E-Check')
            {
                if(contact.Routing_Number__c == null || contact.Routing_Number__c == '')
                {
                    StrSaveResult += 'Please enter your routing number.\n<br/>';
                }
                if(contact.Account_Number__c == null || contact.Account_Number__c == '')
                {
                    StrSaveResult += 'Please enter your account number.\n<br/>';
                }
            }
            
            if(StrSaveResult != null && StrSaveResult !='')
            {
                return null;
            }
            
            if(is_anonymous == false)
            {
                // First create account
                account.name = contact.FirstName + ' ' + contact.LastName;
                if(contact.phone != null && contact.phone != '')
                {
                    account.phone = contact.phone;
                }
                else
                {
                    account.phone = '00923346050941';
                }
                
                insert account;
                
                // Then create contact 
                contact.accountId = account.id;
                contact.RecordTypeId = '012F00000013Gk2';
                insert contact;
            }
            if(is_anonymous == true)
            {
                Contact contact_to_update = [select Id, Middle_Name__c, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry, Payment_Type__c, Card_Number__c, Expiration_Month__c, Expiration_Year__c, CVV2__c, Account_Type__c, Routing_Number__c, Account_Number__c from Contact where id=:prev_contact_id limit 1];
                
                if(contact.Middle_Name__c != null && contact.Middle_Name__c != '')
                {
                    contact_to_update.Middle_Name__c = contact.Middle_Name__c;
                }
                if(contact.MailingStreet != null && contact.MailingStreet != '')
                {
                    contact_to_update.MailingStreet = contact.MailingStreet;
                }
                if(contact.MailingCity != null && contact.MailingCity != '')
                {
                    contact_to_update.MailingCity = contact.MailingCity;
                }
                if(contact.MailingState != null && contact.MailingState != '')
                {
                    contact_to_update.MailingState = contact.MailingState;
                }
                if(contact.MailingPostalCode != null && contact.MailingPostalCode != '')
                {
                    contact_to_update.MailingPostalCode = contact.MailingPostalCode;
                }
                if(contact.MailingCountry != null && contact.MailingCountry != '')
                {
                    contact_to_update.MailingCountry = contact.MailingCountry;
                }
                
                update contact_to_update;
                contact_to_update = null;
            }


Shahab KhanShahab Khan
// Enable gateway account
            
            String account_id = '100181797727';
            
            if(contact.Donation_Type__c != null && (contact.Donation_Type__c == 'ADAMS 10/10 Club' || contact.Donation_Type__c == 'ADAMS Operations' || contact.Donation_Type__c == 'Other'))
            {
                //Adams Operations
                //9EOFRZWUHAFHOFMXWHHYWMGLT/Y5.GBT
                account_id = '100181797727';
            }
            else
                if(contact.Donation_Type__c != null && contact.Donation_Type__c == 'Ashburn New Masjid')
            {
                //Ashburn Acquisition
                //CF7GXZN46AU8RMQQ7VCKQF89V5XNIFEO
                account_id = '100181719805';
            }
            else
                if(contact.Donation_Type__c != null && contact.Donation_Type__c == 'Ashburn Operations')
            {
                //Ashburn
                //QNLXPZ3/JSFU8ERRARDW6LBOMWCVMDG5
                account_id = '100181704859';
            }
            else
                if(contact.Donation_Type__c != null && contact.Donation_Type__c == 'Gainesville New Masjid')
            {
                //Gainesville New Masjid
                //NS2ULFHQ2GDUYEE9VAWUAUWWEW3YRCJU
                account_id = '100181686984';
            }
            else
                if(contact.Donation_Type__c != null && contact.Donation_Type__c == 'Gainesville Operations')
            {
                //Gainesville Operations
                //PCPK2DOIVSLQR9ZT78HINNBLOVQA8KPN
                account_id = '100181796062';
            }
            else
                if(contact.Donation_Type__c != null && contact.Donation_Type__c == 'Phase III Construction')
            {
                //PhaseThree
                //OBDNHVA7KCMALRUII7OK8ZMSCQPBPSG8
                account_id = '100181703930';
            }
            else
                if(contact.Donation_Type__c != null && contact.Donation_Type__c == 'Sully Center')
            {
                //Sully Ops
                //VGX6MUHHFACV4PDTTP2KM721NSXE72ML
                account_id = '100181718020';
            }
            else
                if(contact.Donation_Type__c != null && (contact.Donation_Type__c == 'Sadaqa' || contact.Donation_Type__c == 'Zakat' || contact.Donation_Type__c == 'Zakat Ul Fitr'))
            {
                //Zakat
                //0PG850Z/HD.14VMN1SONYWUDPCAAOW.R
                account_id = '100181718495';
            }
            
            BluePay_DNA__BluePay_Gateway_Settings__c accountToUpdate;
            
            if(payment_type != null && payment_type != 'Cash' && payment_type != 'Physical Check')
            {
                accountToUpdate = [SELECT BluePay_DNA__Enabled__c FROM BluePay_DNA__BluePay_Gateway_Settings__c WHERE BluePay_DNA__Enabled__c =True LIMIT 1];
                accountToUpdate.BluePay_DNA__Enabled__c = False;
                update accountToUpdate;
                
                accountToUpdate = null;
                accountToUpdate = [SELECT BluePay_DNA__Enabled__c FROM BluePay_DNA__BluePay_Gateway_Settings__c WHERE BluePay_DNA__Account_ID__c =:account_id LIMIT 1];
                accountToUpdate.BluePay_DNA__Enabled__c = True;
                update accountToUpdate;
            }
            
            // Add Donation
            if(donation_amount > 0)
            {
                if(contact.Donation_Frequeny__c != null && contact.Donation_Frequeny__c == 'Single')
                {
                    opportunity = null;
                    role = null;
                    bluePayment = null;
                    opportunity = new Opportunity();
                    role = new OpportunityContactRole();
                    bluePayment = new BluePay_DNA__Payment__c();
                    
                    if(is_anonymous == false)
                    {
                        opportunity.accountId = account.id;
                        opportunity.name = account.name + ' Donation ' + current_date;
                    }
                    else
                    {
                        opportunity.accountId = prev_account_id;
                        opportunity.name = prev_account_name.Name + ' Donation ' + current_date;
                    }
                    opportunity.CampaignId = contact.Primary_Campaign_Source__c;
                    opportunity.Donation_Type__c = contact.Donation_Type__c;
                    opportunity.Donation_Source_Type__c = contact.Donation_Source__c;
                    opportunity.Payment_Type__c = payment_type;
                    if(contact.Check_Number__c != null)
                    {
                        opportunity.Check_Number__c = contact.Check_Number__c;
                    }
                    opportunity.BluePay_DNA__Donation_Type__c = (contact.Donation_Type__c != null) ? contact.Donation_Type__c : '';
                    opportunity.Other_Donation_Type__c = contact.Other_Gift_Type__c;
                    
                    if(contact.Donataion_Amount__c != null && contact.Donataion_Amount__c != '')
                    {
                        opportunity.amount = Integer.valueof(contact.Donataion_Amount__c);
                    }
                    else
                    {
                        opportunity.amount = contact.DonataionAmountOther__c;
                    }
                    opportunity.CloseDate = Date.today();
                    opportunity.stageName = 'Posted';
                    insert opportunity;
                    
                    // Then create opportunity role
                    role.opportunityId = opportunity.id;
                    if(is_anonymous == false)
                    {
                        role.contactId = contact.id;
                    }
                    else
                    {
                        role.contactId = prev_contact_id;
                    }
                    role.Role = '10';
                    insert role;
                    
                    if(payment_type != null && payment_type != 'Cash' && payment_type != 'Physical Check')
                    {
                        // Add Blue Pay Payment
                        
                        if(is_anonymous == false)
                        {
                            bluePayment.BluePay_DNA__Account_Name__c = account.id;
                            bluePayment.BluePay_DNA__Contact_Name__c = contact.id;
                        }
                        else if(is_anonymous == true)
                        {
                            bluePayment.BluePay_DNA__Account_Name__c = prev_account_id;
                            bluePayment.BluePay_DNA__Contact_Name__c = prev_contact_id;
                        }
                        
                        bluePayment.BluePay_DNA__Opportunity_Name__c = opportunity.id;
                        bluePayment.BluePay_DNA__Address__c = contact.MailingStreet;
                        bluePayment.BluePay_DNA__City__c = contact.MailingCity;
                        bluePayment.BluePay_DNA__State__c = contact.MailingState;
                        bluePayment.BluePay_DNA__Zipcode__c = contact.MailingPostalCode;
                        bluePayment.BluePay_DNA__Email__c = contact.Email;
                        bluePayment.BluePay_DNA__Phone__c = contact.Phone;
                        bluePayment.BluePay_DNA__Comments__c = contact.Member_Comments__c;
                        
                        if(contact.Donataion_Amount__c != null && contact.Donataion_Amount__c != '')
                        {
                            bluePayment.BluePay_DNA__Amount__c = Integer.valueof(contact.Donataion_Amount__c);
                        }
                        else
                        {
                            bluePayment.BluePay_DNA__Amount__c = contact.DonataionAmountOther__c;
                        }
                        
                        bluePayment.BluePay_DNA__Transaction_Type__c = 'Sale';
                        bluePayment.BluePay_DNA__Is_External__c = 'Yes';
                        bluePayment.BluePay_DNA__Is_Recurring__c = 'No';
                        
                        bluePayment.BluePay_DNA__Payment_Type__c = payment_type;
                        
                        if(payment_type != null && payment_type == 'Credit Card')
                        {
                            bluePayment.BluePay_DNA__Card_Number__c = contact.Card_Number__c;
                            bluePayment.BluePay_DNA__Expiration_Month__c = contact.Expiration_Month__c;
                            bluePayment.BluePay_DNA__Expiration_Year__c = contact.Expiration_Year__c;
                            bluePayment.BluePay_DNA__CVV2__c = contact.CVV2__c;
                        }
                        else
                        {
                            bluePayment.BluePay_DNA__Account_Type__c = contact.Account_Type__c;
                            bluePayment.BluePay_DNA__Account_Number__c = contact.Account_Number__c;
                            bluePayment.BluePay_DNA__Routing_Number__c = contact.Routing_Number__c;
                        }
                        insert bluePayment;
                    }
                }

Shahab KhanShahab Khan
else if(contact.Donation_Frequeny__c != null &&  contact.Donation_Frequeny__c == 'Recurring')
                {
                    
                    if(contact.Donataion_Amount__c != null && contact.Donataion_Amount__c != '')
                    {
                        recDonation.npe03__Amount__c = Integer.valueof(contact.Donataion_Amount__c);
                    }
                    else
                    {
                        recDonation.npe03__Amount__c = contact.DonataionAmountOther__c;
                    }
                    recDonation.npe03__Date_Established__c = Date.today();
                    recDonation.npe03__Installment_Period__c = contact.Recurring_Period__c;
                    if(is_anonymous == false)
                    {
                        recDonation.Name = account.name + ' Recurring Donation ' + current_date;
                        recDonation.npe03__Contact__c = contact.id;
                    }
                    else
                    {
                        recDonation.Name = prev_account_name.Name + ' Recurring Donation ' + current_date;
                        recDonation.npe03__Contact__c = prev_contact_id;
                    }
                    insert recDonation;
                    
                    if(payment_type != null && payment_type != 'Cash' && payment_type != 'Physical Check')
                    {
                        // Add Rebilling Cycle
                        
                        rebilling_cycle = null;
                        rebilling_cycle = new BluePay_DNA__Rebilling_Cycle__c();
                        
                        if(is_anonymous == false)
                        {
                            rebilling_cycle.BluePay_DNA__Contact_Name__c = contact.id;
                        }
                        else
                        {
                            rebilling_cycle.BluePay_DNA__Contact_Name__c = prev_contact_id;
                        }
                        
                        if(contact.Donataion_Amount__c != null && contact.Donataion_Amount__c != '')
                        {
                            rebilling_cycle.BluePay_DNA__Rebill_Amount__c = Integer.valueof(contact.Donataion_Amount__c);
                        }
                        else
                        {
                            rebilling_cycle.BluePay_DNA__Rebill_Amount__c = contact.DonataionAmountOther__c;
                        }
                        rebilling_cycle.BluePay_DNA__Rebill_Cycles_Remaining__c = contact.No_of_Gifts__c;
                        
                        if(contact.Recurring_Period__c != null &&  contact.Recurring_Period__c == 'Weekly')
                        {
                            if(is_anonymous == false)
                            {
                                rebilling_cycle.Name = account.name + ' - ' + rebilling_cycle.BluePay_DNA__Rebill_Amount__c + ' - 1Week(s) cycle';
                            }
                            else
                            {
                                rebilling_cycle.Name = prev_account_name.Name + ' - ' + rebilling_cycle.BluePay_DNA__Rebill_Amount__c + ' - 1Week(s) cycle';
                            }
                            rebilling_cycle.BluePay_DNA__Rebill_Frequency_Number__c = '1';
                            rebilling_cycle.BluePay_DNA__Rebill_Frequency__c = 'Week(s)';   
                        }
                        else
                            if(contact.Recurring_Period__c != null &&  contact.Recurring_Period__c == 'Bi-Weekly')
                        {
                            if(is_anonymous == false)
                            {
                                rebilling_cycle.Name = account.name + ' - ' + rebilling_cycle.BluePay_DNA__Rebill_Amount__c + ' - 2Week(s) cycle';
                            }
                            else
                            {
                                rebilling_cycle.Name = prev_account_name.Name + ' - ' + rebilling_cycle.BluePay_DNA__Rebill_Amount__c + ' - 2Week(s) cycle';
                            }
                            rebilling_cycle.BluePay_DNA__Rebill_Frequency_Number__c = '2';
                            rebilling_cycle.BluePay_DNA__Rebill_Frequency__c = 'Week(s)';   
                        }
                        else
                            if(contact.Recurring_Period__c != null &&  contact.Recurring_Period__c == 'Monthly')
                        {
                            if(is_anonymous == false)
                            {
                                rebilling_cycle.Name = account.name + ' - ' + rebilling_cycle.BluePay_DNA__Rebill_Amount__c + ' - 1Month(s) cycle';
                            }
                            else
                            {
                                rebilling_cycle.Name = prev_account_name.Name + ' - ' + rebilling_cycle.BluePay_DNA__Rebill_Amount__c + ' - 1Month(s) cycle';
                            }
                            rebilling_cycle.BluePay_DNA__Rebill_Frequency_Number__c = '1';
                            rebilling_cycle.BluePay_DNA__Rebill_Frequency__c = 'Month(s)';  
                        }
                        else
                            if(contact.Recurring_Period__c != null &&  contact.Recurring_Period__c == 'Annually')
                        {
                            if(is_anonymous == false)
                            {
                                rebilling_cycle.Name = account.name + ' - ' + rebilling_cycle.BluePay_DNA__Rebill_Amount__c + ' - 1Year(s) cycle';
                            }
                            else
                            {
                                rebilling_cycle.Name = prev_account_name.Name + ' - ' + rebilling_cycle.BluePay_DNA__Rebill_Amount__c + ' - 1Year(s) cycle';
                            }
                            rebilling_cycle.BluePay_DNA__Rebill_Frequency_Number__c = '1';
                            rebilling_cycle.BluePay_DNA__Rebill_Frequency__c = 'Year(s)';   
                        }
                        
                        rebilling_cycle.BluePay_DNA__Rebill_Next_Date__c = next_date;
                        rebilling_cycle.BluePay_DNA__Is_External__c = 'Yes';
                        rebilling_cycle.BluePay_DNA__Payment_Type__c = payment_type;
                        
                        if(payment_type != null && payment_type == 'Credit Card')
                        {
                            rebilling_cycle.BluePay_DNA__Card_Number__c = contact.Card_Number__c;
                            rebilling_cycle.BluePay_DNA__Expiration_Month__c = contact.Expiration_Month__c;
                            rebilling_cycle.BluePay_DNA__Expiration_Year__c = contact.Expiration_Year__c;
                        }
                        else
                        {
                            rebilling_cycle.BluePay_DNA__Payment_Type__c = 'Check';
                            rebilling_cycle.BluePay_DNA__Account_Type__c = contact.Account_Type__c;
                            rebilling_cycle.BluePay_DNA__Account_Number__c = contact.Account_Number__c;
                            rebilling_cycle.BluePay_DNA__Routing_Number__c = contact.Routing_Number__c;
                        }
                        insert rebilling_cycle;
                    }
                    Date re_today_date = Date.today();
                    // Add Recurring Donations under Oppertunity
                    
                    List < Opportunity > oppList = new List<Opportunity>();
                    List < OpportunityContactRole > roleList = new List<OpportunityContactRole>();
                    
                    for (Integer i = 1; i <= Integer.valueOf(contact.No_of_Gifts__c); i++)
                    {
                        opportunity = null;
                        role = null;
                        opportunity = new Opportunity();
                        role = new OpportunityContactRole();
                        
                        if(is_anonymous == false)
                        {
                            opportunity.accountId = account.id;
                            opportunity.name = account.name + ' Donation (' + i + ' of 5) ' + current_date;
                        }
                        else
                        {
                            opportunity.accountId = prev_account_id;
                            opportunity.name = prev_account_name.Name + ' Donation (' + i + ' of 5) ' + current_date;
                        }
                        opportunity.Donation_Type__c = contact.Donation_Type__c;
                        opportunity.Payment_Type__c = payment_type;
                        if(contact.Check_Number__c != null)
                        {
                            opportunity.Check_Number__c = contact.Check_Number__c;
                        }
                        opportunity.CampaignId = contact.Primary_Campaign_Source__c;
                        opportunity.BluePay_DNA__Donation_Type__c = (contact.Donation_Type__c != null) ? contact.Donation_Type__c : '';
                        opportunity.Other_Donation_Type__c = contact.Other_Gift_Type__c;
                        opportunity.Donation_Source_Type__c = contact.Donation_Source__c;
                        opportunity.npe03__Recurring_Donation__c = recDonation.id;
                        
                        if(contact.Donataion_Amount__c != null && contact.Donataion_Amount__c != '')
                        {
                            opportunity.amount = Integer.valueof(contact.Donataion_Amount__c);
                        }
                        else
                        {
                            opportunity.amount = contact.DonataionAmountOther__c;
                        }
                        
                        if(i == 1)
                        {
                            opportunity.CloseDate = re_today_date;
                        }
                        else
                        {
                            if(contact.Recurring_Period__c != null &&  contact.Recurring_Period__c == 'Weekly')
                            {
                                re_today_date = re_today_date.addDays(7);
                                opportunity.CloseDate = re_today_date;
                            }
                            else if(contact.Recurring_Period__c != null &&  contact.Recurring_Period__c == 'Bi-Weekly')
                            {
                                re_today_date = re_today_date.addDays(14);
                                opportunity.CloseDate = re_today_date;
                            }
                            else if(contact.Recurring_Period__c != null &&  contact.Recurring_Period__c == 'Monthly')
                            {
                                re_today_date = re_today_date.addMonths(1);
                                opportunity.CloseDate = re_today_date;
                            }
                            else if(contact.Recurring_Period__c != null &&  contact.Recurring_Period__c == 'Yearly')
                            {
                                re_today_date = re_today_date.addYears(1);
                                opportunity.CloseDate = re_today_date;
                            }
                        }
                        
                        opportunity.stageName = 'Pledged';
                        oppList.add(opportunity);
                        //insert opportunity;
                        
                        // Then create opportunity role
                        
                        //insert role;
                    }
                    insert(oppList);
                    for(Opportunity opp: oppList)
                    {
                        role = null;
                        role = new OpportunityContactRole();
                        
                        //role.opportunityId = opportunity.id;
                        if(is_anonymous == false)
                        {
                            role.contactId = contact.id;
                        }
                        else
                        {
                            role.contactId = prev_contact_id;
                        }
                        role.Role = '10';
                        if(opp.id != null && opp.id !='')
                        {
                            role.opportunityId = opp.id;
                        }
                        roleList.add(role);
                    }
                    insert(roleList);
                }
            }
            
            
            if(is_anonymous == false)
            {
                StrSaveResult = 'Your donor account created successfully. Thank you for your donation.';
            }
            else
            {
                StrSaveResult = 'Your donation has been added successfully under account (' + prev_account_name.Name+')';
            }
            
            //StrSaveResult = 'Your transaction is being processed. Please check your email for confirmation. If you have any questions, please contact <a href="mailto:zonara.anwar@adamscenter.org">Zonara.anwar@adamscenter.org</a>';
            
        }
        catch (exception ex)
        {
            if(is_anonymous == false)
            {
                Delete account;
            }
            
            StrSaveResult = 'Exception type caught: ' + ex.getTypeName();
            StrSaveResult += '\n<br/>Message: ' + ex.getMessage();
            StrSaveResult += '\n<br/>Cause: ' + ex.getCause();
            StrSaveResult += '\n<br/>Line number: ' + ex.getLineNumber();
            StrSaveResult += '\n<br/>Stack trace: ' + ex.getStackTraceString();
            System.debug('Line number: ' + ex.getLineNumber());
            System.debug('Stack trace: ' + ex.getStackTraceString());
        }
        
        contact_to_use = null;
        contact = null;
        account = null;
        opportunity = null;
        role = null;
        recDonation = null;
        rebilling_cycle = null;
        bluePayment = null;
        account = new Account();
        opportunity = new Opportunity();
        role = new OpportunityContactRole();
        recDonation = new npe03__Recurring_Donation__c();
        bluePayment = new BluePay_DNA__Payment__c();
        rebilling_cycle = new BluePay_DNA__Rebilling_Cycle__c();
        payment_type = 'Credit Card';
        Datetime cDT = System.now();
        next_date = System.now().addHours(1);
        current_date = cDT.format('MM/d/yyyy');
        prev_account_id = '001M000000ZtSf8IAF';
        prev_contact_id = '003M000000TiQ3A';
        //prev_account_id = '001F0000019uZqiIAE';
        //prev_contact_id = '003F000001UTJl3';
        return null;
    }

}
Class ends here
Shahab KhanShahab Khan
Denis i am waiting for your reply.

Thanks
Denis VakulishinDenis Vakulishin
About these lines
public String prev_account_id = '001M000000ZtSf8IAF';
public String prev_contact_id = '003M000000TiQ3A';

1) Ensure that Contact with ID = 003M000000TiQ3A exists.
2) Try to use 18-digit Id(as you use for prev_account_id)

Tell me if this will help.
Denis VakulishinDenis Vakulishin
The simpliest way to check if record exists is to use url like
https://emea.salesforce.com/003M000000TiQ3A

instead of EMEA you should set you server.
Shahab KhanShahab Khan
I have checked

1) Contact with ID = 003M000000TiQ3A exists.
2) I have used 18 digit

Still not succeeded
same exception

User-added image

Please help me
Denis VakulishinDenis Vakulishin
try to use
public Id prev_account_id = '001M000000ZtSf8IAF';
public Id prev_contact_id = '003M000000TiQ3A';


Shahab KhanShahab Khan
Denis i am entering unique data it is using contact.id not prev_contact_id

Denis VakulishinDenis Vakulishin
Sorry, 
I'm wondering how I couldn't see that.
You cannot compare ID and String. 
Try to replace this line 
if(opp.id != null && opp.id !='')

with this 
if(opp.id != null)


This was selected as the best answer
Shahab KhanShahab Khan
Thank you very much Denis its working now.