Activity Forums Salesforce® Discussions Post Install Script in Salesforce for deleting custom object data

  • Post Install Script in Salesforce for deleting custom object data

    Posted by Bhavesh on September 8, 2016 at 5:25 am

    I am deleting custom object data using Post Install Script. if data is huge, post install script error is coming.

    I am writing below code in it.

    List<UserMessage__c> m_loUserMessage;
    m_loUserMessage = [select Title__c, MsgLine1__c, MsgLine2__c, MsgLine3__c, MsgLine4__c, MsgLine5__c, ReturnPage__c,
    Increment__c from UserMessage__c order by Increment__c];
    if(m_loUserMessage.size() > 10)
    {
    for(Integer i=0;i<(m_loUserMessage.size()-10);i++)
    {
    delete m_loUserMessage[i];
    }
    }

    Any one have suggestion for this for loop. Some of our customers has huge data. I think so that this error is coming.

    Where should i put this for loop

     

    Bhavesh replied 8 years, 3 months ago 2 Members · 2 Replies
  • 2 Replies
  • Surbhi

    Member
    September 8, 2016 at 8:34 am

    Hi Bhavesh,

    In the for loop, where you are deleting the record, you are performing DML operation inside the for loop and we can perform only limited DML operation in a thread.

    Instead of deleting this in for loop, you can add the records to a list and then delete that list. But as you are querying the custom object data into a list, then there is also limit of querying into a list.

    So, I would suggest using a batch class for this as it will handle really a huge amount of data. Please refer the below sample code for executing batch class from Post Install Script:

    public without sharing class InstallationSystem implements Database.Batchable<SObject>, InstallHandler {
    public void onInstall(InstallContext ic) {
    // Initialize some data here
    Database.executeBatch(this);
    }
    public Database.QueryLocator start(Database.BatchableContext bc) {
    return Database.getQueryLocator([some query]);
    }
    public void execute(Database.BatchableContext bc, Sobject[] records) {
    }
    public void finish(Database.BatchableContext bc) {
    }
    }

    I hope this will help you.

    Please let me know if you have any query.

    Thanks

  • Bhavesh

    Member
    September 8, 2016 at 8:46 am

    Hello Surabhi,

    Thanks for quick reply.

    I am doing another operation also in post install script. Please check below whole code of my post install script.

    public class TaxNowPostInstall implements InstallHandler
    {
    public void onInstall(InstallContext context)
    {
    if(context.previousVersion() == null) // first install - create a new TaxNow Settings Record
    {
    //Insert some data when package install
    TaxNowSetting__c tns = new TaxNowSetting__c(Name='AvaTax',Active_Setting__c=true,
    Service_URL__c = 'https://avatax.avalara.net',TriggerLimit__c = 1);
    insert(tns);
    //Searching prodcut 'Tax Refund' from product catlog if it is not there, we are inserting it.
    List<List<Product2 >>searchList = [FIND :'Tax Refund' RETURNING Product2 ];
    Product2 pr = new Product2();
    PriceBook2 customPriceBook = new PriceBook2();
    if(searchList.size() == 0)
    {

    pr.Name='Tax Refund';
    pr.isActive=true;
    pr.ProductCode = 'Tax Refund';
    insert pr;

    // Insert Pricebook

    customPriceBook.Name='Tax Refund';
    customPriceBook.IsActive=true;
    insert customPriceBook;
    }
    // Insert Product
    List<EntityUse__c> initialCodes = new List<EntityUse__c>
    {
    new EntityUse__c(Description__c='Non-resident (Canada)', Name ='R'),
    new EntityUse__c(Description__c='Commercial Fishery (Canada)', Name ='Q'),
    new EntityUse__c(Description__c='Commercial aquaculture (Canada)', Name ='P'),
    new EntityUse__c(Description__c='Other (requires Exempt Reason Desc) (both)', Name ='L'),
    new EntityUse__c(Description__c='Direct mail (United States)', Name ='K'),
    new EntityUse__c(Description__c='Direct pay permit (United States)', Name ='J'),
    new EntityUse__c(Description__c='Industrial production / manufacturer (both)', Name ='I'),
    new EntityUse__c(Description__c='Commercial agricultural production (both)', Name ='H'),
    new EntityUse__c(Description__c='Resale (both)', Name ='G'),
    new EntityUse__c(Description__c='Religious or educational org (both)', Name ='F'),
    new EntityUse__c(Description__c='Charitable or benevolent org (both)', Name ='E'),
    new EntityUse__c(Description__c='Foreign diplomat (both)', Name ='D'),
    new EntityUse__c(Description__c='Tribe / Status Indian / Indian Band (both)', Name ='C'),
    new EntityUse__c(Description__c='State government (United States)', Name ='B'),
    new EntityUse__c(Description__c='Federal government (United States)',Name='A')
    };
    insert initialCodes;
    List<Shipping_Codes__c> initialsCodes = new List<Shipping_Codes__c>
    {
    new Shipping_Codes__c(Description__c='Delivery by company vehicle before passage of title', Name ='FR010000'),
    new Shipping_Codes__c(Description__c='After passage of title', Name ='FR010100'),
    new Shipping_Codes__c(Description__c='Shipping Only (Not paid directly to common carrier)', Name ='FR010200'),
    new Shipping_Codes__c(Description__c='Common carrier - FOB destination', Name ='FR020000'),
    new Shipping_Codes__c(Description__c='Common carrier - FOB origin', Name ='FR020100'),
    new Shipping_Codes__c(Description__c='Common carrier - FOB unknown', Name ='FR020200'),
    new Shipping_Codes__c(Description__c='Non-common carrier - FOB destination', Name ='FR020300'),
    new Shipping_Codes__c(Description__c='Non-common carrier - FOB origin', Name ='FR020400'),
    new Shipping_Codes__c(Description__c='Non-common carrier - FOB unknown', Name ='FR020500'),
    new Shipping_Codes__c(Description__c='Direct Mail-printed material for mass audience delivery', Name ='FR020600'),
    new Shipping_Codes__c(Description__c='Charges that exceed the actual cost of delivery', Name ='FR020700'),
    new Shipping_Codes__c(Description__c='Charges that exceed reasonable and prevailing rates', Name ='FR020800'),
    new Shipping_Codes__c(Description__c='Shipping And Handling Combined', Name ='FR020900'),
    new Shipping_Codes__c(Description__c='Common carrier - FOB destination', Name ='FR030000'),
    new Shipping_Codes__c(Description__c='Common carrier - FOB origin',Name='FR030100'),
    new Shipping_Codes__c(Description__c='Common carrier - FOB unknown', Name ='FR030200'),
    new Shipping_Codes__c(Description__c='Non-common carrier - FOB destination', Name ='FR030300'),
    new Shipping_Codes__c(Description__c='Non-common carrier - FOB origin', Name ='FR030400'),
    new Shipping_Codes__c(Description__c='Non-common carrier - FOB unknown', Name ='FR030500'),
    new Shipping_Codes__c(Description__c='Direct Mail-printed material for mass audience delivery', Name ='FR030600'),
    new Shipping_Codes__c(Description__c='Charges that exceed the actual cost of delivery', Name ='FR030700'),
    new Shipping_Codes__c(Description__c='Charges that exceed reasonable and prevailing rates', Name ='FR030800'),
    new Shipping_Codes__c(Description__c='Electronically Delivered', Name ='FR030900'),
    new Shipping_Codes__c(Description__c='Temporary Unmapped Freight SKU - taxable default', Name ='FR040000'),
    new Shipping_Codes__c(Description__c='Miscellaneous', Name ='FR999999')
    };
    insert initialsCodes;
    }
    else // upgrade - make sure all TaxNowSettings have a value in TriggerLimit
    {
    for(TaxNowSetting__c tns : [select TriggerLimit__c from TaxNowSetting__c])
    {
    //once avalara package install, we insert TriggerLimit == 1 by default.
    if(tns.TriggerLimit__c == null)
    {
    tns.TriggerLimit__c = 1;
    update(tns);
    }
    }
    }

    }
    }

    Where should i wrote batch function in this script. Please suggest me. If possible please modify it and send me again.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos