5

I know there is postinstallation script for when deploying package but can i do something similar when deploying app with force.com migration script? Basically there are custom object data which need to be copy to another environment. This can sometimes troublesome when working on the salesforce app with multiple developers. So I was wondering if I can call postinstallation script or some apex class when deploying to another environment.

1 Answer 1

8

Assuming you are talking about the Ant migration tools, a colleague of mine created an Ant task for the purpose of executing some Apex. The Apex is ScheduledJobs.remove() in this example Ant script:

<taskdef
        name="apex"
        classname="com.claimvantage.force.ant.AnonymousApex"
        classpathref="ant.additions.classpath"
        />
<apex 
        username="${sf.username}"
        password="${sf.password}"
        serverurl="${sf.serverurl}"
        code="ScheduledJobs.remove();"
        />

The Ant task code is trivial as it builds on code already in ant-salesforce.jar:

package com.claimvantage.force.ant;
import org.apache.tools.ant.BuildException;

import com.salesforce.ant.SFDCAntTask;
import com.sforce.soap.apex.ExecuteAnonymousResult;
import com.sforce.soap.apex.SoapConnection;
import com.sforce.ws.ConnectionException;

public class AnonymousApex extends SFDCAntTask {

    private String code;

    public void setCode(String code) {
        this.code = code;
    }

    public void execute() throws BuildException {
        try {
            SoapConnection connection = getApexConnection();
            ExecuteAnonymousResult result = connection.executeAnonymous(code);
            if (result.isSuccess()) {
                log("Successfully executed apex code: " + code);
            } else {
                throw new BuildException(result.getExceptionMessage());
            }
        } catch (ConnectionException e) {
            log("Note: use ant -verbose to get more information on the failure");
            throw new BuildException("Failed: " + e.getMessage(), e);
        }
    }
}

To use this Ant task, compile the code with ant-salesforce.jar and ant.jar in the classpath and then put the resulting class in a .jar file and reference that and ant-salesforce.jar in the taskdef classpath.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .